Hello there,
I've been struggling with connecting 2 Uno's plus ethernet shields directly with Udp, no router, just a direct ethernetcable in between.
This should work as I've read in examples in this forum, but Udp.endpacket of the sending Arduino keeps returning 0. I tried a normal ethernet cable, I tried a crossover cable, I purchased 2 new Uno Rev 3's and 2 new Ethernet shields V2. To no avail.
I understand the 0 indicates that the sender thinks the receiver is not there.
I borrowed OPS's (other people's sketches) from the forum but get the same status of 0 on Udp.endpacket. I must make a silly mistake. The ethernet cable does not touch the rest of my network it should be totally isolated network of two nodes.
Since the sender cannot send, of course the receiver doesn't receive and prints "No data" (at least that makes sense).
This is the sender's sketch:
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet2.h>
#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
byte mac[] = {0x90, 0xA2, 0xDA, 0x11, 0x13, 0xFA};
IPAddress Send_ip (192, 168, 2, 177);
int Send_port = 8888; // remote port to send to
IPAddress Rec_ip(192, 168, 2, 172);
int Rec_port = 8887; // local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char SendBuffer[] = "S1 Moves";
int stat;
EthernetUDP Udp;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, Send_ip);
Serial.println(Ethernet.localIP());
stat = Udp.begin(Send_port);
Serial.println ("success udp.begin? ");
Serial.println (stat);
}
void loop() {
stat = Udp.beginPacket(Rec_ip, Rec_port);
Serial.println ("success udp.beginpacket? ");
Serial.println (stat);
stat = Udp.write(SendBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println ("success udp.write? ");
Serial.println (stat);
stat = Udp.endPacket();
Serial.println ("success udp.endpacket? ");
Serial.println (stat);
Serial.print ("package sent with content: ");
Serial.println (SendBuffer);
delay (5000);
}
And the receiver's sketch:
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet2.h>
#include <EthernetUdp2.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[] = {
0x90, 0xA2, 0xDA, 0x11, 0x12, 0xF9
};
IPAddress Send_ip (192, 168, 2, 177);
unsigned int Send_port = 8888; // local port to listen on
IPAddress Rec_ip(192, 168, 2, 172);
unsigned int Rec_port = 8887; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, Rec_ip); /* my credentials */
Serial.println(Ethernet.localIP());
Udp.begin(Rec_port);
}
void loop() {
int packetSize = 0;
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);
}
else
Serial.println ("No data ");
delay(1000);
}
The sender's serial output is:
192.168.2.177
success udp.begin?
1
success udp.beginpacket?
1
success udp.write?
24
success udp.endpacket?
0
package sent with content: S1 Moves
Any suggestion is highly appreciated.
Ton