Arduino Mega UDP Send & Receive with Different IP Address

Hi, it's my first post about Arduino, and sorry for my bad English...

Now, I make a project consists of three Arduino Megas, and each of them have Ethernet Shield. Well, I want to make a simple send and receive procedure between them using UDP. Each of Arduino, say it Arduino 1,2 and 3, have IP Address, as you can see below:

Arduino 1 : 101.8.18.102
Arduino 2 : 101.8.18.103
Arduino 3 : 101.8.19.101

int portUDP 9600;

From this IP Address configuration, for example, I want to send string "gH" from Arduino 1 by using write() one by one to each Arduino. The result is each Arduino (2 and 3) can't receive this string. When I using broadcast method (send to 101.8.18.255), that is by using:

IPAddress broadcast 101.8.18.255;

Udp.beginPacket(broadcast, portUDP);
Udp.write("gH");
Udp.endPacket();

Arduino 2 can receive the string, even though Arduino 3 still can't receive it because it have different subnet (perhaps).

I am sorry, I still noob about networking etc., but how can I solve this problems, at least I can send and also receive this simple string from one Arduino to another Arduino?

And, in my setup, I only set for Ethernet begin statement like this:

Ethernet.begin(mac, ip); //ip Arduino 1, 2 or 3

Is it necessary for me to set other configuration like dns, gateway and subnet mask? Do you have any recommend setting so it can be work as I expected?

Thank you :slight_smile:

Perhaps yes. If Arduino 1 attempts to send a packet to Arduino 3, it will use the gateway IP 108.8.18.1. You must change the IP of Arduino 3 if it is on the same localnet.

If you send only the mac and ip parameters, the dns server and gateway will be 108.8.18.1, and the subnet will be 255.255.255.0.

Thanks SurferTim for your reply, but unfortunately, my Arduino 3’s IP Address can’t be changed to the same localnet (108.8.18.x), because this is assigned from my mentor :frowning:

I’m trying to make configuration in Ethernet.begin() like this:

For Arduino 1:
IPAddress ip(101, 8, 18, 102);
IPAddress dns1(101, 8, 18, 1);
IPAddress gateway(101, 8, 18, 1);
IPAddress subnet(255, 255, 0, 0);

For Arduino 3:
IPAddress ip(101, 8, 19, 101);
IPAddress dns1(101, 8, 19, 1);
IPAddress gateway(101, 8, 19, 1);
IPAddress subnet(255, 255, 0, 0);

Is it true that configuring subnet by (255,255,0,0) allow me to send broadcast message to IP address (101,8,x,x)?

Are you sure that gateway ip is correct? If they are all using localnet only, it won't matter. It won't try the gateway unless the destination ip is not in the 101.8.x.x range.

The broadcast address for your network is 101.8.255.255.

You must specify all parameters in your begin call.

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

edit: And the answer to your last question is yes. You must use 101.8.255.255 broadcast ip. The broadcast ip is always the last (highest) ip in the subnet. That is not a private ip subnet.Don't know why you are using it as a private localnet.