Page 1 of 1

ESP32 control via UDP datagrams

Posted: Thu Sep 05, 2019 10:14 pm
by martinius96
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 and port 1234 to Packet Sender's destination IP address 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".
Source code for ESP32:

Code: Select all

/*|Author: MARTIN CHLEBOVEC                                                        |*/
/*|Web:                      |*/
/*|Doska: ESP32 DevkitC v4 / ESP32 Devkit v1                                       |*/
 //Revision - updated: 16. August 2020
#include "WiFi.h"
#include "AsyncUDP.h"
const char* ssid = "WIFI_NAME";
const char* pass = "WIFI_PASSWORD";
const int led = 2; //D pin (Build-in LED for Devkit V1)
AsyncUDP udp;
void setup()
  pinMode(led, OUTPUT);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
  if (udp.listen(4444)) {
    Serial.print("UDP running at IP: ");
    Serial.println(" port: 4444");
    udp.onPacket([](AsyncUDPPacket packet) {
      Serial.print("Type of UDP datagram: ");
      Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
      Serial.print(", Sender: ");
      Serial.print(", Receiver: ");
      Serial.print(", Message lenght: ");
      Serial.print(", Payload: ");
      Serial.write(, packet.length());
      String myString = (const char*);
      if (myString.startsWith("ON")) {
        Serial.println("Action: Turning on relay");
        digitalWrite(led, HIGH);
      } else if (myString.startsWith("OFF")) {
        Serial.println("Action: Turning off relay");
        digitalWrite(led, LOW);
      packet.printf("ESP32 received %u bytes of datas", packet.length()); //send what you received to sender via UDP
void loop()
  delay(1000); //if no delay, UDP message will not be loaded
More interesting projects and more information about this project can be found at:
Packet Sender can be downloaded from:

Re: ESP32 control via UDP datagrams

Posted: Mon Oct 07, 2019 3:50 pm
by fasani
Very nice example.
We also use UDP for some of our LED projects. The only limitation is that at least in Arduino framework, you can only receive until 1,47 Kb that is more or less the Maximum Transport Unit (MTU) in the ESP32.

More than that and you should use a buffering function. And that at least in Arduino framework can be done but in TCP level (Also http) but not in UDP. Anyways 1,4 Kb is already a lot of data.

The project is open source and use it to control addressable LEDs

If you need to surpass the MTU is also possible to use compression.
I forked Google's Brotli compressor to add the right headers for the ESP32, there is also a compression / decompression example available