Communication between devices that doesn’t require a server (UDP code examples)

UDP broadcast

Two things to keep in mind about UDP broadcast:

  • UDP broadcast is not a safe method. There is no check if anyone has received the packet. It is just sent out on the network and that’s it. If a recipient miss it, no warning or error is thrown.
  • UDP broadcast works only in your local network. You cannot send a UDP broadcast to “the world”. ISPs, modems and specially mobile networks do not allow it.

UDP broadcast sender example for ESP8266

This code snippet shows how to initialize and use a simple UDP broadcast sender for the ESP8266.

This is a very simplified example of an UDP broadcaster. As I am sending only status messages, which are JSON objects converted to ASCII strings, the only argument for the function is a String. If you want to send binary data, you would need to adapt this.

The function used is call beginPacketMulticast, which is in my opinion a wrong name, because there is a difference between UDP Multicast and UDP Broadcast. For multicast, the packet is sent only to a list of IP addresses that are registered to the multicast, while broadcast means the packet is just sent out and whoever’s IP address matches the mask can receive it. But that’s how it is defined in the ESP8266 Arduino framework.

All needed is the WiFiUDP class, the local IP address, the multicast mask (equals broadcast mask) and the UDP port. udpBroadCaster.beginPacketMulticast() starts the communication on a specific port, udpBroadCaster.print() sends the payload (can be repeated if several packets are combined in one broadcast), udpBroadCaster.endPacket() finishes the sending and udpBroadCaster.stop() ends the communication and frees up the used UDP port.

UDP broadcast listener example for ESP8266

The listener code for UDP broadcasts is very simple as well.

Listening to a specific UDP port is initialized in setup() by calling udpListener.begin(udpBcPort);.

In the loop() we then just check if a package has arrived and parse it.

UDP broadcast sender example for ESP32

This code snippet shows how to initialize and use a simple UDP broadcast sender for the ESP32.

Nearly the same as for ESP8266, but here the mix-up between multicast and broadcast is cleaned up. udpServer.beginPacket(ipAddr, udpPort);  starts the communication. Important to know: This is the same call for sending a packet to a single IP, then ipAddr is a single IP address) or to broadcast on the network, then ipAddr is a IP mask for the broadcasting.

I added some error checking here as well, which is still missing in my ESP8266 code.

UDP broadcast listener example for ESP32

Finally something that could be taken 99% from my ESP8266 code. Only difference is the spelling of the required header file. In ESP8266 the header file is WiFiUDP.h, in ESP32 the header file is WiFiUdp.h. This are the small traps you can get stuck when moving code from the ESP8266 to the ESP32.



Leave a Reply

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

Free Link Directory