Arduino accepts UDP Unicast messages, but not Broadcast

Hi all,

I'm trying to write a program to accept network UDP signals across the network. However, even just using the example code in Ethernet - Arduino Reference, the packets are not received (packetSize always equals 0) unless the sender device explicitly sends the data to the Arudino's IP address, then it works as expected. Unfortunately I want multiple devices to read these signals so this is not a satisfactory solution for me.

I'm running an Arduino Mega2560 R3 and a regular ethernet shield.

Is this normal? I honestly couldn't figure out why the Broadcast UDP signals weren't being received, or is there another step that I'm missing...?

Thanks!

You must set the ip address in the ethernet shield to the broadcast address of the network it is on. For an example, if the network is 192.168.0.0/24, the broadcast ip is 192.168.0.255. This does not apply to dhcp, since there is no network, so I think it uses 255.255.255.255.

I believe I did that already.

So my code set the IP address of the arduino to be:

IPAddress ip(172, 31, 0, 5);
Ethernet.begin(mac,ip);

The subnet mask of this network I think is 255.255.0.0, does that mean I should add

Ethernet.begin(mac, ip, dns, gateway, subnet);

To match the changed subnet from the default 255.255.255.0?

All that depends on the network settings used by the sender. Normally the network netmask is 255.255.255.0. That is what it uses by default if you do not specify a subnet in the begin call. The broadcast ip is the last ip in that network subnet. This would be the broadcast ip for that ip/subnet.

IPAddress ip(172,31,0,255);
Ethernet.begin(mac,ip);

If the subnet mask is 255.255.0.0, then you must specify that in the begin call and change the ip address to the broadcast ip for the new subnet.

IPAddress ip(172, 31, 255,255);
IPAddress gateway(172,31,0,1);
IPAddress subnet(255,255,0,0);
Ethernet.begin(mac,ip, gateway,gateway,subnet);