开云体育

Locked Re: RFID Connectivity #rfid


 

MFRC522, Ethernet (TCP/IP), Nano/Uno/Mega

The following sketch should work with the Nano, Uno, and Mega boards with an Ethernet Shield attached. Note the the Nano and Uno boards can only support 6 connected MFRC522 readers so as to not conflict with the pins used for the Ethernet Shield and without running out of available pins. The Mega can support all 8 connected readers. For debugging purposes,?the output will be sent to both the Serial connection and the Ethernet client. Also, the ReaderID (A, B, C, D, E, etc.) is determined by the SS pin being used and not simply the number of readers.

Arduino Nano and Uno:
?
Reader A: SS Pin 5, RST Pin 4
Reader B: SS Pin 6, RST Pin 7
Reader C: SS Pin 8, RST Pin 9
Reader D: SS Pin 10, RST Pin A1
Reader E: SS Pin A2, RST Pin A3
Reader F: SS Pin A4, RST Pin A5
Arduino Mega 2560:
?
Reader A: SS Pin 5, RST Pin 22
Reader B: SS Pin 6, RST Pin 23
Reader C: SS Pin 7, RST Pin 24
Reader D: SS Pin 8, RST Pin 25
Reader E: SS Pin 9, RST Pin 26
Reader F: SS Pin A0, RST Pin 27
Reader G: SS Pin A1, RST Pin 28
Reader H: SS Pin A2, RST Pin 29

All of these pin assignments are compatible with the Ethernet shield, as they do not conflict with the shield's pins. The Ethernet shield uses the following pins:
?
Arduino Uno/Nano: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK)
Arduino Mega 2560: 10 (SS), 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS for SD card)

All of these pins are available on the Nano, Uno, and Mega boards for use.

// 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();
? ? ? }
? ? }
? }
}


Tom

Join [email protected] to automatically receive all group messages.