2023-01-10 10:08:09andrewseia

若何用藍芽Bluetooth連線控制 Arduino 網站架

學會Arduino根基操控後
一定會想學會無線遙控,如藍芽Bluetooth, Wifi
這篇申明藍芽Bluetooth操控

結果圖
1.png


影片


代碼:

  1. // Include necessary libraries
  2. #include <BLEDevice.h>
  3. #include <BLEServer.h>
  4. #include <BLEUtils.h>
  5. //#include <BLE2902.h>
  6. //#include <Wire.h>
  7.  
  8. // 定義 UUIDs (注意要與App Inventor內容對應)
  9. #define SERVICE_UUID            "C6FBDD3C-7123-4C9E-86AB-005F1A7EDA01"
  10. #define CHARACTERISTIC_UUID_RX  "B88E098B-E464-4B54-B827-79EB2B150A9F"
  11. #define CHARACTERISTIC_UUID_TX  "D769FACF-A4DA-47BA-9253-65359EE480FB"
  12.  
  13. // 界說LM35 ESP32 GPIO接腳
  14. const int analogIn = A0;
  15.   
  16. int RawValue= 0;
  17. double Voltage = 0;
  18. double tempC = 0;
  19. double tempF = 0;
  20. String BLE_Code;
  21. BLECharacteristic *pCharacteristic;
  22. bool deviceConnected = false;
  23. // Handle received and sent messages
  24. boolean ledState=false;
  25. String message = "";
  26. char incomingChar;
  27.  
  28. // Temperature Sensor 與led接腳變數
  29. float temperature = 0;
  30. const int ledPin = 2;
  31.  
  32. // 設定 callbacks onConnect & onDisconnect函數
  33. class MyServerCallbacks: public BLEServerCallbacks {
  34.   void onConnect(BLEServer* pServer) {
  35.     deviceConnected = true;
  36.   };
  37.   void onDisconnect(BLEServer* pServer) {
  38.     deviceConnected = false;
  39.   }
  40. };
  41.  
  42. // 設定 callback function 當收到新的資訊 (from the Android application)
  43. class MyCallbacks: public BLECharacteristicCallbacks {
  44.   void onWrite(BLECharacteristic *pCharacteristic) {
  45.     std::string rxValue = pCharacteristic->getValue();
  46.     BLE_Code="";
  47.     if(rxValue.length() > 0) {
  48.       Serial.print("接收資料為 : ");
  49.       for(int i = 0; i < rxValue.length(); i++) {
  50.         BLE_Code+=rxValue[i];
  51.         Serial.print(rxValue[i]);
  52.       }
  53.       Serial.println();
  54.       BLE_Code.toUpperCase();
  55.       Serial.println(BLE_Code);
  56.       if(BLE_Code.indexOf("LED")==0)
  57.       {
  58.         ledState=!ledState;
  59.       Serial.println(ledState);
  60.       }
  61.       if(BLE_Code.indexOf("ON")==0)
  62.       {
  63.         Serial.println("LED 點亮!");
  64.         ledState=true;
  65.       }
  66.       else if(BLE_Code.indexOf("OFF")==0) {
  67.         Serial.println("LED 熄滅!");
  68.         ledState=false;
  69.       }
  70.     }
  71.   }
  72. };
  73.  
  74. void setup() {
  75.   Serial.begin(115200);
  76.   pinMode(ledPin, OUTPUT);
  77.    
  78.   // 成立BLE Device
  79.   BLEDevice::init("ESP32_WeMos1");
  80.  
  81.   // 創設BLE Server
  82.   BLEServer *pServer = BLEDevice::createServer();
  83.   pServer->setCallbacks(new MyServerCallbacks());
  84.  
  85.   // 創立BLE Service
  86.   BLEService *pService = pServer->createService(SERVICE_UUID);
  87.  
  88.   // 創設BLE Characteristic
  89.   pCharacteristic = pService->createCharacteristic(
  90.                       CHARACTERISTIC_UUID_TX,
  91.                       BLECharacteristic::PROPERTY_NOTIFY);                     
  92. //  pCharacteristic->addDescriptor(new BLE2902());
  93.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  94.                                          CHARACTERISTIC_UUID_RX,
  95.                                          BLECharacteristic::PROPERTY_WRITE);
  96. pCharacteristic->setCallbacks(new MyCallbacks());
  97.  
  98.   // 入手下手(起)service
  99.   pService->start();
  100.  
  101.   // 最先(起)advertising
  102.   pServer->getAdvertising()->start();
  103.   Serial.println("等待BLE手機連線....");
  104.   
  105.   digitalWrite(ledPin,LOW);
  106.   delay(500);
  107.   digitalWrite(ledPin,HIGH);
  108.   delay(500);
  109.   digitalWrite(ledPin,LOW);
  110. }
  111.  
  112. void loop() {
  113.   // Check received message and control output accordingly
  114.     if (ledState)
  115.         digitalWrite(ledPin, HIGH);
  116.       else
  117.         digitalWrite(ledPin, LOW);
  118.   delay(20);
  119. }
複製代碼


文章出處:網頁設計,網站架設 ,網路行銷,網頁優化,SEO - NetYea 網頁設計



來自: