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".
Schematics:
Source code for ESP32:
Code: Select all
/*|--------------------------------------------------------------------------------|*/
/*|Author: MARTIN CHLEBOVEC |*/
/*|Web: https://arduino.php5.sk/udp-control-esp32.php?lang=en |*/
/*|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()
{
Serial.begin(115200);
pinMode(led, OUTPUT);
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (udp.listen(4444)) {
Serial.print("UDP running at IP: ");
Serial.print(WiFi.localIP());
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(packet.remoteIP());
Serial.print(":");
Serial.print(packet.remotePort());
Serial.print(", Receiver: ");
Serial.print(packet.localIP());
Serial.print(":");
Serial.print(packet.localPort());
Serial.print(", Message lenght: ");
Serial.print(packet.length());
Serial.print(", Payload: ");
Serial.write(packet.data(), packet.length());
Serial.println();
String myString = (const char*)packet.data();
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
}
Packet Sender can be downloaded from: https://packetsender.com/