Arduino to arduido via ethernet

I’ve been trying to get these two arduinos mega 2560 talk to each other but they just don’t do what i want. I don’t know if i’ve set up the ip, the mac or what i’ve done wrong. I’ve tried using UDP to send a char through, turn that char into an int, and use that int to turn a set of leds to make a binary counter (From 0 to 7).

This if from the talking arduino:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 92);
int i = 1;
unsigned int localPort = 8888;

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

void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(9600);

Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(char(i));
Udp.endPacket();
Serial.begin(9600);
Serial.print(i);
char c = char(i);
Serial.print(char(i));
}

void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(char(i));
Udp.endPacket();
Serial.print("Received. ");
if (i<8) i=1;
else i++;
}
Serial.print("Loop. ");
delay(500);
}

And this is from the listener

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x10
};
IPAddress ip(192, 168, 1, 93);
int i;
unsigned int localPort = 8888;

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(9600);

pinMode(30, OUTPUT);
pinMode(31, OUTPUT);
pinMode(32, OUTPUT);

Serial.print("Started. ");
}

void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize > 0)
{
char c = Udp.read();
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(‘k’);
Udp.endPacket();
i = (int)c;
for (int j=0; j<3; j++)
{
if (bitRead(i-1, j) == 0)
{
digitalWrite(30+j, LOW);
}
else digitalWrite(30+j, HIGH);
}
Serial.print("Received. ");
}
delay(500);
}

I’ve been trying to get the ‘loop’ advance one cycle whenever one of them gets the message of the other one.

In the sender, this won't work.

 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

Try this in the sender only.

 IPAddress dstIP(192,168,1,93);
 int dstPort = 8888;

 Udp.beginPacket(dstIP, dstPort);

You can evaluate the return value for endPacket to see if the destination received the packet (same localnet only).

SurferTim:
Try this in the sender only.

 IPAddress dstIP(192,168,1,93);

int dstPort = 8888;

Udp.beginPacket(dstIP, dstPort);



You can evaluate the return value for endPacket to see if the destination received the packet (same localnet only).

What if i want both to be senders?

I’ve done as you said, no working result yet.

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h> 

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192,168,1,94);
IPAddress dstIP(192,168,1,93);
int i = 0;
unsigned int localPort = 8888;
unsigned int dstPort = 8880;

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

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600);
}

void loop()
{
    Udp.beginPacket(dstIP, dstPort);
    Udp.write(char(i));
    int x = Udp.endPacket();
    Serial.print(x);
    if (i<7) i++;
    else i=0;
    Serial.print("|");
    delay(500);
}
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h> 

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x10
};
IPAddress ip(192,168,1,93);
int i = 0;
unsigned int localPort = 8880;

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

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
}

void loop()
{
    Udp.parsePacket();
    char c = Udp.read();
    i = (int)c;
    Serial.print(i);
    for (int j=0; j<3; j++)
    {
      if (bitRead(i, j) == 0) digitalWrite(30+j, LOW);
      else digitalWrite(30+j, HIGH);
    }
    Serial.print("|");
    delay(500);
}

What does the sender show on the serial monitor for the value of x? If '1' then the packet was sent ok. If '0' then the send failed.

^I check on every loop, the serial monitor shows "0|0|0|0|0..."

Are both devices connected to the same localnet?

They're directly connected. I've tried a normal and a crossover ethernet cable. Neither of them worked.

You'll need to use the crossover cable for direct connections.

The listener code has changed, you are no longer checking if there are actually any bytes to read before attempting to read them.
I wouldn't bother converting the read value to an int, just work with the byte directly.
To aid debugging, I'd serial.print() the received data (convert the byte value to a string first :wink: )
If you only need 2 arduinos talking to each other, you could use the same port number for both.
I'd go via an Ethernet switch (with non-crossover cables obviously), it makes debugging a bit easier. Got wireshark? Plug in a PC and let wireshark sniff out the packets. I'd try sending the data to the PC first and make sure that is getting them, wireshark will tell you exactly what is going on. It won't matter for this debugging that the PC isn't setup to actually receive the data, wireshark will see it.