fbpixel
Tags: ,
4.5
(11)

The NodeMCU ESP8266 is a small microcontroller with a Wifi chip. It is possible to establish a communication between two ESP8266, when they use the same network. In this tutorial, we will see an architecture with two NodeMCUs that will exchange data via the UDP protocol.

If you do not have access to a second ESP8266, you can test the communication with the software Packet Sender.

Matériel

  • Ordinateur
  • NodeMCU ESP8266 x2 ou plus
  • Câble USB A Mâle/Micro B Mâle x2

Code ESP8266 UDP Server

For the Server microcontroller, we are going to create a server to manage the requests coming from the client microcontrollers. We use the ESP8266WiFi.h library which allows the management of the WiFi chip for the ESP8266 cards and the WiFiUdp.h library which allows to manage the UDP requests.

N.B.: Don’t forget to change the values of ssid and password, by the name and the password of the wifi network you want to use

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

WiFiUDP udp;

char packetBuffer[255];
unsigned int localPort = 9999;

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

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  udp.begin(localPort);

  Serial.print(F("UDP Server : ")); Serial.println(WiFi.localIP());
}

void loop() {
  int packetSize = udp.parsePacket();

  Serial.print(" Received packet from : "); Serial.println(udp.remoteIP());
  Serial.print(" Size : "); Serial.println(packetSize);
  Serial.print(" Data : ");
  if (packetSize) {
    int len = udp.read(packetBuffer, 255);
    if (len > 0) packetBuffer[len - 1] = 0;
    Serial.print(packetBuffer);
    udp.beginPacket(udp.remoteIP(), udp.remotePort());
    udp.write("UDP packet was received OK\r\n");
    udp.endPacket();
  }
  Serial.println("\n");
  delay(500);

  Serial.print("[Server Connected] ");
  Serial.println (WiFi.localIP());
}

Code ESP8266 UDP Client

In the client code, we will connect to the server using the previously used IP address.

N.B.: Don’t forget to modify and use the same ssid and password values as in the Server module so that the two ESP8266 cards connect to the same network.

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

WiFiUDP udp;

char packetBuffer[255];
unsigned int localPort = 9999;

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

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  udp.begin(localPort);

  Serial.print(F("UDP Client : ")); Serial.println(WiFi.localIP());
}

void loop() {
  int packetSize = udp.parsePacket();

  Serial.print(" Received packet from : "); Serial.println(udp.remoteIP());
  Serial.print(" Size : "); Serial.println(packetSize);
  Serial.print(" Data : ");
  if (packetSize) {
    int len = udp.read(packetBuffer, 255);
    if (len > 0) packetBuffer[len - 1] = 0;
    Serial.print(packetBuffer);
  }
  Serial.println("\n");
  delay(500);

  Serial.print("[Client Connected] ");
  Serial.println(WiFi.localIP());

  udp.beginPacket("192.168.1.94", 9999);
  udp.write("Send millis: ");
  char buf[20];
  unsigned long testID = millis();
  sprintf(buf, "%lu", testID);
  Serial.print(" Sent : "); Serial.println(buf);

  udp.write(buf);
  udp.write("\r\n");
  udp.endPacket();
}

Result

When the microcontrollers connect to the Wifi and the communication between the two ESP8266 is established, we can see that information is exchanged between the two cards. It is then possible to control a device connected to the client from the server or vice versa.

Applications

  • Create a network of Esp8266 NodeMCUs to manage different devices or sensors

Sources

How useful was this post?

Click on a star to rate it!

Average rating 4.5 / 5. Vote count: 11

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?