ESP32 Control Via UDP Datagrams
by martinius96 in Circuits > Arduino
33275 Views, 4 Favorites, 0 Comments
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 for one output. This tutorial is from Your-IoT. 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.
Used Software
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 and ESP32
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.29 and port 1234 to Packet Sender's destination IP address 192.168.1.246 and port 57927.
ESP32 and Its UART
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: "Turning on relay", "Turning off relay". Relay in this case has inverted logic, so LOW signal turns it on and HIGH signal turns it off. So you can edit that part on your own.
2024 update. Sketch was tested under latest Arduino Core 3.0.X for ESP32 and working without any problem. Tested on devkit with ESP32-WROOM-32 MCU, the most used chip.
Also, if you set UDP server port to 1234 on Packet Sender side, you will be able to receive each second information from the ESP32 node, as it is sending broadcast on that port within whole LAN network with text payload "Anyone here?".