UDP Communication between two Arduino Mega/Ethernet shield

I’ve been trying to get two Arduino Megas to talk with one another over UDP. I’ve tried the sample programs, but they are hard to understand. I then tried to code a Send and Recv sketch that sends a simple message from one to the other. The code is below:

Send

// sends a message first

#include <SPI.h>
#include <Ethernet.h>
//#include <EthernetUdp2.h>

#define parm1 10 // local
#define parm2 20 // remote
unsigned int localPort = 8888; // Port

#define EthMegPin 53
#define debugDelay 2500
#define Halt while(true)
#define Forever while(true)
boolean start = true;
byte EthernetID = parm1; //debug
byte remoteID = parm2; //debug

// define UDP object
EthernetUDP localUdp;

char UDPRecvBuffer[UDP_TX_PACKET_MAX_SIZE];
char UDPSendBuffer[] = "ack";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xFE, parm1 };
byte local_IP[4];
byte remote_IP[4];
int packetSize;
byte localScriptToken;
int remote_message = 0;



void setup() {

  pinMode(EthMegPin, OUTPUT);
  digitalWrite(EthMegPin, HIGH);
  Ethernet.init(10);  // Most Arduino shields
  byte EthernetID = parm1; //debug
  byte remoteID = parm2; //debug

  Serial.begin(9600);

  // EthernetID read in setup
  mac[4] = EthernetID;

  IPAddress local_ip( 10, 10, 1, EthernetID );
  IPAddress remote_IP(10, 10, 1, remoteID);
  Ethernet.begin(mac, local_ip);
  //print out the IP address
  Serial.print("Setup: Local IP = ");
  Serial.println(Ethernet.localIP());
  Serial.print("Setup: Rem't IP = 10, 10, 1, ");
  Serial.println(remoteID);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Setup: Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    Halt;
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Setup: Ethernet cable is not connected.");
  }

  int Udp_Conn = localUdp.begin(localPort);
  if (Udp_Conn) {
    Serial.print("Setup: UDP port connection successful to ");
    Serial.println(localPort);
  }
  else {
    Serial.print("Setup: UDP port connection failed to ");
    Serial.println(localPort);
  }
}


void loop() {
  int UdpConn = localUdp.beginPacket(remote_IP, localUdp.remotePort());
  if (UdpConn) {
    Serial.println("Loop: Begin packet successful");
  }
  else {
    Serial.print("Setup: UDP packet begin failed. Host not found");
  }

  remote_message ++;
  itoa(remote_message, UDPSendBuffer, 10);
  int charsSent = localUdp.write(UDPSendBuffer);
  Serial.print(charsSent);
  Serial.println(" characters sent");
  int UdpSent = localUdp.endPacket();
  if (UdpSent) {
    Serial.println("Message sent via UDP");
  }
  else {
    Serial.println("Error occurred during write");
  }
  Halt;
}

and Recv

// receives a message first

#define parm1 20
#define parm2 10
#define EthMegPin 53
#define debugDelay 2500
#define Halt while(true)
#define Forever while(true)
boolean start = false;

#include <SPI.h>
#include <Ethernet.h>
//#include <EthernetUdp2.h>

char UDPRecvBuffer[UDP_TX_PACKET_MAX_SIZE];
char UDPSendBuffer[] = "ack";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xFE, parm1 };
byte local_IP[4];
byte remote_IP[4];
unsigned int localPort = 8888;
int packetSize;
// define UDP object
EthernetUDP Udp;

byte localScriptToken;
int remote_message = 0;

void setup() {

  byte EthernetID = parm2; //debug
  byte remoteID = parm1; //debug

  // EthernetID read in setup
  mac[4] = EthernetID;
  IPAddress local_ip( 10, 1, 1, EthernetID );
  IPAddress remote_IP(10, 1, 1, remoteID);

  Ethernet.begin(mac, local_ip);
  Udp.begin(localPort);

  Serial.begin(9600);
  Serial.println(F("Starting..."));
}

void loop() {

  packetSize = Udp.parsePacket();
  Serial.print(F("Packet Size  "));
  Serial.println(packetSize); delay(5000);

  // receive a message
  if (packetSize) {
    Udp.read(UDPRecvBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.print(F("Buffer  "));
    Serial.println(UDPRecvBuffer); delay(5000);
    localScriptToken = atoi(UDPRecvBuffer);
    // display token
    Serial.print(F("Recv'd "));
    Serial.println(localScriptToken);
    remote_message = localScriptToken;
    delay(debugDelay);
  }

}

When I run these two sketches, the connection appears to start and immediately end before any data is transferred.

Any ideas what I am doing wrong???

It appears you swapped parm1 and parm2 between the SEND/RCV program, but then you also swapped the assignments to EthernetID and remoteID.

This amounts to two swaps, which mean both programs are the same if regards to send/rcv.

Also, your mac address needs to match the mac address that is on the sticker on the ethernet shield. I seriously doubt both shields are the same since that would defeat the purpose.

I’ll double check the IP assignments when I get a chance (i.e., when I have access to my development machine). The whole parm1/parm2/localID/remoteID had confused even me! I’ll probably change my defined/assignment scheme.

But for others information, contemporary Ethernet shields do NOT have a Mac ID assigned. The Mac ID is defined by the user during setup. I’m using a user-definable MAC address, where the last two bytes are set to be the same as the last octet in the local IP address. These shields will never connect to a public network and there will be <<255 nodes at all times!

Thank you for your reply. Sorry if there’s been a delay in responding - I never received notification that anyone replied.

djsfantasi:
But for others information, contemporary Ethernet shields do NOT have a Mac ID assigned. The Mac ID is defined by the user during setup.

Good to know. I only have a couple of ethernet shields from several years ago and they don't have this feature.

That is the purpose of the line, which defines an array mac,

byte mac = { 0xDE, 0xAD, 0xBE, 0xFE, parm1 };

which contains the user defined MAC address for the Ethernet shield. When the shield is initialized by the following line, it’s MAC address is set.

Ethernet.begin(mac, local_ip);