ESP32 Broadcast UDP

Hello all,

I am working on a project that features two types of devices (both ESP32s): a central base unit (called the Radio Unit) and field devices (called buttons). The Radio Unit is responsible for monitoring all the buttons via UDP and sending information to them individually (this part is working great). However, my problem is that I need the Radio Unit to send a "reset" message to all the buttons (the reset message is the same for all buttons) at the same time. The best way I know how to do this is to send the message over the broadcast IP address so that all the buttons will receive the message (see my code below).

The IP address of each button is assigned by my Internet Service Provider (the Radio Unit has a set IP address), but their subnet is always 255.255.254.0 with a gateway of 10.187.18.1, thus leading to a broadcast IP address of 10.187.19.255. However, sending a UDP packet over this broadcast IP address from the Radio Unit, I cannot seem to receive this packet at the field devices. What's up with that?

Radio Unit (Tx): ESP32-PoE-ISO

IPAddress local_IP(10,187,19,125); // ESP32's IP Address
IPAddress gateway(10,187,18,1); // gateway
IPAddress subnet(255, 255, 254, 0); // set subnet
IPAddress broadcastIP(10,187,19,255); // broadcast IP address

#define resetMessageSize 4 // number of bytes
    uint8_t udpBufferSendResetMessage[resetMessageSize] = {0}; // initialize buffer to 0
    struct resetMessage
    {
      public:
        uint16_t reset; // boolean flag raised whenever the Radio Unit needs all other emergency buttons to reset
    }; struct resetMessage resetStruct; // create an instance of this struct

void WiFi_ResetMessage(void)
{
  bool resetMessageReceived = false; // flag raised when the reset message was received
  resetStruct.reset = 0xFFFF; // assert flag
  memcpy(udpBufferSendResetMessage, &resetStruct, resetMessageSize); // copy data from resetStruct to udpBufferSendResetMessage
  UDP.beginPacket(broadcastIP, udpPort); // begin a packet transfer to the emergency button that just sent a message
  while(!resetMessageReceived) // try to send the message until the message was received
  {
    resetMessageReceived = UDP.write(udpBufferSendResetMessage, resetMessageSize); // send data packet 
  }
  UDP.endPacket(); // end packet transfer
}

Buttons aka the field devices (Rx): XIAO-ESP32-C3

#define resetMessageSize 4 // size of reset message
    uint8_t udpBufferReceiveResetMessage[resetMessageSize] = {0}; // initialize buffer to 0
    struct resetMessage
    {
      public:
        uint16_t reset; // boolean flag raised whenever the Radio Unit needs all other emergency buttons to reset
    }; struct resetMessage resetStruct; // create an instance of this struct

void WiFi_UDPHandler(bool emergencyFlag)
{
  // Check for an incoming message before sending one out
  udpPacketSize = UDP.parsePacket(); // get incoming packet size
  Serial.print("Incoming packet size: "); Serial.println(udpPacketSize);
  remoteIP = UDP.remoteIP(); // get IP address from transmitting device
  if (udpPacketSize == resetMessageSize) // if the size of the message matches with the reset message
  {
    Serial.println("Receiving reset message...");
    UDP.read(udpBufferReceiveResetMessage, resetMessageSize); // receive incoming message
    memcpy(&resetStruct, udpBufferReceiveResetMessage, resetMessageSize); // copy incoming data (stored in udpBufferReceiveResetMessage) into the resetStruct
    Serial.print("Buffer: 0x"); Serial.println(resetStruct.reset);
    if (resetStruct.reset == 0xFFFF) // if there has been a reset command
    {
      resetUdp = true; // raise flag to trigger re-initialization
    }
  }
}

maybe try..

IPAddress broadcastIP(10,187,255,255); // broadcast IP address
//or
IPAddress broadcastIP(255,255,255,255); // broadcast IP address

good luck.. ~q

Thanks for the reply, but sadly neither of these broadcast IPs worked. I wonder if it is an issue with my ISP blocking broadcast messages from my Radio Unit's IP address.

Yes, sadly broadcasting only works with locally connected devices on the same net..
Won't work across internet..
sorry.. ~q

All my devices are on the same network though. In an earlier version of my program, I was setting the IP addresses of each field device myself and then broadcasting to them from my Radio Unit successfully. However, the broadcasting feature broke whenever I turned over IP address assignment of the field devices to the ISP. I figured that I needed to change my broadcast address, but now I'm thinking that something else is up.

maybe router wants a reboot then.. :slight_smile:

What addresses are they assigning, can you serial print them??

~q

I have one on hand right now with an IP of 10.187.19.183, and the Radio Unit has a set IP of 10.187.19.125.

router could be blocking you..
have you sniffed the network yet??
Wireshark, Microsoft network monitor..

~q

curious, what port??

~q

why, should that not be 2??
maybe it is working??

~q

Both on port 10078.

This is an issue that I have run into before. For some reason, the UDP packet only works when the buffer size is a multiple of 2. Originally, I had the resetMessageSize set to 2 and the reset element of resetStruct set to uint8_t, but this wasn't working either. So, I changed it because I had a theory that maybe the 2 byte size was too small.

UDP is not a guaranteed protocol..
Meaning you may not get all the packets..
You should build something on top to help with this..
ESP32 UDP receiver..
Here's a demo i did a while ago..
Sends a structure, and has a sequence number and simple checksum to validate data..

but, broadcasting 2 bytes should work too, how often are you sending??

~q

1 Like

that's bad..

~q

that's bad too..
not as bad as the other..
this copies 4 bytes from a 2 byte structure, only God knows what them other 2 bytes are..

~q

Before, I had the buffer size set to 2 bytes and the reset element of the resetStruct set to uint16_t and still wasn't able to receive the UDP packet.

I can verify that my UDP is working because I'm able to send UDP packets back and forth between the Radio Unit and field devices whenever I address the field devices' specific IP addresses.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.