Comunicacion entre dos arduinos con ethernet shield

Hola que tal, lo que trato de hacer es estabecer una comunicacion entre mis dos arduinos, estoy usando dos arduino mega con un shield ethernet cada uno, ya he intentado con los comandos para la comunicacion UDP pero no he logrado establecer comunicacion

Con este programa es con el que intento mandar el mensaje:

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

byte mac[] = { 0xDA, 0x90, 0xCE, 0xD3, 0xB5, 0xED };
byte subnet[] = { 255, 255, 255, 255 };
char c;
unsigned char mensaje[3]="hola";

IPAddress ip(172, 16, 15, 246);
IPAddress destIp(172, 16, 15, 248);

unsigned int localPort = 8888;
unsigned int remotePort = 5678;

EthernetUDP Udp;

void setup(){
  Ethernet.begin(mac, ip, subnet);
  Udp.begin(localPort);
  Serial.begin(9600);
  Serial.print(Ethernet.localIP());
}

void loop(){
  if (Serial.available()>0){
    c = Serial.read();
    if (c=='l'){
      Udp.beginPacket(destIp, remotePort);
      Udp.write(mensaje, 3);
      Udp.endPacket();
    }
  }
}

Este otro es el que recibiria el mensaje:

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

byte mac[] = { 0xF3, 0xAD, 0x4E, 0xC2, 0x23, 0xE8 };
byte subnet[] = { 255, 255, 255, 255 };
IPAddress ip(172, 16, 15, 248);
String string="";
unsigned int localPort = 5678;

EthernetUDP Udp;

void setup(){
  Ethernet.begin(mac, ip, subnet);
  Udp.begin(localPort);
  Serial.begin(9600);
  Serial.println(Ethernet.localIP());
}

void loop(){
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Tamaño del paquete recibido ");
    Serial.println(packetSize);
    Serial.print("Desde ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }
    Serial.print(", puerto ");
    Serial.println(Udp.remotePort());

    Serial.print("Contiene: ");
    Serial.println(Udp.read());
    Serial.print("\n");
  }
  delay(10);
}

Este ultimo digo yo que si funciona porque al establecer el puerto 5678 como puerto local, recibe algunos mensajes pero no se que son.

Tamaño del paquete recibido 127
Desde 172.16.15.247, puerto 5678
Contiene: 0

Tamaño del paquete recibido 79
Desde 172.16.15.243, puerto 5678
Contiene: 0

Supongo que algo me hace falta o algo estoy haciendo mal, ojalá puedan ayudarme, saludos.

Hola yo tuve el mismo problema y lo solucione ayer.
Dentro del codigo de la arduino que manda el mensaje o lo que quieras busca donde
dice :

Udp.beginPacket(destIp, remotePort);

cambialo por :

Udp.beginPacket("172, 16, 15, 248", 5678);

Yo lo solucione de esa forma. Espero que te sea util. AH y saca el ",3" que esta despues de la variable mensaje

Hola SistemasGGL:

Gracias por la ayuda, tambien he solucionado el problema, aun que no era problema del programa sino que era problema del switch donde estaba conectando los arduinos, sigo sin saber cual es el problema con el switch pero al conectarlo directamente al router del proveedor de internet el programa funcionó correctamente.

Anexo como deje los codigos al final, talvez haya cosas innecesarias pero por el momento me funcionan para enviar mensajes de un arduino a otro.

Codigo para enviar mensaje:

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

byte mac[] = { 0xDA, 0x90, 0xCE, 0xD3, 0xB5, 0xED };
byte subnet[] = { 255, 255, 255, 0 };
char inChar, mensaje[24];
String string="";

IPAddress ip(192, 168, 1, 179);
IPAddress destIp(192, 168, 1, 177);

unsigned int localPort = 5678;

EthernetUDP Udp;

void setup(){
  Ethernet.begin(mac, ip, subnet);
  Udp.begin(localPort);
  Serial.begin(9600);
  string.reserve(200);
  Serial.println(Ethernet.localIP());
  Serial.println();
}

void loop(){
  if (Serial.available()>0){
    inChar = Serial.read();
    string+=inChar;    
    string.toCharArray(mensaje, 24);
    if (inChar=='/'){
      Udp.beginPacket(destIp, localPort);
      Udp.write(mensaje, 24);
      Udp.endPacket();
      string="";
    }
  }
}

Codigo para recibir los mensajes:

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

char packet[24];
byte mac[] = { 0xF3, 0xAD, 0x4E, 0xC2, 0x23, 0xE8 };
byte subnet[] = { 255, 255, 255, 255 };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 5678;

EthernetUDP Udp;

void setup(){
  Ethernet.begin(mac, ip, subnet);
  Udp.begin(localPort);
  Serial.begin(9600);
  Serial.println(Ethernet.localIP());
}

void loop(){
  int packetSize = Udp.parsePacket();
  if (packetSize){
    Serial.print("Tamaño del paquete recibido ");
    Serial.println(packetSize);
    Serial.print("Desde ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3){
        Serial.print(".");
      }
    }
    Serial.print(", puerto ");
    Serial.println(Udp.remotePort());

    Udp.read(packet, 24);
    Serial.print("Contiene: ");
    Serial.print(packet);
    Serial.print("\n\n");
  }
  delay(10);
}

Espero le sea util a alguien mas, saludos :slight_smile:

P.D. el 3 es parte de la sintaxys que miré en la pagina de arduino para mandar cadenas: Ethernet - Arduino Reference