基于ESP32的BLE蓝牙开关
生活随笔
收集整理的這篇文章主要介紹了
基于ESP32的BLE蓝牙开关
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
構思:
藍牙無線插座的硬件設備為:ESP32-WROOM-32、OLED(Organic Light-Emitting Diode)顯示屏模塊、繼電器、電源轉(zhuǎn)換器、插頭部件、插座部件。具體結構如下圖所示。
硬件電路的基本工作流程如下:接通電源,模塊將220V的AC(Alternating current)電流轉(zhuǎn)換成5V的DC(Direct Current)電流,整流電路將5V轉(zhuǎn)為3.3V,ESP32就可以獲得穩(wěn)定的電流并正常工作,用戶通過使用手機打開藍牙,啟動APP,手機與該插座建立配對藍牙,發(fā)送開啟指令后,繼電器模塊控制插座的開關,同時OLED屏幕開始運作并顯示插座運作的時間,并將數(shù)據(jù)通過藍牙發(fā)送給手機APP端。
標題
Arduino代碼
#include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h>#include <Wire.h> // 引入驅(qū)動OLED0.91所需的庫 #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>#define SCREEN_WIDTH 128 // 設置OLED寬度,單位:像素 #define SCREEN_HEIGHT 32 // 設置OLED高度,單位:像素// 自定義重置引腳,雖然教程未使用,但卻是Adafruit_SSD1306庫文件所必需的 #define OLED_RESET 4 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);BLEServer *pServer = NULL; BLECharacteristic * pTxCharacteristic; bool deviceConnected = false; bool oldDeviceConnected = false;bool deviceswitch = false;uint8_t txValue = 0;uint8_t runtime = 0;// See the following for generating UUIDs: // https://www.uuidgenerator.net/#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"class MyServerCallbacks: public BLEServerCallbacks {void onConnect(BLEServer* pServer) {deviceConnected = true;};void onDisconnect(BLEServer* pServer) {deviceConnected = false;} };class MyCallbacks: public BLECharacteristicCallbacks {void onWrite(BLECharacteristic *pCharacteristic) {std::string rxValue = pCharacteristic->getValue();if (rxValue.length() > 0) {Serial.println("*********");Serial.print("Received Value: ");for (int i = 0; i < rxValue.length(); i++)Serial.print(rxValue[i]);if(rxValue[0]=='a'){digitalWrite(2,HIGH);//open switchdeviceswitch=true;runtime = millis() / 1000;}if(rxValue[0]=='b'){digitalWrite(2,LOW);//open switchdeviceswitch=false;}Serial.println();Serial.println("*********");}} };void setup() {Serial.begin(115200);pinMode(2,OUTPUT);//設置輸出2腳digitalWrite(2,LOW);//默認低電平Wire.begin(/*SDA*/22,/*SCL*/21);// 初始化OLED并設置其IIC地址為 0x3Cdisplay.begin(SSD1306_SWITCHCAPVCC, 0x3C);// Create the BLE DeviceBLEDevice::init("UART Service");// Create the BLE ServerpServer = BLEDevice::createServer();pServer->setCallbacks(new MyServerCallbacks());// Create the BLE ServiceBLEService *pService = pServer->createService(SERVICE_UUID);// Create a BLE CharacteristicpTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX,BLECharacteristic::PROPERTY_NOTIFY);pTxCharacteristic->addDescriptor(new BLE2902());BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX,BLECharacteristic::PROPERTY_WRITE);pRxCharacteristic->setCallbacks(new MyCallbacks());// Start the servicepService->start();// Start advertisingpServer->getAdvertising()->start();Serial.println("Waiting a client connection to notify..."); }void loop() {if (deviceConnected) {pTxCharacteristic->setValue(&txValue, 1);pTxCharacteristic->notify();txValue++;delay(10); // bluetooth stack will go into congestion, if too many packets are sent}// disconnectingif (!deviceConnected && oldDeviceConnected) {delay(500); // give the bluetooth stack the chance to get things readypServer->startAdvertising(); // restart advertisingSerial.println("start advertising");oldDeviceConnected = deviceConnected;}// connectingif (deviceConnected && !oldDeviceConnected) {// do stuff here on connectingoldDeviceConnected = deviceConnected;}if(deviceswitch==true){words_display();display.display();//Serial.println("start advertising");} }void words_display() {uint8_t runtime0 = (millis() / 1000)-runtime;// 清除屏幕display.clearDisplay();// 設置字體顏色,白色可見display.setTextColor(WHITE);//設置字體大小display.setTextSize(1.5);//設置光標位置display.setCursor(0, 0);display.print("BLE SWITCH");display.setCursor(0, 10);display.print("Run time: ");//打印自開發(fā)板重置以來的秒數(shù):if(runtime0>3600){display.print(runtime0/3600);display.print(" h");}if(runtime0>60){display.print(runtime0/60);display.print(" min");}else {display.print(runtime0);display.print(" s"); }display.setCursor(0, 20);//Serial.println(runtime);//ESP.restart(); //ESP.reset(); }最后說明一下,保存的代碼有兩個文件夾,如果上述不能使用請下載壓縮包,
有論述性文章可供參考。
總結
以上是生活随笔為你收集整理的基于ESP32的BLE蓝牙开关的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hive中 <> 和 != 的区别
- 下一篇: 开源BI工具2:apache/super