资讯详情

来看看单片机学生有多卷,基于esp32的简易宿舍开门神器

我们来看看单片机学生的多卷。esp32简单宿舍开门神器

0ba2c091efa10779bbc726a06dd46887.png

我相信很多人都有忘记带钥匙的经历。我经常看到一些学生不得不在宿舍门口当门神,因为他们忘记带钥匙。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; //GPIO蜂鸣器,提示音

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(); ///重新发布广告

 &nbs;  }

};

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();

&nbsp;&nbsp;for (int i = 0; i < 6; i++) {

&nbsp;&nbsp;&nbsp;&nbsp;char str[3];

&nbsp;&nbsp;&nbsp;&nbsp;sprintf(str, "%02X", (int)point[i]);

&nbsp;&nbsp;&nbsp;&nbsp;Serial.print(str);

&nbsp;&nbsp;&nbsp;&nbsp;if (i < 5){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Serial.print(":");

&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;}

}

void setup() {

&nbsp;&nbsp;// put your setup code here, to run once:

/******************串口初始化*******************/

Serial.begin(9600);

/*********************蓝牙初始化************************/

&nbsp;&nbsp;BLEDevice::init("ESP32藍牙LED開關");

&nbsp;&nbsp;BLEServer *pServer = BLEDevice::createServer();

&nbsp;&nbsp;pServer->setCallbacks(new ServerCallbacks());

&nbsp;&nbsp;//建立服務

&nbsp;&nbsp;BLEService *pService = pServer->createService(SERVICE_UUID);

&nbsp;&nbsp;//建立特徵

&nbsp;&nbsp;pCharact_TX = pService->createCharacteristic(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CHARACTERISTIC_UUID_TX,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BLECharacteristic::PROPERTY_NOTIFY |

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BLECharacteristic::PROPERTY_READ

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);

&nbsp;&nbsp;pCharact_TX->addDescriptor(new BLE2902());

&nbsp;&nbsp;pCharact_TX->setCallbacks(new TXCallbacks());

&nbsp;&nbsp;BLECharacteristic *pCharact_RX = pService->createCharacteristic(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CHARACTERISTIC_UUID_RX,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BLECharacteristic::PROPERTY_WRITE

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);

&nbsp;&nbsp;pCharact_RX->setCallbacks(new RXCallbacks());

&nbsp;&nbsp;BLEDescriptor *pDesc = new BLEDescriptor((uint16_t)0x2901);

&nbsp;&nbsp;pDesc->setValue("控制板內建LED的開關");

&nbsp;&nbsp;pCharact_RX->addDescriptor(pDesc);

&nbsp;&nbsp;//啟動服務

&nbsp;&nbsp;pService->start();

&nbsp;&nbsp;//開始廣播

&nbsp;&nbsp;pServer->getAdvertising()->start();

&nbsp;&nbsp;Serial.println("等待用戶端連線…");

&nbsp;&nbsp;printDeviceAddress();

&nbsp;&nbsp;/*****************引脚初始化***********************************************/

&nbsp;&nbsp;pinMode(Buzzer, OUTPUT);

&nbsp;&nbsp;pinMode(2,OUTPUT);//此引脚高电平正转

&nbsp;&nbsp;pinMode(4,OUTPUT);//此引脚高电平反转S

}

/********************************蜂鸣器音效***********************************/

void Di(int a)

{

&nbsp;&nbsp;for (int i = 0; i < a; i++)

&nbsp;&nbsp;{

&nbsp;&nbsp;&nbsp;&nbsp;digitalWrite(Buzzer, 1);

&nbsp;&nbsp;&nbsp;&nbsp;delay(250);

&nbsp;&nbsp;&nbsp;&nbsp;digitalWrite(Buzzer, 0);

&nbsp;&nbsp;&nbsp;&nbsp;delay(50);

&nbsp;&nbsp;}

}

void loop() {

&nbsp;&nbsp;// put your main code here, to run repeatedly:

&nbsp;&nbsp;/*******************************定时重启********************************/

&nbsp;&nbsp;&nbsp;ww = millis(); //给开机时间赋值

&nbsp;&nbsp;if (ww >= 240*1000)//定时重启函数

&nbsp;&nbsp;{

&nbsp;&nbsp;&nbsp;&nbsp;ESP.restart();

&nbsp;&nbsp;&nbsp;&nbsp;return;

&nbsp;&nbsp;}

&nbsp;&nbsp;/*******************蓝牙开门**************************/

&nbsp;&nbsp;&nbsp;if (deviceConnected) {

&nbsp;&nbsp;&nbsp;&nbsp;int hallVal = hallRead();

&nbsp;&nbsp;&nbsp;&nbsp;char buffer[5];

&nbsp;&nbsp;&nbsp;&nbsp;itoa(hallVal, buffer, 10);

&nbsp;&nbsp;&nbsp;&nbsp;pCharact_TX->setValue(buffer);

&nbsp;&nbsp;&nbsp;&nbsp;pCharact_TX->notify();

&nbsp;&nbsp;&nbsp;&nbsp;Serial.printf("送出:&nbsp;%d\n", hallVal);

&nbsp;&nbsp;}

&nbsp;&nbsp;delay(500);

}

[/code]

BLE低功耗蓝牙代码比传统蓝牙要复杂太多了~

我的室友再也不用担心我没带钥匙了~

///插播一条:我自己在今年年初录制了一套还比较系统的入门单片机教程,想要的同学找我拿就行了免費的,私信我就可以哦~点我头像黑色字体加我也能领取哦。最近比较闲,带做毕设,带学生参加省级或以上比赛///

标签: esp8266双路继电器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台