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???