Tuesday, October 11, 2016

ESP8266 UDP Server Example

Hello everyone,

In this tutorial, i will show you how to receive UDP Packets via ESP8266.

I assume that you know something about UDP and i will not show you to how udp is working.

In this tutorial, will sent an UDP packet via pc that connected to same network with esp8266 and we show the UDP packets on Serial Port Screen.

This tutorial contains 3 simple steps. 2 of steps are main and final step is for testing.

We need :

Hardware :
1 x Esp8266 Board (I am using ESP8266 -07)
1 x USB to RS232 Chip for programming (FTDI)


Software :
Packet Sender ( for sending UDP Packets) Download

Step 1 : Connecting Wi-Fi Network

#include <ESP8266WiFi.h>

const char* ssid     = "KeyTech-1";
const char* password = "TOBB_Muhendis";


void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
  WiFi.config(IPAddress(192, 168, 1, 60), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {

}


 
I dont explain this step because this is very common part for esp8266.

Note: I set my ip address static 192.168.1.60
WiFi.config(IPAddress(192, 168, 1, 60), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0)); //This line is optional... 

Step 2 : Starting UDP Server

We use WifiUDP library for setting an UDP Server.

Firstly, we create a WiFiUDP Object and i have selected udp server port 2807. You can change your port number whatever you want or which port yo need to listen. Also i created a packet buffer and i selected its size as 2. You can change it to how many bytes you need in your project.

//Creating UDP Listener Object.
WiFiUDP UDPTestServer;
unsigned int UDPPort = 2807;


//Packet Buffer
const int packetSize = 2;
byte packetBuffer[packetSize];



Secondly, in void setup() , starting udp listener ...

 UDPTestServer.begin(UDPPort); 



Thirdly, creating a udp message handle. You Can access your data in myData variable. I selected to it as string. You can change it what you need or what you are sending. I will sent String type packets for testing ...

void handleUDPServer() {
  int cb = UDPTestServer.parsePacket();

  if (cb) {
    UDPTestServer.read(packetBuffer, packetSize);

    String myData = "";
   
    for(int i = 0; i < packetSize; i++) {
      myData += (char)packetBuffer[i];
    }

    Serial.println(myData);

  }
}


 Complete Project Code : 

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid     = "XXX";
const char* password = "XXX";

WiFiUDP UDPTestServer;
unsigned int UDPPort = 2807;

const int packetSize = 2;
byte packetBuffer[packetSize];

void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
  WiFi.config(IPAddress(192, 168, 1, 60), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  UDPTestServer.begin(UDPPort);
 
}

int value = 0;

void loop() {
   handleUDPServer();
   delay(1);
}

void handleUDPServer() {
  int cb = UDPTestServer.parsePacket();
  if (cb) {
    UDPTestServer.read(packetBuffer, packetSize);
    String myData = "";
    for(int i = 0; i < packetSize; i++) {
      myData += (char)packetBuffer[i];
    }
    Serial.println(myData);
  }
}



 Download Complete Project

 Step 3 : Testing UDP Server ...

In this step we will test our esp8266 udp listener ... Run the packetsender program that you downloaded before.  On below picture, i explain how you can use the program. Program will send UDP Packets when press the Send button.



Here is the final result ...




 
 
You can see your UDP Packets on Serial Port Screen.

I hope it will helps you on your projects.  You download complete project on below link. 



Download Complete Project



7 comments:

  1. Really Good Tutorial. Extremely detailed and simple to follow.

    ReplyDelete
  2. Ja - das hat sofort funktioniert.
    Nur -!- wie sende ich Daten an PacketSender oder einen anderen ESP?
    automatic translation
    Yes - it worked immediately.
    Only -! - how do I send data to PacketSender or another ESP?

    ReplyDelete
    Replies
    1. Hello,


      Please try code below.


      //192.168.1.60 should be your ESP8266 Address.

      UDPTestServer.beginPacket(IPAddress(192, 168, 1, 60), 3000);
      String Packet = "TestPacketESPtoESP";
      const char *PacketToSend = Packet.c_str();
      UDPTestServer.write(PacketToSend);
      UDPTestServer.endPacket();

      Note : Sorry i have no time to test it.

      Delete
    2. This comment has been removed by the author.

      Delete
  3. can you write a tutorial for UDP client?

    ReplyDelete
  4. It is not working. The ESP8266 is connecting properly to the WiFi but when I send a Package the ESP does not print anything in the Serial Monitor.
    Its true that the two devices have to be on the same WiFi?
    And do I need to download the WifiUDP library? Even if I don't get any errors?

    ReplyDelete
    Replies
    1. Hello my friend,

      Are you sure about your computer send the command correctly, Maybe your firewall or antivirus blocks the Sender program.

      Please share your experiences and your code. It is hard to tell what is your problem

      Delete

ESP8266 SoftwareSerial Library & Level Shifter for ESP8266 - Arduino

Hello everyone, I have found a very usefull software library for esp8266. Library works well with Arduino IDE. In this tutorial, I will s...