Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By gerardwr
#13114 Please find attached a working UDP Server Example.

The picture below shows (on the right) the use of a Mac as UDP client sending messages, and the serial output of the UDP Server sketch on the ESP (on the left).

Schermafbeelding 2015-03-31 om 17.27.50.png


UdpServer.ino.zip
You do not have the required permissions to view the files attached to this post.
User avatar
By jnogues
#13618 Hi gerardwr, thanks for share your work. I'm starting with esp8266 and I need a UDP server than recives and send packets (sometimes server and sometimes client). I try it with lua in nodemcu and I can't do it.
Now I want to give a chance to ArduinoESP.

If in your example I want to send a response, Do you know how do it?

Regards,

jnogues
User avatar
By bernd331
#16310 Hi,

I try to extend the example. If a Client send something to the ESP8266, the ESP8266 should answer with something. See the code... The ESP8266 answers onyl one time. Then the ESP8266 stop answering to the Client.

I'm sure, I miss something. But what? Any idea?

Thanks and greets, Bernd

Code: Select all/*
 * 31 mar 2015
 * This sketch display UDP packets coming from an UDP client.
 * On a Mac the NC command can be used to send UDP. (nc -u 192.168.1.101 2390).
 *
 * Configuration : Enter the ssid and password of your Wifi AP. Enter the port number your server is listening on.
 *
 */

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

extern "C" {  //required for read Vdd Voltage
#include "user_interface.h"
  // uint16 readvdd33(void);
}

int status = WL_IDLE_STATUS;
const char* ssid = "UPC0864334";  //  your network SSID (name)
const char* pass = "geheim990";       // your network password

unsigned int localPort = 12345;      // local port to listen for UDP packets

byte packetBuffer[512]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // setting up Station AP
  WiFi.begin(ssid, pass);
 
  // Wait for connect to AP
  Serial.print("[Connecting]");
  Serial.print(ssid);
  int tries=0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    tries++;
    if (tries > 30){
      break;
    }
  }
  Serial.println();


printWifiStatus();

  Serial.println("Connected to wifi");
  Serial.print("Udp server started at port ");
  Serial.println(localPort);
  Udp.begin(localPort);
}

void loop()
{
  int noBytes = Udp.parsePacket();
  String received_command = "";
 
  if ( noBytes ) {
    Serial.print(millis() / 1000);
    Serial.print(":Packet of ");
    Serial.print(noBytes);
    Serial.print(" received from ");
    Serial.print(Udp.remoteIP());
    Serial.print(":");
    Serial.println(Udp.remotePort());
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,noBytes); // read the packet into the buffer

    // display the packet contents in HEX
    for (int i=1;i<=noBytes;i++)
    {
      Serial.print(packetBuffer[i-1],HEX);
      received_command = received_command + char(packetBuffer[i - 1]);
      if (i % 32 == 0)
      {
        Serial.println();
      }
      else Serial.print(' ');
    } // end for
    Serial.println();
   
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write("Answer from ESP8266 ChipID#");
    Udp.print(system_get_chip_id());
    Udp.write("#IP of ESP8266#");
    Udp.println(WiFi.localIP());
    Udp.endPacket();
   
    Serial.println(received_command);
    Serial.println();
  } // end if


}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}
User avatar
By gerardwr
#16348
bernd331 wrote:Hi,

I try to extend the example. If a Client send something to the ESP8266, the ESP8266 should answer with something. See the code... The ESP8266 answers onyl one time. Then the ESP8266 stop answering to the Client.

I'm sure, I miss something. But what? Any idea?


Hi Bernd,

Have a look at my example here, that may help:
http://www.esp8266.com/viewtopic.php?f=29&t=2451