ESP32 control via UDP datagrams

martinius96
Posts: 4
Joined: Thu Dec 13, 2018 1:39 am

ESP32 control via UDP datagrams

Postby martinius96 » Thu Sep 05, 2019 10:14 pm

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.
Image
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.
Image
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".
Image
Schematics:
Image
Source code for ESP32:

Code: Select all

//Author: Martin Chlebovec
//Personal website: https://arduino.php5.sk
//DONATE FOR MORE: https://paypal.me/chlebovec
#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: https://arduino.php5.sk/udp-control-esp32.php
Packet Sender can be downloaded from: https://packetsender.com/

User avatar
fasani
Posts: 44
Joined: Wed Jan 30, 2019 12:00 pm
Location: Berlin
Contact:

Re: ESP32 control via UDP datagrams

Postby fasani » Mon Oct 07, 2019 3:50 pm

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
https://github.com/martinberlin/udpx/tree/develop

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
https://github.com/martinberlin/brotli
Backend developer-PHP Engineer
https://fasani.de Berlin, Germany

Who is online

Users browsing this forum: No registered users and 3 guests