ESP32 UDP broadcast listener – get information from other sensors

In my home control project I have several ESP8266, Arduino Yun and Arduino Uno WiFi modules placed all around my house. They have different functions like motion detection, alarm system or remote light control. These devices announce their status as UDP broadcasts on my local WiFi network.

Why using UDP broadcast instead of a point to point transfer? Because I want to have the status available on any phone, tablet or computer without the need of a central server, I chose UDP broadcast. This way any device can listen to the UDP broadcast and display the information or send control commands to others. In addition I do not need to work with fixed IP addresses but instead the devices have their dynamic IP’s assigned by the router.

By listening to the broadcast any device can build up a list of other devices IP addresses instead of having a fixed list of devices that needs to be maintained everytime I add a new device.

The software to listen to UDP broadcasts is quite simple.

Include the required header files and define some variables

Start the listener in setup()

Check if a UDP broadcast was received in loop()

If a message was received (udpMsgLength != 0) parse the message. The messages are all in the form of an JSON object and “standardized” for all devices sending such status messages. The broadcast we are looking for is in the format

which comes from the security device in my backyard. This device has beside of a motion sensor as well a temperature and humidity sensor attached. Inside the broadcast

tells me the device name, “sb1” stands for security backyard device 1

are the measured temperature, humidity and the calculated heat index.

In the routine getUDPbroadcast() we transform the received broadcast message into an JSON object and then check for these entries. Once we have extracted the values, they are displayed on the TFT.



Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory