Hola!
Mi programacion es muy sencilla
Se trata de un lector de tarjetas que dependiendo de si las tiene registradas o no mada un codigo udp que sera un mensaje en red para otros dispositivos.
El problema que tengo es que al trabajar con el puerto de comunicacion y volcar le programa funciona sin problema
Al desconectar el puerto de comunicacion y conectar la alimentacion directamente no se inicializa la ip y por lo tanto no funciona nada en la red.
te dejo el codigo
// no soy tampoco una crack con el codigo...
//RST D9
//SDA(SS) D8
//MOSI D11
//MISO D12
//SCK D13
#include <SPI.h>
#include <MFRC522.h>
#include <SPI.h>
#include <Ethernet.h>
EthernetUDP Udp;
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x5C, 0x18};
IPAddress ip(192,168,1,15);
unsigned int localport = 8888;
IPAddress remoteIP(192,168,1,200);
unsigned int remotePort = 0002;
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
const int RST_PIN = 9; // Pin 9 para el reset del RC522
const int SS_PIN = 8; // Pin 8 para el SS (SDA) del RC522
MFRC522 mfrc522(SS_PIN, RST_PIN); // Crear instancia del MFRC522
byte validKey1[4] = { 0x4E, 0x4C, 0xB3, 0xC7 }; // Ejemplo de clave valida
byte validKey2[4] = { 0x01, 0x28, 0xB3, 0xC7 };
byte validKey3[4] = { 0x4E, 0x46, 0xB3, 0xC7 };
//Función para comparar dos vectores
bool isEqualArray(byte* arrayA, byte* arrayB, int length)
{
for (int index = 0; index < length; index++)
{
if (arrayA[index] != arrayB[index]) return false;
}
return true;
}
void setup() {
Serial.begin(9600);
Serial.println("****************");
Serial.println("Puerto Serial OK");
Ethernet.begin(mac,ip);
Serial.print("IP : ");
Serial.println(Ethernet.localIP());
Udp.begin(localport);
Ethernet.setSubnetMask(subnet);
SPI.begin(); // Iniciar SPI
mfrc522.PCD_Init(); // Iniciar MFRC522
}
void loop() {
// Detectar tarjeta
if (mfrc522.PICC_IsNewCardPresent())
{
//Seleccionamos una tarjeta
if (mfrc522.PICC_ReadCardSerial())
{
// Comparar ID con las claves válidas
if (isEqualArray(mfrc522.uid.uidByte, validKey1, 4)){
Serial.println("Tarjeta valida");
Udp.beginPacket(remoteIP, remotePort);
Udp.write("message");
Serial.println("Mensaje UDP");
Udp.endPacket();
delay(5000);}
else
{
if (isEqualArray(mfrc522.uid.uidByte, validKey2, 4)){
Serial.println("Tarjeta valida 2");
Udp.beginPacket(remoteIP, remotePort);
Udp.write("message");
Serial.println("Mensaje UDP");
Udp.endPacket();
delay(5000);}
else
{
if (isEqualArray(mfrc522.uid.uidByte, validKey3, 4)){
Serial.println("Tarjeta valida3");
Udp.beginPacket(remoteIP, remotePort);
Udp.write("message");
Serial.println("Mensaje UDP");
Udp.endPacket();
delay(5000);}
else
Serial.println("Tarjeta invalida");
}
}
mfrc522.PICC_HaltA();
}
}
//cierre de lectura y comprobacion
// cierre deteccion tarjeta
delay(250);
}//cierre de void loop
Un abrazo y gracias!!