Introduction
In this blog post, we will explore how to setup a simple application using Ethernet UDP communication between any device and an ESP32 PLC.
We will explain how to create an EthernetUDP instance on your ESP32 PLC which will be accessible by all devices on the same sub-net. Reading and using the following code, you will be able to easily learn how to send messages to your ESP32 PLC from another device, using one of the simplest communication protocols used in industrial data transfer.
Simple UDP application
We have written the following code:
This code is fully scalable, allowing for easy addition of new UPC orders and responses. All incoming messages are parsed in the responseUdp() function, which returns a response string which can be later sent through the same port it was received or a fixed one (default).
Example
There are many ways to send an UDP packet to a device, but we'll be using the following command:
$ echo "msg" > /dev/udp/10.42.0.20/8888
To listen for a response we'll be relying on this other command:
$ netcat -ul 8889
Notice 8888 and 8889 are the ports where the communication takes place through, as declared in the code. If we send a few messages through port 8888:
We get the following responses on port 8889, while blinking the Q0_0 LED:
UDP communication using Ethernet on ESP32 PLC