Sending UDP [broadcast vs unicast)

I was wondering if anyone can help me here. It seems like a simple problem but I am getting strange results

Below is a simple example of code to show the problem:


UDP Udp;

void setup() {
   Udp.begin(3000);
}

void loop() {
    Udp.beginPacket("192.168.3.255",3000);
    //Udp.beginPacket("192.168.2.7",3000);
    Udp.write("\n\r");
    Udp.endPacket();
    delay(100);
}

If I flash this code with:
Udp.beginPacket(“192.168.3.255”,3000); (broadcast address)

The code flashes, over several seconds, I get breathing Cyan (but no UDP broadcast packets), followed by breathing Green, followed breathing Cyan and immediate UDP packets.


If I flash this code with:
Udp.beginPacket(“192.168.2.7”,3000); (unicast address)

The code flashes, over several seconds, I get breathing Cyan (but no UDP broadcast packets), followed by breathing Green, followed breathing Cyan but no UDP packets.

Hi @trneal

You do not have the correct method for using udp.beginPacket with an IP address. Here is the doc:

https://docs.particle.io/reference/firmware/core/#beginpacket-

So try this instead:

Udp.beginPacket(IPAddress(192,168,3,255),3000);

Actually I get exactly the same results using either “192.168.3.255” and IPAddress(192,168,3,255)

So there is really two questions here:

  1. What with the breathing cyan -> breathing green -> breathing cyan after a normal firmware flash.

  2. Why the different behavior between broadcast and unicast addresses.

The same exist symptoms/issues occur with the “192.168.3.255” and 192.168.2.7

Tom

Actually I found by problem between broadcast and unicast relating to an ARP issue between two linked wifi access points.

Maybe the breathing cyan -> breathing green -> breathing cyan [after an otherwise normal flash] is normal. It just caused a delay in the startup of the function.

If your code turns the Particle cloud off explicitly, then you get breathing green, meaning connected to the Internet but not the cloud. I am not sure why you are seeing that if you are not turning off the cloud.

Normally a quoted string IP address like you have does not work. You either need a hostname like “www.google.com” that DNS can resolve, or an IP address object or an uint8_t array. DNS cannot resolve addresses like “192.x.x.x”.

1 Like

Breathing green also happens when the code blocks the cloud house keeping

1 Like