“实现门禁卡读取:ESP8266与RFID RC522设备的对接”
文章目录
前言
在本篇文章中,您将 使用ESP8266,连接到RFID RC522 设备上,读取门禁卡上的设备信息。本文提供了一个实用示例,以帮助您更好地学习它。
一、所需材料
二、电路接线图和实物图
实物图
接线图:
三 RC522 基础知识普及
想要驱动RC522模块对IC卡(这里用的是M1卡型号是S50)进行读写操作,一定要有以下5个步骤:
一、寻卡
二、防冲突
三、选择卡
四、验证扇区密码(每个扇区都有密匙A和密匙B,验证正确才能对该扇区的某块进行读写)
五、读/写
硬件方面与单片机I/O口连好,使用SPI通信的时候NSS就是SDA引脚,IRQ悬空。
四、 RFID库安装

搜索并下载安装MFRC522库文件

五、 Arduino代码解析
代码如下
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 6
#define SS_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
String carid="";
/**
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}
/**
* Main loop.
*/
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
//Serial.println(mfrc522.PICC_GetTypeName(piccType));
Serial.println();
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
六、 解析过程实物展示
刷卡信息展示
连接成功之后,就可以看到不停的有设备的数据信息读取出来。
Card UID
Card SAK
PICC type: MIFARE 1KB
Sector Block
stack
ctx: cont
七、总结
以上就是今天要讲的内容,本文仅仅简单介绍 RFID的读取,后续讲介绍16个扇区的写入功能,arduino 现在已经非常的成熟了,是一个非常成熟的解决方案了。
并且rfid 也挺成熟的了,可以购买个 rc522 自己研究下,主要是接线多点。
用到了 spi 和 两个 gio的针脚。
*玩转esp8266 ESP32 喜欢的朋友请加入我微信 ilinker32 有偿技术咨询 *
如果我的创作对你有帮助 ,那么你的鼓励将是我创作的最大动力!非常感谢。