Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By danmarion
#47479 Hi, I am writing an application that will send data from the esp8266 to another device over wifi using a UDP connection, i.e. not through the internet or a router. Below is the part of the code (brought down to its simplest form) that deals with the connection. I am using AT commands on the Arduino UNO. The blue light flashes on the esp8266 and I am able to connect to the other device (iPad) but I can't seem to be able to send my data. I might (most likely) be missing something very basic in the code but before I throw the whole thing through the window, can someone point me in the right direction? Thank you.
[code]

#include "SoftwareSerial.h"
SoftwareSerial esp8266(5,6);
void setup() {

Serial.begin(38400, SERIAL_8N1);
esp8266.begin(38400);

esp8266.println("AT+RST\r\n");
delay(1000);
esp8266.println("AT+CIPCLOSE\r\n");
esp8266.println("AT+CIPMUX=0\r\n");
esp8266.println("AT+CWMODE=3\r\n");
String udpmode = "AT+CIPSTART=";
udpmode+="4,",
udpmode+= "UDP";
udpmode+=",";
udpmode+="196.168.4.1";
udpmode+=",10110,1001,0";
esp8266.println("udpmode\r\n");
esp8266.println("AT+CIPMODE=1\r\n");

}

void loop() {
// Send data;
String mydata = "123456";// Real data is variable. Needed because mydata contains periods and special characters
esp8266.println("AT+CIPSEND=6\r\n");
delay (2000);
esp8266.println("mydata\r\n");
delay(2000);
Serial.println(mydata);// So that I can see the data flowing on the serial monitor
}
// End of code
User avatar
By xplorn
#47622 I haven't tried it in Arduino. Here is a simple example for NodeMCU lua. It sends a PIR motion sensor result to syslog.

Code: Select allwifi.sta.setip({ip="10.0.0.2",netmask="255.255.255.0",gateway="10.0.0.1"})
wifi.setmode(wifi.STATION)
wifi.sta.config("myAp","myPassword")

cu=net.createConnection(net.UDP)
cu:on("receive", function(cu, c) print(c) end)
cu:connect(514, "10.0.0.10")

inpin=6                         -- GPIO12 input
gpio.mode(inpin,gpio.INPUT) 

function motion()
  pir_state=gpio.read(inpin)
  print("PIR read:"..pir_state)
  motion_status="No Data"
  if pir_state==0 then
    motion_status="Not Activated"
    logmessage="10.0.0.2 pir[001]: PIR Motion Sensor: Motion Not Detected"
  else
    motion_status="Activated"
    logmessage="10.0.0.2 pir[001]: PIR Motion Sensor: Motion Detected"
  end
  cu:send(logmessage)
  print(motion_status)

  return pir_state
end

-- 5s sensor read
tmr.alarm(1,5000,1, function()motion()end)