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
}
}
}