Hello, I am trying to send UDP packages between 2 arduino's but can't get it working. I have been working on this for 2 days now and trying several examples but nothing works, there must be something simple that I miss.
My setup:
Arduino Nano which has to send data to an Arduino Due, both network setups are working when I load a webserver file on them and I can see them from my browser on the PC.
I have tried a crossover cable between the the 2 network adapters to eliminate network problems but this didn't work neither, the link leds are on and the data leds light up when the UDP message is send but nothing on the receiver. Please help, I don't know what I can do more.
Code for the Nano (sender):
#include <EthernetENC.h>
#include <EthernetUdp.h>
#define UDP_TX_PACKET_MAX_SIZE 80
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xD1, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888; // local port to listen on
IPAddress remoteIP(192, 168, 1, 180);
unsigned int remotePort(8880);
char ReplyBuffer[] = "UDP test"; // a string to send back
EthernetUDP Udp; // An EthernetUDP instance to let us send and receive packets over UDP
void setup() {
Ethernet.begin(mac, ip); // start the Ethernet
Serial.begin(9600);
if (Ethernet.hardwareStatus() == EthernetNoHardware) { // Check for Ethernet hardware present
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
else
{
Serial.println("Ethernet shield detected");
}
// start UDP
Udp.begin(localPort);
}
void loop() {
Serial.print("Send UDP packet");
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
delay(1000);
}
And the code for the Due which should receive the above:
#include <EthernetENC.h>
#include <EthernetUdp.h>
#define UDP_TX_PACKET_MAX_SIZE 80
// 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, 180);
unsigned int localPort = 8880; // local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged and received"; // a string to send back
EthernetUDP Udp; // An EthernetUDP instance to let us send and receive packets over UDP
void setup() {
Ethernet.init(A5);
Ethernet.begin(mac, ip); // start the Ethernet
Serial.begin(9600);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
else {
Serial.println("Ethernet shield detected");
}
// start UDP
Udp.begin(localPort);
}
void loop() {
int packetSize = Udp.parsePacket(); // if there's data available, read a packet
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);
}```