Two-arduino Ethernet/Wifi communication using UDP

Hello

I am trying to get 2 Arduino wifi's with Ethernet shields to communicate using Ethernet, and in the future wifi. For now they are connected to each other with a single Ethernet cable, but then I want to upgrade it to being able to communicate over Ethernet and Wifi on the same network, and then eventually being able to communicate with Ethernet/Wifi over the Internet (basically my end goal is remote control).

I am having trouble with the two arduinos connected simply to each other. I modified the UdpSendRecvString example so that one Arduino sends a string to the other, and then the other Arduino sends an acknowledgement. When I run the code, the sender's TX LED blinks and the receiver's RX LED blinks, but nothing appears in the Serial monitor of the receiver that would indicate it received the message, and the sender doesn't receive an acknowledgement.

Sender code:

#include <SPI.h>          // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>  // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168,1,177);

unsigned int localPort = 8888;              // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet,
boolean sentPacket = false;

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // put your setup code here, to run once:
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  Serial.println("Sender");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (!sentPacket) {
    Serial.println(F("Sending packet \'Hello World\'"));
    Udp.beginPacket(IPAddress(192, 168, 1, 178), Udp.remotePort());
    Udp.write("Hello World");
    Udp.endPacket();
    Serial.println(F("Waiting for acknowledgement"));
    sendMillis = millis();
    sentPacket = true;
  } else {
      int packetSize = Udp.parsePacket();
      if(packetSize)
      {
        Serial.print(F("Received packet of size "));
        Serial.println(packetSize);
        Serial.print(F("From "));
        IPAddress remote = Udp.remoteIP();
        for (int i =0; i < 4; i++)
        {
          Serial.print(remote[i], DEC);
          if (i < 3)
          {
            Serial.print(".");
          }
        }
        Serial.print(F(", port "));
        Serial.println(Udp.remotePort());
    
        // read the packet into packetBufffer
        Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
        Serial.println("Contents:");
        Serial.println(packetBuffer);
        while (true) {}
      }
    }
  }
  delay(10);
}

Receiver code:

#include <SPI.h>          // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>  // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };
IPAddress ip(192, 168,1,178);

unsigned int localPort = 8888;              // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // put your setup code here, to run once:
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  Serial.println("Reciever");
}

void loop() {
  // put your main code here, to run repeatedly:
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

How did you connect the two boards together?

It is likely you need a cross-over cable. They look like normal Ethernet cable but usually have a little sticker attached to them saying cross-over. You can also identify the wires in the connectors. See wiki link below

Some newer Ethernet adapters in laptops can do the cross-over internally but its unlikely your Arduino board can do that. An alternative is to connect them to a network switch with two normal cables. The switch will then handle the correct wiring.

In the sender code try sending to port 8888 because that is the port the receiver is listening on.

Udp.beginPacket(IPAddress(192, 168, 1, 178), Udp.remotePort());

Klaus_K:
How did you connect the two boards together?

I used a regular Ethernet cable, so that's probably the problem
Since I didn't have a crossover cable, I switched to using Wifi and successfully got them communicating