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.