ESP32 control via UDP datagrams

Today I would like to point out the possibility of controlling the ESP32 development board by sending UDP messages for easy ON / OFF control. This is one of the easiest ways to send data, process it, and take action based on the information received. ESP32 is a platform that is used in most cases in connection with WiFi connectivity as a webserver or webclient, which connects to a remote server and POST, GET method, or with the integration of the MQTT protocol, sends data to a remote server / guest.

Libraries for ESP32 written in C++ Arduino core (framework) also allows to use asynchronous UDP libraries for sending and receiving data. An interesting feature is asynchronicity, as they work in the "background" and the user does not have to program complex functions to accept connections, process information, etc. To send data to the ESP32 development board, we need a simple UDP client. In my case, I used the Windows version of the relatively well-known Packet Sender client.

It is a universal client (not only) for UDP, it also supports TCP connections, including SSL, so it is possible to make a request on a secure port using a certificate of certification authority, client certificate, server certificate (if Packet Sender receives data as a server). Packet Sender requires you to select the so-called. mode for slow devices. ESP32 was unable to process the datagram coming from the Packet Sender unless the option to add a 500ms pause was selected.


Packet Sender has a simple interface that allows you to specify the transmission protocol, allows you to enter the destination IP address of the device (ESP32), the transmission port and, last but not least, the sent information (text). UDP does not guarantee delivery of messages. Does not contain CRC - check product of sent data. For this reason, ESP32 attempts to respond to the sender's IP address and port with a separate UDP message to confirm receipt of the message.

However, even here, the message does not have to reach Packet Sender. In the log you can see the sent data and received data. Response to sent information is relatively small, about 70ms. During this time, ESP32 can process the datagram, apply the given state to the output terminal (it controls the diode, relay) and send the return datagram to the sender's IP address and port. In this case, ESP32 from IP address 192.168.1.9 and port 1234 to Packet Sender's destination IP address 192.168.1.5 and port 51282.


ESP32 also informs the user via the Serial Line (UART), listing the received connection with the bit length of the information as well as the information itself, can also distinguish the forwarding method (Unicast, Multicast, Broadcast), also displays the sender's IP address including port (later used for return datagram). If the ON / OFF action is detected, ESP32 will also write to UART the status information: "Zapinam rele", "Vypinam rele". So in this case, if you send command ZAP, it will turn on LED. If you send command VYP, it will turn off LED.

Schematics:

Source code for ESP32:

#include "WiFi.h"
#include "AsyncUDP.h"
const char* ssid = "WIFI_NAME";
const char* pass = "WIFI_PASSWORD";
const int rele = 23;
AsyncUDP udp;

void setup()
{
  Serial.begin(115200);
  pinMode(rele, OUTPUT);
  WiFi.disconnect(true);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  if (udp.listen(1234)) {
    Serial.print("UDP Listening on IP: ");
    Serial.println(WiFi.localIP());
    udp.onPacket([](AsyncUDPPacket packet) {
      Serial.print("UDP Packet Type: ");
      Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
      Serial.print(", From: ");
      Serial.print(packet.remoteIP());
      Serial.print(":");
      Serial.print(packet.remotePort());
      Serial.print(", To: ");
      Serial.print(packet.localIP());
      Serial.print(":");
      Serial.print(packet.localPort());
      Serial.print(", Length: ");
      Serial.print(packet.length()); //dlzka packetu
      Serial.print(", Data: ");
      Serial.write(packet.data(), packet.length());
      Serial.println();
      String myString = (const char*)packet.data();
      if (myString == "ZAP") {
        Serial.println("Zapinam rele");
        digitalWrite(rele, LOW);
      } else if (myString == "VYP") {
        Serial.println("Vypinam rele");
        digitalWrite(rele, HIGH);
      }
      packet.printf("Got %u bytes of data", packet.length());
    });
  }
}

void loop()
{
  delay(1000);
  udp.broadcast("Anyone here?");
}

More interesting projects and more information about this project can be found at: Ovládanie (UDP) ESP32
Packet Sender can be downloaded from: https://packetsender.com/

1 Like