当前位置:网站首页>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();
}
三、接线
边栏推荐
- Unit test concept and purpose
- Oracle-数据完整性
- 20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
- Using C language to realize the exchange between the contents of two arrays (provided that the array is the same size)
- 20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
- How to specify the number of cycles in JSTL- How to loop over something a specified number of times in JSTL?
- Redis based distributed lock
- CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
- 什么是产品思维
- Self examination before school starts
猜你喜欢

Get to know the drawing component of flutter - custompaint

NE555波形发生器手把手教程之NE555内部结构(一)

From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years

Can JDBC based on openjdk connect to MySQL?

Vulnerability discovery - App application vulnerability probe type utilization and repair

Pytorch auto derivation

Deployment of mini version message queue based on redis6.0

The communication mechanism and extension of Supervisor

HDU 2488 A Knight's Journey(DFS)

Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support
随机推荐
PyTorch安装并使用gpu加速
The real topic of the 11th provincial competition of Bluebridge cup 2020 - crop hybridization
深度学习的历史
Solving the weird problem that the query conditions affect the value of query fields in MySQL query
Mindjet mindmanager2022 mind map decompression installer tutorial
Date类的实现
对libco的一点看法
Day31-t1380-2022-02-15-not answer by yourself
20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
How to specify the number of cycles in JSTL- How to loop over something a specified number of times in JSTL?
Basic data structure of redis
Longest valid bracket
获取屏幕高度
Using asyncio for concurrency
第53章 从业务逻辑实现角度整体性理解程序
Oracle data integrity
Introduction to ES6 promise, new features of ES7 and es8 async and await
CTF tool (1) -- archpr -- including installation / use process
Vulnerability discovery - App application vulnerability probe type utilization and repair
[untitled]