ESP32 MFRC522 RFID
Read RFID Tag’s UID with RC522
目前在台灣市占率最高的 NFC 卡片是 NXP Mifare 系列的卡片 (如:Mifare Ultralight、Mifare Classic、Plus、DESFire),這類的卡片使用 ISO 14443A 的規範,通訊頻率是 13.56 MHz,其中又以 Mifare Classic 1K 最為常見,本文主要針對這種卡片來進行說明。
RFID RC522 Interfacing with ESP32
* MFRC522 ESP32
* Reader/PCD
* Signal Pin Pin
* ----------------------------------
* RST/Reset RST GPIO27
* SPI SS SDA(SS) GPIO5
* SPI MOSI MOSI GPIO23
* SPI MISO MISO GPIO19
* SPI SCK SCK GPIO18
/***
* Typical pin layout used:
* -----------------------------------------
* MFRC522 ESP32
* Reader/PCD
* Signal Pin Pin
* ------------------------------------------
* RST/Reset RST GPIO 27
* SPI SS SDA(SS) GPIO 5
* SPI MOSI MOSI GPIO 23
* SPI MISO MISO GPIO 19
* SPI SCK SCK GPIO 18
*
* ------------------------------------------
***/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 5 // ESP32 pin GIOP5 //MFRC522 SS接腳 與 ESP32 的連接Pin
#define RST_PIN 27 // ESP32 pin GIOP27 //MFRC522 RST接腳 與 ESP32 的連接Pin
#define LED 2 //定義LED接腳
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[4];
//========================================================================
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
//========================================================================
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(' ');
Serial.print(buffer[i], DEC);
}
}
//========================================================================
void setup() {
Serial.begin(115200);
// Setting LED pin as output
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // Turn off the LED initially
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println(F("This code scan the MIFARE Classsic NUID."));
Serial.print(F("Using the following key:"));
printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}
//========================================================================
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
if (rfid.uid.uidByte[0] != nuidPICC[0] ||
rfid.uid.uidByte[1] != nuidPICC[1] ||
rfid.uid.uidByte[2] != nuidPICC[2] ||
rfid.uid.uidByte[3] != nuidPICC[3] ) {
Serial.println(F("A new card has been detected."));
digitalWrite(LED, HIGH);
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
Serial.println(F("The NUID tag is:"));
Serial.print(F("In hex: "));
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
Serial.print(F("In dec: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
}
else Serial.println(F("Card read previously."));
digitalWrite(LED, LOW);
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
//========================================================================
沒有留言:
張貼留言