Hallo liebe Community!
Ich versuche den UID-Tag einer Magnetkarte einzulesen und dann via UDP an meine Software zu schicken. Die UDP Verbindung funktioniert, jedoch kann ich keine Karte einlesen. Finde im Internet leider nichts dazu.
Ich arbeite mit dem SendandRecieve Beispielsketch.
Hoffe es kann mir jemand helfen.
Mein Sketch:
#include <MFRC522.h>
#include<SPI.h>
constexpr uint8_t RST_PIN = 5;
constexpr uint8_t SS_PIN = 53;
MFRC522 mfrc522(SS_PIN, RST_PIN);
//UDP
#include <Ethernet2.h>
#include <EthernetUdp2.h>
EthernetUDP Udp;
byte mac[] = {0x90, 0xA2, 0xDA, 0x11, 0x2E, 0x46};
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char ReplyBuffer[] = "Hier sollte die UID stehen dann.";
void setup() {
// put your setup code here, to run once:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(9600);
}
void loop() {
String content;
// if there's data available, read a packet
int 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);
//------------------------------------------------------------------------
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//----------------------------------------------------------------------
for (byte i = 0; i < mfrc522.uid.size; i++) {
content.concat(String(mfrc522.uid.uidByte[i] <0x10 ? "0" : " "));
content.concat(String(mfrc522.uid.uidByte[i],HEX));
}
content.toUpperCase();
String UID= content.substring(1);
Serial.print(UID);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}