In this tutorial we will make two ESP8266 as wireless serial (chat application) using UDP communication protocol. You can have communication between multiple ESPs at the same time. We are using UDP broadcast. UDP is unreliable, but works for many application. UDP gives advantage of sending one message to all ESP8266 devices on same network at the same time using broadcast.
In this post I will explain two types of ESP to ESP communication methods.
One ESP to another ESP communication
In this we make one ESP as Access point and another as Client which connects to access point and starts communication. This example is like chat using two ESPs.
ESP8266 Code for ESP to ESP Communication
Access Point ESP Program
This ESP makes access point. Access point always have IP: 192.168.4.1
/* Wireless Serial using UDP ESP8266 Hardware: NodeMCU Circuits4you.com 2018 Master Board creates Access Point */ #include <ESP8266WiFi.h> #include <WiFiUdp.h> const char *ssid = "circuits4you"; const char *pass = "password"; unsigned int localPort = 2000; // local port to listen for UDP packets IPAddress ServerIP(192,168,4,1); IPAddress ClientIP(192,168,4,2); // A UDP instance to let us send and receive packets over UDP WiFiUDP udp; char packetBuffer[9]; //Where we get the UDP data //======================================================================= // Setup //======================================================================= void setup() { Serial.begin(9600); Serial.println(); WiFi.softAP(ssid, pass); //Create Access point //Start UDP Serial.println("Starting UDP"); udp.begin(localPort); Serial.print("Local port: "); Serial.println(udp.localPort()); } //====================================================================== // MAIN LOOP //====================================================================== void loop() { int cb = udp.parsePacket(); if (!cb) { //If serial data is recived send it to UDP if(Serial.available()>0) { udp.beginPacket(ClientIP, 2000); //Send UDP requests are to port 2000 char a[1]; a[0]=char(Serial.read()); //Serial Byte Read udp.write(a,1); //Send one byte to ESP8266 udp.endPacket(); } } else { // We've received a UDP packet, send it to serial udp.read(packetBuffer, 1); // read the packet into the buffer, we are reading only one byte Serial.print(packetBuffer); delay(20); } } //======================================================================
Client ESP8266 Program
This ESP connects to access point. First Device which connects to Access point have 192.168.4.2 IP
/* Wireless Serial using UDP ESP8266 Hardware: NodeMCU Circuits4you.com 2018 Slave Board connects to Access Point */ #include <ESP8266WiFi.h> #include <WiFiUdp.h> const char *ssid = "circuits4you"; const char *pass = "password"; unsigned int localPort = 2000; // local port to listen for UDP packets IPAddress ServerIP(192,168,4,1); IPAddress ClientIP(192,168,4,2); // A UDP instance to let us send and receive packets over UDP WiFiUDP udp; char packetBuffer[9]; //Where we get the UDP data //====================================================================== // Setup //====================================================================== void setup() { Serial.begin(9600); Serial.println(); WiFi.begin(ssid, pass); //Connect to access point Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); //Start UDP Serial.println("Starting UDP"); udp.begin(localPort); Serial.print("Local port: "); Serial.println(udp.localPort()); } //====================================================================== // MAIN LOOP //====================================================================== void loop() { int cb = udp.parsePacket(); if (!cb) { //If serial data is recived send it to UDP if(Serial.available()>0) { udp.beginPacket(ServerIP, 2000); //Send Data to Master unit //Send UDP requests are to port 2000 char a[1]; a[0]=char(Serial.read()); //Serial Byte Read udp.write(a,1); //Send one byte to ESP8266 udp.endPacket(); } } else { // We've received a UDP packet, send it to serial udp.read(packetBuffer, 1); // read the packet into the buffer, we are reading only one byte Serial.print(packetBuffer); delay(20); } } //=======================================================================
Results one to one ESP Communication
After Uploading program in both ESP. Open Serial monitor of both esp8266 and type some message i.e. Send some serial data to ESP8266. Note that baud rate is 9600.
From above result it is clear that UDP communication have some problems, If data is repetitive then UDP is OK. Another problem with my program is I am sending single byte to another ESP this creates problem of Packet sequence issue.
Many to Many ESP Communication
In this example all ESP8266 connected on same WiFi network can read messages of all devices and can send message to all WiFi Devices. The basic difference between one to one and Many to Many is all ESP8266 are in STA mode (client). To make intercommunication use MODBUS like addressing system. Ignore messages which are having different address.
In this program I am using only one IP i.e. broadcast IP 192.168 43.255. Make sure your IP range and make last digits to 255.
Upload this program in All ESP8266. Change SendIP as per your network series. Give your WiFi Router SSID and Password.
/* Wireless Serial using UDP ESP8266 Hardware: NodeMCU Circuits4you.com 2018 UDP Broadcast multi esp to esp communication */ #include <ESP8266WiFi.h> #include <WiFiUdp.h> const char *ssid = "circuits4you.com"; const char *pass = "password"; unsigned int localPort = 2000; // local port to listen for UDP packets IPAddress SendIP(192,168,43,255); //UDP Broadcast IP data sent to all devicess on same network // A UDP instance to let us send and receive packets over UDP WiFiUDP udp; char packetBuffer[9]; //Where we get the UDP data //====================================================================== // Setup //======================================================================= void setup() { Serial.begin(9600); Serial.println(); WiFi.begin(ssid, pass); //Connect to access point Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); //Start UDP Serial.println("Starting UDP"); udp.begin(localPort); Serial.print("Local port: "); Serial.println(udp.localPort()); } //====================================================================== // MAIN LOOP //====================================================================== void loop() { int cb = udp.parsePacket(); if (!cb) { //If serial data is recived send it to UDP if(Serial.available()>0) { udp.beginPacket(SendIP, 2000); //Send Data to Master unit //Send UDP requests are to port 2000 char a[1]; a[0]=char(Serial.read()); //Serial Byte Read udp.write(a,1); //Send one byte to ESP8266 udp.endPacket(); } } else { // We've received a UDP packet, send it to serial udp.read(packetBuffer, 1); // read the packet into the buffer, we are reading only one byte Serial.print(packetBuffer); delay(20); } } //=======================================================================
Results of Multiple ESP communication
I sent 123456789 from one ESP to another, Few numbers are missed as I said earlier UDP is unreliable. You can clearly see missing data packets. Its useful when you send data multiple times and have some feedback mechanism. This way you can have esp to esp communication.
I follow the tutorial if working perfect.
I have one question when u receive from the server same string who can do to with this string turn on a led in the client.
I try different way but don’t can do it.
Can u help me