Hey.
I am working on a project where i would like to send the UID of a magnetcard via UDP to a project on my PC. The Connection itselfs works. I am able to send a string from my Arduino to my Software. But if I want to read out the UID and send it to the software, it doesnt work. Could anybody help me how to use the UDP and the RC522 parallel in one Sketch?
I am working with the exmplesketch (SendandReceiveString).
#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[] = "UID should be in here afterwards.";
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);
}