当前位置:网站首页>ESP8266 RC522
ESP8266 RC522
2022-07-01 00:37:00 【Mrli0530】
一、读取卡片信息
#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();
}
三、接线
边栏推荐
- None of the following candidates is applicable because of a receiver type mismatch
- Vnctf 2022 cm CM1 re reproduction
- Web interface testing of software testing
- [daily record] - bug encountered in BigDecimal division operation
- IBL预计算的疑问终于解开了
- 深度学习的历史
- 20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
- leetcode 474. Ones and zeroes (medium)
- Plot size and resolution with R markdown, knitr, pandoc, beamer
- Length of the longest integrable subarray
猜你喜欢

left join左连接匹配数据为NULL时显示指定值

The principle of journal node

Packing and unpacking of C #

PyTorch安装并使用gpu加速

C language file operation for conquering C language

C # generates PPK files in putty format (supports passphrase)

HDU 2488 A Knight's Journey(DFS)

Sword finger offer 19 Regular Expression Matching

SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding

Date类的实现
随机推荐
Basic knowledge of Embedded Network - introduction of mqtt
Wechat official account development (1) introduction to wechat official account
Using C language to realize the exchange between the contents of two arrays (provided that the array is the same size)
合适的工作就是好工作
The principle and related problems of acid in MySQL
None of the following candidates is applicable because of a receiver type mismatch
The real topic of the 11th provincial competition of Bluebridge cup 2020 - crop hybridization
Metauniverse and virtual reality (II)
P4 learning - p4runtime
CTF tool (1) -- archpr -- including installation / use process
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
【日常记录】——对BigDecimal除法运算时遇到的Bug
C language file operation for conquering C language
【2023联发科提前批笔试题】~ 题目及参考答案
What is product thinking
leetcode 474. Ones and Zeroes 一和零(中等)
Oracle-表的创建与管理
Two-stage RO: part 1
The communication mechanism and extension of Supervisor
Gavin's insight on the transformer live broadcast course - rasa project's actual banking financial BOT Intelligent Business Dialogue robot system startup, language understanding, dialogue decision-mak