实训9——蓝牙修改开锁密码
實訓九 : 藍牙修改開鎖密碼
一、實驗目的
通過ESP32,按鍵模塊和SG90舵機,實現藍牙發送相應的命令修改按鍵密碼
二、實驗內容
1.連接ESP32和按鍵模塊的引腳,以及ESP32和SG90舵機的引腳
2.從鍵盤輸入密碼,ESP32獲取到所輸入的密碼并判斷是否正確
3.密碼驗證正確后,ESP32控制舵機的轉動
三、實驗設備
1.ESP32-WROOM-32D 開發板
2. 3*4按鍵模塊
3.SG90舵機
4.杜邦線
四、實驗步驟
1) 連接引腳
3*4矩陣鍵盤
引腳依次為: 7 6 5 4 3 2 1(從左往右)
SG90 舵機
SG90 舵機的引腳圖
引腳依次為:
1.VCC
2.PWM : 脈沖寬度調制
3.GND
連接圖
2) 環境配置
1.下載適用于ESP32的電機驅動的庫 Servo.h,下載鏈接:**https://github.com/RoboticsBrno/ESP32-Arduino-Servo-Library **
2.打開Arduino IDE,選擇左上角 工具–開發板,選擇對應的開發板(ESP32 Dev Module)
3.選擇 工具–端口 , 選擇對應的端口
3) 測試代碼
#include <dummy.h> #include <Servo.h> #include <BLEDevice.h> #include <BLE2902.h> #include <String.h> #include <Keypad.h> #define SERVO_PIN 15BLECharacteristic *pCharacteristic; bool deviceConnected = false; #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" #define openInterval 5000 const byte ROWS = 4; //four rows const byte COLS = 3; //three columns char keys[ROWS][COLS] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'} }; byte rowPins[ROWS] = {33, 27, 14, 12}; //connect to the row pinouts of the keypad byte colPins[COLS] = {4, 0, 2}; //connect to the column pinouts of the keypadKeypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); byte ble_rx_buffer[20]; //接收藍牙指令 byte verfi_code[6] = {0x31,0x32,0x33,0x34,0x35,0x36};//初始密碼 byte init_pw[6] = {1,2,3,4,5,6};//初始按鍵密碼 Servo myservo; int pos=0; bool toggle; //開鎖判斷位 void locker(bool toggle) { //開鎖myservo.attach(SERVO_PIN); if (toggle) {myservo.write(90); // 開鎖} else {myservo.write(0); // 關鎖}delay(250); // 等馬達轉到定位myservo.detach(); } class MyServerCallbacks: public BLEServerCallbacks {void onConnect(BLEServer* pServer) {deviceConnected = true;};void onDisconnect(BLEServer* pServer) {deviceConnected = false;} }; /*相應的命令幀例子* 55aa1000313233343536 藍牙發送開門指令,hex 16進制發送* 55aa2000363534333231 藍牙修改密碼指令 */ class MyCallbacks: public BLECharacteristicCallbacks {void onWrite(BLECharacteristic *pCharacteristic) {std::string rxValue = pCharacteristic->getValue(); byte op;for (int i = 0; i < rxValue.length(); i++){ble_rx_buffer[i] = rxValue[i];Serial.print(rxValue[i]);}if(ble_rx_buffer[0] == 0x55 && ble_rx_buffer[1] == 0xaa){ //匹配幀頭55aaop=ble_rx_buffer[2];switch(op){ //標識位case 0x10:{ //藍牙開門if(memcmp(ble_rx_buffer+4,verfi_code,6) == 0){ //匹配成功toggle = true; Serial.println("開門成功"); }else{Serial.println("密碼錯誤");}break;}case 0x20:{ //設置按鍵密碼static byte temp_pw[6]; //臨時密碼for(byte i=0;i<6;i++){ if(ble_rx_buffer[i+4]<=0x39 && ble_rx_buffer[i+4]>=0x30){ //保證密碼在0-9之間 temp_pw[i]=ble_rx_buffer[i+4]-0x30; }else{ //無效字符break;}if(i==5){ //密碼修改完成Serial.println("密碼修改完成");memcpy(init_pw,temp_pw,6); }}break;}case 0x30:{ //查看指紋break;}case 0x31:{//添加指紋break;}case 0x32:{ //刪除指紋break;}}//locker(toggle);}} };void setupBLE(String BLEName){ //連接藍牙const char *ble_name = BLEName.c_str(); BLEDevice::init(ble_name); BLEServer *pServer = BLEDevice::createServer(); //創建服務pServer->setCallbacks(new MyServerCallbacks());BLEService *pService = pServer->createService(SERVICE_UUID); pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX,BLECharacteristic::PROPERTY_NOTIFY); pCharacteristic->addDescriptor(new BLE2902()); //添加描述BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX,BLECharacteristic::PROPERTY_WRITE);pCharacteristic->setCallbacks(new MyCallbacks()); //receive message callback pService->start();// Start the servicepServer->getAdvertising()->start();// Start advertisingSerial.println("Waiting a client connection to notify..."); }void compare_pw(char key){static byte pw[20];static byte keys_len = 0;if(key){Serial.println(key);switch(key){case '#':{ //確認鍵if(keys_len == 6 && memcmp(pw,init_pw,6)==0){ //匹配成功,開鎖Serial.println("開門成功");toggle = true; }else{Serial.println("密碼錯誤");}keys_len=0; //清零break;}case '*':{ //回退鍵if(keys_len >0)keys_len--;break;}default:{ //默認數字鍵0-9pw[keys_len++]=key-'0';if(keys_len>19){Serial.println("OverSize");keys_len=0;}break;}}} } void setup() { Serial.begin(115200);setupBLE("ESP32 Device");//設置藍牙名稱myservo.attach(SERVO_PIN); //引腳12為PWM腳 } void loop() {static unsigned long toggle_time = millis();static bool lock_back_flag=false;compare_pw(keypad.getKey()); //比對按鍵輸入密碼if(toggle){ //開門計時locker(toggle); //開鎖toggle = false;lock_back_flag = true;toggle_time = millis();}if(millis() - toggle_time > openInterval && lock_back_flag){ //回鎖時間lock_back_flag = false;locker(toggle);//回鎖} }4)手機APP發送信息
通訊協議:
| 長度 | 2B | 1B | 1B | 16B |
幀頭:55aa
標識:
10 : 藍牙開門
20:設置密碼
30:查看指紋
31:添加指紋
32:刪除指紋
檢驗和:目前不做校驗,填00即可
數據: 16字節,可以不填滿
藍牙修改按鍵密碼
| 內容 | 55aa | 20 | 00 | 6B修改密碼 |
藍牙修改按鍵密碼發送消息案例:55aa2000363534333231
ps:都是以16進制發送,對應相應的ASCII碼
5) 測試結果
鍵盤分配0-9,*為回退鍵,#為確認鍵
代碼燒錄成功后,藍牙發送55aa2000363534333231,修改密碼為654321
鍵盤輸入6 5 4 3 2 1 #(確認鍵)
如果密碼不正確,則需顯示密碼錯誤;
如果密碼正確,則會控制舵機轉動來實現開鎖操作
總結
以上是生活随笔為你收集整理的实训9——蓝牙修改开锁密码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot集成elastics
- 下一篇: python中的与或非 | ^