// Import Libraries
#include <SPI.h>? ? ? ? ? ?// SPI library for communicating with the MFRC522 reader
#include <MFRC522.h>? ? ? ?// MFRC522 library for reading RFID cards
#include <Ethernet.h>? ? ? // Ethernet library for the Ethernet shield
?
// Ethernet configuration
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 177);? ?// Change as per your network requirements
EthernetServer server(80);? ? ? ? // Change as per your network requirements
?
// Define the SS (Slave Select) and RST (Reset) pins for each reader
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO)
const uint8_t ssPins[] = {5, 6, 8, 10, A2, A4};
const uint8_t rstPins[] = {4, 7, 9, A1, A3, A5};
#elif defined(ARDUINO_AVR_MEGA2560)
const uint8_t ssPins[] = {5, 6, 7, 8, 9, A0, A1, A2};
const uint8_t rstPins[] = {22, 23, 24, 25, 26, 27, 28, 29};
#endif
?
const int numReaders = sizeof(ssPins) / sizeof(ssPins[0]);
const char readerID[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
?
struct RFIDReader {
? char id;
? uint8_t ssPin;
? uint8_t rstPin;
? MFRC522 mfrc522;
? byte nuid[7];
?
? RFIDReader() : id(0), ssPin(0), rstPin(0), mfrc522(MFRC522(0, 0)) {}
};
?
RFIDReader readers[numReaders];
?
void setup() {
? Serial.begin(9600);
? SPI.begin();
? Ethernet.begin(mac, ip);
? server.begin();
?
? // Set pin 53 as OUTPUT for the Arduino Mega 2560
? #if defined(ARDUINO_AVR_MEGA2560)
? ? pinMode(53, OUTPUT);
? #endif
?
? // Explicitly deselect the SD card if not in use
? pinMode(4, OUTPUT);
? digitalWrite(4, HIGH);
?
? for (uint8_t i = 0; i < numReaders; i++) {
? ? readers[i].ssPin = ssPins[i];
? ? readers[i].rstPin = rstPins[i];
? ? readers[i].id = readerID[i];
? ? readers[i].mfrc522 = MFRC522(readers[i].ssPin, readers[i].rstPin);
? ? readers[i].mfrc522.PCD_Init();
? ? readers[i].mfrc522.PCD_SetAntennaGain(readers[i].mfrc522.RxGain_max);
? }
}
?
void loop() {
? EthernetClient client = server.available();
?
? if (client) {
? ? for (uint8_t i = 0; i < numReaders; i++) {
? ? ? if (readers[i].mfrc522.PICC_IsNewCardPresent() && readers[i].mfrc522.PICC_ReadCardSerial()) {
? ? ? ? for (uint8_t j = 0; j < readers[i].mfrc522.uid.size; j++) {
? ? ? ? ? readers[i].nuid[j] = readers[i].mfrc522.uid.uidByte[j];
? ? ? ? }
?
? ? ? ? byte checksum = readers[i].nuid[0];
? ? ? ? for (uint8_t j = 1; j < 5; j++) {
? ? ? ? ? checksum ^= readers[i].nuid[j];
? ? ? ? }
?
? ? ? ? // Send output to Serial connection
? ? ? ? Serial.write(readers[i].id);
?
? ? ? ? // Send output to Ethernet client
? ? ? ? client.write(readers[i].id);
?
? ? ? ? for (uint8_t j = 0; j < 5; j++) {
? ? ? ? ? // Send output to Serial connection
? ? ? ? ? Serial.print(readers[i].nuid[j] < 0x10 ? "0" : "");
? ? ? ? ? Serial.print(readers[i].nuid[j], HEX);
?
? ? ? ? ? // Send output to Ethernet client
? ? ? ? ? client.print(readers[i].nuid[j] < 0x10 ? "0" : "");
? ? ? ? ? client.print(readers[i].nuid[j], HEX);
? ? ? ? }
?
? ? ? ? // Send output to Serial connection
? ? ? ? Serial.print(checksum < 0x10 ? "0" : "");
? ? ? ? Serial.print(checksum, HEX);
?
? ? ? ? // Send output to Ethernet client
? ? ? ? client.print(checksum < 0x10 ? "0" : "");
? ? ? ? client.print(checksum, HEX);
?
? ? ? ? // Send output to Serial connection
? ? ? ? Serial.write(0x0D);
? ? ? ? Serial.write(0x0A);
? ? ? ? Serial.write('>');
?
? ? ? ? // Send output to Ethernet client
? ? ? ? client.write(0x0D); // CR
? ? ? ? client.write(0x0A); // LF
? ? ? ? client.write('>');? // ETX replaced by '>'
?
? ? ? ? readers[i].mfrc522.PICC_HaltA();
? ? ? ? readers[i].mfrc522.PCD_StopCrypto1();
? ? ? }
? ? }
? }
}