ESP32 control via UDP datagrams
Today I would like to point out the possibility to control the ESP32 development board by sending UDP messages for easy ON / OFF control. It is one of the simplest methods to send data, process it and take action based on the information received. ESP32 is a platform that in most cases is used in connection with WiFi connectivity as a web server or web client that 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 ++, so-called The Arduino core (framework) also allows the use of asynchronous UDP libraries for sending and receiving data. Interesting is asynchrony, as they work in the "background" and the user does not have to program complex functions for accepting connections, processing information, etc. In order to be able to send data to the ESP32 development board, we need a simple UDP client. In my case, I used the Windows version of the fairly 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 authority certificate, 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 did not manage to process the datagram coming from the Packet Sender if the option with the addition of a 500ms pause was not 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 information sent (text). UDP does not guarantee message delivery. It does not contain CRC - control product of sent data. For this reason, ESP32 tries to respond to the sender's IP address and port with a separate UDP message to acknowledge receipt of the message.
However, even here the message does not have to reach the Packet Sender. In the log it is possible to see the sent data as well as the received data. The response to the sent information is relatively small, about 70ms. During this time, the ESP32 manages to process the datagram, apply the given state to the output terminal (controls the diode, relay) and send the return datagram to the IP address and port of the sender. 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 and 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, the ESP32 also displays information on the application of the status on the UART: Switching on relay, Switching off relay.
Packet Sender can be downloaded from: https://packetsender.com/