当前位置:网站首页>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();
}
三、接线
边栏推荐
- Shift operators
- 剑指 Offer 19. 正则表达式匹配
- Self examination before school starts
- A single element in an ordered array
- Double linked list: initialize insert delete traversal
- CTF tool (1) -- archpr -- including installation / use process
- MySQL variables, stored procedures and functions
- CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
- 合适的工作就是好工作
- Sword finger offer 19 Regular Expression Matching
猜你喜欢
![[daily record] - bug encountered in BigDecimal division operation](/img/82/3105586841076b9bb1c57eac221ddf.png)
[daily record] - bug encountered in BigDecimal division operation

Vulnerability discovery - App application vulnerability probe type utilization and repair

优质的水泵 SolidWorks模型素材推荐,不容错过

20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking

5. TPM module initialization

Set different background colors for the border and text of the button

Date类的实现

Deployment of mini version message queue based on redis6.0

初识 Flutter 的绘图组件 — CustomPaint

Koa koa-combine-routers 分路由管理
随机推荐
Solving the weird problem that the query conditions affect the value of query fields in MySQL query
Usage of C set
New content violation degree determination scana bad information monitoring capability update issue 5
2022 is half way through. It's hard to make money
Using C language to realize the exchange between the contents of two arrays (provided that the array is the same size)
Some views on libco
什么是产品思维
[LeetCode] 爬楼梯【70】
Pytorch auto derivation
Line number of Jenkins pipeline script execution exception
解决 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
关于Unity一般的输入操作方式
Using asyncio for concurrency
Two-stage RO: part 1
Sword finger offer 18 Delete the node of the linked list
Shift operators
1009 product of polynomials (25 points) [PTA class A]
Exercises on recursion in C language
Is the public read-only field with immutable structure valid- Does using public readonly fields for immutable structs work?
Implementation of date class