宿舍门锁2.0
#include <SPI.h>
#include <Servo.h>
#include <SoftwareSerial.h>
//手动安装
#include <MFRC522.h>
#define RST_PIN 5 // 配置针脚
#define SS_PIN 4
#define TOUCH_SIG D8
SoftwareSerial mySerial(D0, D3);
MFRC522 mfrc522(SS_PIN, RST_PIN); // 创建新的RFID实例
MFRC522::MIFARE_Key key;
Servo servo;
unsigned long previousMillis = 0; //毫秒时间记录
int servoPin = 2;//D4 //接舵机
void setup() {
pinMode(TOUCH_SIG, INPUT);
Serial.begin(115200); // 设置串口波特率为9600
delay(100);
mySerial.begin(115200);
delay(100);
SPI.begin(); // SPI开始
mfrc522.PCD_Init(); // Init MFRC522 card
delay(100);
servo.attach(servoPin);
servo.write(0);
delay(200);
servo.detach();
mySerial.print("c");
Serial.println("smatr208-lock-start");
}
void loop()
{
rc522();
}
void rc522() {
// 寻找新卡
if ( ! mfrc522.PICC_IsNewCardPresent()) {
Serial.println("没有找到卡");
return;
}
// 选择一张卡
if ( ! mfrc522.PICC_ReadCardSerial()) {
Serial.println("没有卡可选");
return;
}
// 显示卡片的详细信息
Serial.print("卡片 UID:");
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
}
/*将字节数组转储为串行的十六进制值*/
void dump_byte_array(byte *buffer, byte bufferSize) {
String temphex = "";
for (byte i = 0; i < bufferSize; i++) {
// Serial.print(buffer[i] < 0x10 ? " 0" : " ");
// Serial.print(buffer[i], HEX);
temphex += tohex(buffer[i]);
}
Serial.println(temphex);
delay(100);
check(temphex);
}
//十进制转十六进制
String tohex(int n) {
if (n == 0) {
return "00"; //n为0
}
String result = "";
char _16[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
const int radix = 16;
while (n) {
int i = n % radix; // 余数
result = _16[i] + result; // 将余数对应的十六进制数字加入结果
n /= radix; // 除以16获得商,最为下一轮的被除数
}
if (result.length() < 2) {
result = '0' + result; //不足两位补零
}
return result;
}
//获取状态
boolean get_touch() {
boolean touch_stat = 0;
touch_stat = digitalRead(TOUCH_SIG); //读入状态
return touch_stat;
}
void openlock()
{
mySerial.print("a");
delay(50);
// Serial.println("openlock");
servo.attach(servoPin);
servo.write(100);//、、、、、、、、、开门角度100度
delay(2000);//延时
servo.write(0);//、、、、、、、、、、、、、回0度
delay(1000);
servo.detach();
mySerial.print("b");
}
void check(String temphex)
{
// Serial.println("check");
if (temphex == "501Dxxxx")
{
// Serial.println(temphex);
openlock();
}
else if (temphex == "501Dxxxx")
{
// Serial.println(temphex);
openlock();
}
else if (temphex == "501Dxxxx")
{
// Serial.println(temphex);
openlock();
}
else if (temphex == "501Dxxxx")
{
// Serial.println(temphex);
openlock();
}
else if (temphex == "501Dxxxx")
{
// Serial.println(temphex);公交卡
openlock();
}
else if (temphex == "501Dxxxx")
{
// Serial.println(temphex);公交
openlock();
}
else
{
mySerial.print("d");
}
}
代码编译好后,烧录到ESP266接好RC522模块,打开串口,刷卡可出现卡号,卡号填到代码后面部分,再次烧录