一、读取卡片信息
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 5 // 配置针脚
#define SS_PIN 4
MFRC522 mfrc522(SS_PIN, RST_PIN); // 创建新的RFID实例
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600); // 设置串口波特率为9600
SPI.begin(); // SPI开始
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("test-demo-start");
}
void loop() {
// 寻找新卡
if ( ! mfrc522.PICC_IsNewCardPresent()) {
//Serial.println("没有找到卡");
return;
}
// 选择一张卡
if ( ! mfrc522.PICC_ReadCardSerial()) {
Serial.println("没有卡可选");
return;
}
// 显示卡片的详细信息
Serial.print(F("卡片 UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("卡片类型: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
// 检查兼容性
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("仅仅适合Mifare Classic卡的读写"));
return;
}
MFRC522::StatusCode status;
if (status != MFRC522::STATUS_OK) {
Serial.print(F("身份验证失败?或者是卡链接失败"));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//停止 PICC
mfrc522.PICC_HaltA();
//停止加密PCD
mfrc522.PCD_StopCrypto1();
return;
}
/**
将字节数组转储为串行的十六进制值
*/
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);
}
}
二、完整程序
//RC522
#define BLINKER_WIFI
#include <Blinker.h>
#include <SPI.h>
#include <String.h>
#include <MFRC522.h>
#define SS_PIN 4
#define RST_PIN 5
MFRC522 rfid(SS_PIN, RST_PIN);
byte nuidPICC[4];
int a_1=1;
//初始化
char auth[] = "eb6b127b98b2";
char ssid[] = "ESP8266";
char pswd[] = "12345678";
// 新建组件对象
BlinkerButton Button1("btn-abc");
// 按下按键即会执行该函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
Serial.print("S");
}
void setup()
{
Serial.begin(115200);
//SPI
SPI.begin();
rfid.PCD_Init();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback);
//设置引脚
}
//hex
void printHex(byte *buffer, byte bufferSize)
{
for (byte i = 0; i < bufferSize; i++)
{
//Serial.print(buffer[i] < 0x10 ? " 0" : "");
//Serial.print(buffer[i], HEX);
}
}
//==================开门用================//
void printDec(byte *buffer, byte bufferSize)
{
for (byte i = 0; i < bufferSize; i++) {
//Serial.print(buffer[i] < 0x10 ? " 0" : "");
//Serial.print(buffer[i], DEC);
}
//人员1
if(nuidPICC[0]==0xAA&& nuidPICC[1]==0xFD && nuidPICC[2]==0xA9 && nuidPICC[3]==0x26)// 卡号ID
{
Serial.print("1");
a_1=1;
//这里要开门
}
//人员2
else if (nuidPICC[0]==0xAA&& nuidPICC[1]==0xF9 && nuidPICC[2]==0xD9 && nuidPICC[3]==0x26)
{
Serial.print("2");
a_1=1;
//这里要开门
}
//人员3
else if (nuidPICC[0]==0xAA&& nuidPICC[1]==0xE1 && nuidPICC[2]==0x11 && nuidPICC[3]==0x26)
{
Serial.print("3");
a_1=1;
//这里要开门
}
//人员4
else if (nuidPICC[0]==0xAA&& nuidPICC[1]==0x99 && nuidPICC[2]==0x3F && nuidPICC[3]==0xAD)
{ Serial.print("4");
a_1=1;
//这里要开门
}
//人员5
else if (nuidPICC[0]==0x25&& nuidPICC[1]==0x5C && nuidPICC[2]==0x42 && nuidPICC[3]==0x25)
{
Serial.print("5");
a_1=1;
//这里要开门
}
//人员6
else if (nuidPICC[0]==0x25&& nuidPICC[1]==0x5C && nuidPICC[2]==0x42 && nuidPICC[3]==0x25)
{
Serial.print("6");
a_1=1;
//这里要开门
}
//人员7
else if (nuidPICC[0]==0x35&& nuidPICC[1]==0xB3 && nuidPICC[2]==0x70 && nuidPICC[3]==0x50)
{
Serial.print("7");
a_1=1;
//这里要开门
}
//人员8
else if (nuidPICC[0]==0x39&& nuidPICC[1]==0x8E && nuidPICC[2]==0x3B && nuidPICC[3]==0x10)
{
Serial.print("8");
a_1=1;
//这里要开门
}
//人员9
else if (nuidPICC[0]==0x25&& nuidPICC[1]==0xBA && nuidPICC[2]==0x3C && nuidPICC[3]==0x50)
{
Serial.print("9");
a_1=1;
//这里要开门
}
//人员10
else if (nuidPICC[0]==0x35&& nuidPICC[1]==0x65 && nuidPICC[2]==0x95 && nuidPICC[3]==0x50)
{
Serial.print("0");
a_1=1;
//这里要开门
}
}
//注册+识别
void zhuce(){
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); // 将NUID保存到nuidPICC数组
for (byte i = 0; i < 4; i++)
{
nuidPICC[i] = rfid.uid.uidByte[i];
}
//Serial.print("十六进制UID:");
printHex(rfid.uid.uidByte, rfid.uid.size); //Hex:16进制
//Serial.println();
//
//Serial.print("十进制UID:");
printDec(rfid.uid.uidByte, rfid.uid.size); //Dec:10进制
//Serial.println();
// 使放置在读卡区的IC卡进入休眠状态,不再重复读卡,Halt(停止)
rfid.PICC_HaltA();
// 停止读卡模块编码
rfid.PCD_StopCrypto1();
}
void loop(){
zhuce();
Blinker.run();
}
三、接线