基于esp32的简易宿舍开门神器
时间:2022-11-29 13:30:00
我相信很多人都有忘记带钥匙的经历。我经常看到一些学生不得不在宿舍门口当门神,因为他们忘记带钥匙。esp32后自然想用这个集成wifi便宜的蓝牙芯片解决了生活中的痛点~
很多人应该做过这个。我见过它有用arduino开发板结合射频模块有用stm32做指纹,甚至做人脸识别。可以说八仙过海,各显神通。
我的计划没有技术(太菜了)~)
开门的具体方案是使用esp32控制一个12V减速电机拉扶手(试试舵机,拉不动~)
一.所需材料:
1.esp32-Wroom开发板(20元左右)
2.12v双路继电器(6元)
3.升压模块(3元)
4.面包板(3元)
5.12v直流减速电机(13元)
6.充电宝(容量越大越好)
(杜邦线、电烙铁等常用工具)
二.原理
一开始我想用esp32跑个web开门和局域网页RC522刷卡开门,但功耗稳定性感~
于是把wifi改用蓝牙,发现传统蓝牙功耗感人~
改用BLE低功耗蓝牙发现功耗依然感人,于是阉割了刷卡开门功能~
esp32作为主控,通信方式为BLE低功耗蓝牙,手机端连接到服务端,并将信息发送到服务端服务端接到信息后执行开门代码,控制电机开门。
需要注意的是,我在这里使用它充电宝供电,所以尽可能降低功耗来增加续航。
关于电机控制,我试过用L298n控制失败(理论上是可以的,不知道为什么),然后用双路继电器控制正反转原理如下图所示
关于电机我采用的是25GA370直流减速电机(12v60转/分钟),淘宝上搜索就能看到。扭矩足够了,但缺点是转得太慢。~电机长这样~
三.代码:开发环境是arduino
[code] int Buzzer = 5; //GPIO5蜂鸣器,提示音 long int ww = 0; /**********************舵机***************************************************/ void servo(){ digitalWrite(2,HIGH); delay(12500); digitalWrite(2,LOW); digitalWrite(Buzzer, 1); delay(1000); digitalWrite(Buzzer, 0); delay(2000); digitalWrite(4,HIGH); delay(8000); digitalWrite(4,LOW); } /***********************蓝牙配置********************/ #include #include #include #include #include #define SERVICE_UUID "6e400001-b5a3-f393-e0a9-e50e24dcca9e" #define CHARACTERISTIC_UUID_RX "6e400002-b5a3-f393-e0a9-e50e24dcca9e" #define CHARACTERISTIC_UUID_TX "6e400003-b5a3-f393-e0a9-e50e24dcca9e" bool deviceConnected = false; BLECharacteristic *pCharact_TX; class ServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; Serial.println("连线中断"); BLEDevice::startAdvertising(); // 重新发布广告 } }; class RXCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharact) { std::string rxVal = pCharact->getValue(); Serial.printf("收到输入值:%s\n", rxVal.c_str()); if (rxVal == "on") { Serial.println("开门"); servo(); } else if (rxVal == "off") { Serial.println("重启"); ESP.restart(); } } }; class TXCallbacks: public BLECharacteristicCallbacks { void onStatus(BLECharacteristic *pCharact, Status s, uint32_t code) { Serial.printf("状态码:%d\n", s); Serial.printf("code:%d\n", code); } }; void printDeviceAddress() { const uint8_t* point = esp_bt_dev_get_address(); for (int i = 0; i < 6; i ) { char str[3]; sprintf(str, "X", (int)point[i]); Serial.print(str); if (i < 5){ Serial.print(":"); } } } void setup() { // put your setup code here, to run once: /******************串口初始化*******************/ Serial.begin(9600); /*********************蓝牙初始化************************/ BLEDevice::init("ESP32蓝牙LED开关"); BLEServer *pServer = BLEDevice::createServer(); pServer->setCallbacks(new ServerCallbacks()); // 建立服务 BLEService *pService = pServer->createService(SERVICE_UUID); // 建立特徵 pCharact_TX = pService->createCharacteristic( CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_READ ); pCharact_TX->addDescriptor(new BLE2902()); pCharact_TX->setCallbacks(new TXCallbacks()); BLECharacteristic *pCharact_RX = pService->createCharacteristic( CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE ); pCharact_RX->setCallbacks(new RXCallbacks()); BLEDescriptor *pDesc = new BLEDescriptor((uint16_t)0x2901); pDesc->setValue("控制板内建LED的开关"); pCharact_RX->addDescriptor(pDesc); // 启动服务 pService->start(); // 开始广播 pServer->getAdvertising()->start(); Serial.println("等待用户端连接…"); printDeviceAddress(); /*****************引脚初始化***********************************************/ pinMode(Buzzer, OUTPUT); pinMode(2,OUTPUT);////这引脚高电平正转 pinMode(4,OUTPUT)//此引脚高电平反转S
}
/********************************蜂鸣器音效***********************************/
void Di(int a)
{
for (int i = 0; i < a; i++)
{
digitalWrite(Buzzer, 1);
delay(250);
digitalWrite(Buzzer, 0);
delay(50);
}
}
void loop() {
// put your main code here, to run repeatedly:
/*******************************定时重启********************************/
ww = millis(); //给开机时间赋值
if (ww >= 240*1000)//定时重启函数
{
ESP.restart();
return;
}
/*******************蓝牙开门**************************/
if (deviceConnected) {
int hallVal = hallRead();
char buffer[5];
itoa(hallVal, buffer, 10);
pCharact_TX->setValue(buffer);
pCharact_TX->notify();
Serial.printf("送出: %d\n", hallVal);
}
delay(500);
}
[/code]
四.演示:
手机端软件可以下载nRF Connect(ios和安卓都有)控制
宿舍开门神器
我的室友再也不用担心我没带钥匙了~