当前位置:网站首页>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();
}
三、接线
边栏推荐
- What if the disk of datanode is full?
- How to do the performance pressure test of "Health Code"
- 什么是产品思维
- C#生成putty格式的ppk文件(支持passphrase)
- Unit test concept and purpose
- Docsify building a personal minimalist knowledge warehouse
- The question of IBL precomputation is finally solved
- Two-stage RO: part 1
- 20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
- What should I do without 50W bride price
猜你喜欢
Golang treasure house recommendation
PHP online confusion encryption tutorial sharing + basically no solution
Two-stage RO: part 1
Bugku CTF daily one question dark cloud invitation code
Docsify building a personal minimalist knowledge warehouse
HDU 2488 A Knight's Journey(DFS)
P4学习——p4runtime
PyTorch安装并使用gpu加速
Two-stage RO: part 1
Exercises on recursion in C language
随机推荐
第53章 从业务逻辑实现角度整体性理解程序
Sword finger offer 19 Regular Expression Matching
20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
Golang treasure house recommendation
Share your own terminal DIY display banner
Oracle-表的创建与管理
[2023 MediaTek approved the test questions in advance] ~ questions and reference answers
Left join displays the specified value when the left join matching data is null
Exercises on recursion in C language
TCP三次握手为什么不是两次或四次
Wechat official account development (1) introduction to wechat official account
Vnctf 2022 cm CM1 re reproduction
【2023联发科提前批笔试题】~ 题目及参考答案
Service
【日常记录】——对BigDecimal除法运算时遇到的Bug
Exercise and health
The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
Koa koa-combine-routers 分路由管理
Using asyncio for concurrency
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude