【宿舍门锁】

该博客介绍了一个使用ESP8266、MFRC522 RFID模块和伺服电机实现的宿舍智能门锁系统。通过读取RFID卡号来验证权限,并根据预设的卡号执行开门操作。系统能通过串口输出卡号信息,具备一定的安全性和便利性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

宿舍门锁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模块,打开串口,刷卡可出现卡号,卡号填到代码后面部分,再次烧录
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值