On Sat, Apr 29, 2023 at 05:05 PM, <stephenjohnson500@...> wrote:
MFRC522, Uno, Ethernet and Serial
OK, so thanks for confirming the correct, working sketch for me. Now, I know that you wanted the same debugging functionality as in the Serial PN532 sketch, where the serial monitor tell you that reader X is connected to pin Y and the LED blinking. The serial monitor will display the information about the detected readers, such as the reader ID and the SS pin it's connected to. The built-in LED will also blink to match the number of readers detected during the startup process.
?
The LED will blink with a 1-second delay before the first blink, and a quarter-second delay between turning on and off for each blink. There will be a 1-second delay between each blink, making it easy to count the number of blinks and compare it with the number of detected readers shown on the serial monitor.
// 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);
unsigned int serverPort = 8888;? // Replace with the desired port number
EthernetServer server(serverPort);
?
// 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, 7, A0, A1, A2};
const uint8_t rstPins[] = {4, 8, 9, A3, A4, A5};
const uint8_t readerAssignment[] = {1, 2, 3, 4, 5, 6}; // Assign reader numbers based on SS and RST pins
#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};
const uint8_t readerAssignment[] = {1, 2, 3, 4, 5, 6, 7, 8}; // Assign reader numbers based on SS and RST pins
#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];
EthernetClient client;
?
void setup() {
? Serial.begin(9600);
? SPI.begin();
? pinMode(10, OUTPUT);
? digitalWrite(10, HIGH);
?
? // 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);
?
? // Initialize the Ethernet connection
? Ethernet.begin(mac, ip);
? delay(1000); // Allow the Ethernet shield to initialize
?
? // Set the built-in LED as an output
? pinMode(LED_BUILTIN, OUTPUT);
?
? for (uint8_t i = 0; i < numReaders; i++) {
? ? readers[i].ssPin = ssPins[i];
? ? readers[i].rstPin = rstPins[i];
? ? readers[i].id = readerID[readerAssignment[i] - 1]; // Assign the reader ID based on readerAssignment array
? ? 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);
?
? ? // Print debugging information
? ? Serial.print("Reader ");
? ? Serial.print(readers[i].id);
? ? Serial.print(" detected on SS pin ");
? ? Serial.println(readers[i].ssPin);
?
? ? // Blink the built-in LED
? ? delay(1000); // wait for a second
? ? digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
? ? delay(250); // wait for a quarter second
? ? digitalWrite(LED_BUILTIN, LOW); // turn the LED off
? ? delay(250); // wait for a quarter second
? }
?
? server.begin();
}
?
void loop() {
? if (!client.connected()) {
? ? client.stop();
? ? client = server.accept();
? ? if (client) {
? ? ? Serial.println("Client connected");
? ? }
? }
?
? if (client.connected()) {
? ? 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();
? ? ? }
? ? }
? }
}
Tom