{"id":729,"date":"2022-12-16T17:46:05","date_gmt":"2022-12-16T09:46:05","guid":{"rendered":"http:\/\/wordpress.loc\/?p=729"},"modified":"2022-12-16T17:46:35","modified_gmt":"2022-12-16T09:46:35","slug":"esp32_ch1-28","status":"publish","type":"post","link":"https:\/\/www.nmking.io\/index.php\/2022\/12\/16\/729\/","title":{"rendered":"ESP32 BLE\u9023\u7dda\u8207\u5ee3\u64ad"},"content":{"rendered":"\n<figure class=\"wp-block-pullquote\"><blockquote><p>\u4ee5\u524d\u90fd\u8aaa\u5728\u73a9ESP32\u7684\u85cd\u82bd\uff0c\u5176\u5be6\u90fd\u662fBT Serial \u5c6c\u65bcBT2.0\uff0c\u6700\u8fd1\u6709\u4e00\u500b\u65b0\u6848\u5b50\uff0c\u8981\u6539\u7528BLE\uff0c\u7db2\u8def\u627e\u5230\u8cc7\u6e90\uff0c\u6e2c\u8a66\u6b63\u5e38\u53ef\u7528\uff0c\u53ef\u4ee5\u7528\u624b\u6a5f\u4e0d\u7528\u914d\u5c0d\u76f4\u63a5\u9023\u7dda\uff0c\u7136\u5f8c\u6536\u8cc7\u6599\u9001\u8cc7\u6599\u90fdOK\u3002<\/p><\/blockquote><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u6211\u7684ESP32\u5be6\u505a\u66f8\u7c4d\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/www.nmking.io\/index.php\/2022\/11\/10\/esp32_ch1-2\/\" target=\"_blank\">\u6211\u51fa\u66f8\u4e86 ESP32 \u7269\u806f\u7db2\u5c08\u984c<\/a><br>\u535a\u5ba2\u4f86\u7db2\u5740\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/www.books.com.tw\/products\/0010901195\" target=\"_blank\">https:\/\/www.books.com.tw\/products\/0010901195<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<p>\u4ee5\u524d\u90fd\u8aaa\u5728\u73a9ESP32\u7684\u85cd\u82bd\uff0c\u5176\u5be6\u90fd\u662fBT Serial \u5c6c\u65bcBT2.0\uff0c\u6700\u8fd1\u6709\u4e00\u500b\u65b0\u6848\u5b50\uff0c\u8981\u6539\u7528BLE\uff0c\u7db2\u8def\u627e\u5230\u8cc7\u6e90\uff0c\u6e2c\u8a66\u6b63\u5e38\u53ef\u7528\uff0c\u53ef\u4ee5\u7528\u624b\u6a5f\u4e0d\u7528\u914d\u5c0d\u76f4\u63a5\u9023\u7dda\uff0c\u7136\u5f8c\u6536\u8cc7\u6599\u9001\u8cc7\u6599\u90fdOK\u3002<\/p>\n\n\n\n<p>\u624b\u6a5f\u6536\u9001\u8cc7\u6599\uff0c\u8acb\u7528Lightblue APP\uff08<a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.punchthrough.lightblueexplorer&amp;hl=zh_TW&amp;gl=US\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/play.google.com\/store\/apps\/details?id=com.punchthrough.lightblueexplorer&amp;hl=zh_TW&amp;gl=US<\/a>\uff09<\/p>\n\n\n\n<p>\u5148\u7d00\u9304\u4e00\u4e0b\uff0c\u665a\u9ede\u518d\u88dc\u6559\u5b78<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pic.pimg.tw\/youyouyou\/1609952135-1988464100-g_n.png?w=1290&#038;ssl=1\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Lightblue APP\u641c\u5c0b\u7d50\u679c<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pic.pimg.tw\/youyouyou\/1609951585-3658853189-g_n.png?resize=840%2C643&#038;ssl=1\" alt=\"\" width=\"840\" height=\"643\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/https:\/\/www.instructables.com\/ESP32-Bluetooth-Low-Energy\/\n#include &lt;BLEDevice.h>\n#include &lt;BLEServer.h>\n#include &lt;BLEUtils.h>\n#include &lt;BLE2902.h>\n\nBLECharacteristic *characteristicTX; \/\/atrav\u00e9s desse objeto iremos enviar dados para o client\n\n\/\/objeto respons\u00e1vel pela comunica\u00e7\u00e3o com o sensor de temperatura infravermelho\n\nbool deviceConnected = false; \/\/controle de dispositivo conectado\n\nconst int LED = 2; \/\/LED interno do ESP32 (esse pino pode variar de placa para placa) \/\/ Could be different depending on the dev board. I used the DOIT ESP32 dev board.\nconst int BUZZER = 23; \/\/pino d BUZZER\n\n\/\/ See the following for generating UUIDs:\n\/\/ https:\/\/www.uuidgenerator.net\/\n\n#define SERVICE_UUID           \"ab0828b1-198e-4351-b779-901fa0e0371e\" \/\/ UART service UUID\n#define CHARACTERISTIC_UUID_RX \"4ac8a682-9736-4e5d-932b-e9b31405049c\"\n#define CHARACTERISTIC_UUID_TX \"0972EF8C-7613-4075-AD52-756F33D4DA91\"\n\n\n\/\/callback para receber os eventos de conex\u00e3o de dispositivos\nclass ServerCallbacks: public BLEServerCallbacks {\n    void onConnect(BLEServer* pServer) {\n      deviceConnected = true;\n    };\n\n    void onDisconnect(BLEServer* pServer) {\n      deviceConnected = false;\n    }\n};\n\n\/\/callback  para envendos das caracter\u00edsticas\nclass CharacteristicCallbacks: public BLECharacteristicCallbacks {\n    void onWrite(BLECharacteristic *characteristic) {\n      \/\/retorna ponteiro para o registrador contendo o valor atual da caracteristica\n      std::string rxValue = characteristic->getValue(); \n      \/\/verifica se existe dados (tamanho maior que zero)\n      if (rxValue.length() > 0) {\n        Serial.println(\"*********\");\n        Serial.print(\"Received Value: \");\n\n        for (int i = 0; i &lt; rxValue.length(); i++) {\n          Serial.print(rxValue&#91;i]);\n        }\n\n        Serial.println();\n\n        \/\/ Do stuff based on the command received\n        if (rxValue.find(\"L1\") != -1) { \n          Serial.print(\"Turning LED ON!\");\n          digitalWrite(LED, HIGH);\n        }\n        else if (rxValue.find(\"L0\") != -1) {\n          Serial.print(\"Turning LED OFF!\");\n          digitalWrite(LED, LOW);\n        }\n        \/\/ Do stuff based on the command received from the app\n        else if (rxValue.find(\"B1\") != -1) { \n          Serial.print(\"Turning Buzzer ON!\");\n          digitalWrite(BUZZER, HIGH);\n        }\n        else if (rxValue.find(\"B0\") != -1) {\n          Serial.print(\"Turning Buzzer OFF!\");\n          digitalWrite(BUZZER, LOW);\n        }\n\n        Serial.println();\n        Serial.println(\"*********\");\n      }\n    }\n};\n\nvoid setup() {\n  Serial.begin(115200);\n\n  pinMode(LED, OUTPUT);\n  pinMode(BUZZER, OUTPUT);\n\n  \/\/ Create the BLE Device\n  BLEDevice::init(\"ESP32-BLE\"); \/\/ nome do dispositivo bluetooth\n  \/\/ Create the BLE Server\n  BLEServer *server = BLEDevice::createServer(); \/\/cria um BLE server \n  server->setCallbacks(new ServerCallbacks()); \/\/seta o callback do server\n  \/\/ Create the BLE Service\n  BLEService *service = server->createService(SERVICE_UUID);\n  \/\/ Create a BLE Characteristic para envio de dados\n  characteristicTX = service->createCharacteristic(\n                      CHARACTERISTIC_UUID_TX,\n                      BLECharacteristic::PROPERTY_NOTIFY\n                    );\n                      \n  characteristicTX->addDescriptor(new BLE2902());\n\n  \/\/ Create a BLE Characteristic para recebimento de dados\n  BLECharacteristic *characteristic = service->createCharacteristic(\n                                         CHARACTERISTIC_UUID_RX,\n                                         BLECharacteristic::PROPERTY_WRITE\n                                       );\n\n  characteristic->setCallbacks(new CharacteristicCallbacks());\n  \/\/ Start the service\n  service->start();\n  \/\/ Start advertising (descoberta do ESP32)\n  server->getAdvertising()->start();\n  Serial.println(\"Waiting a client connection to notify...\");\n\n}\n\nvoid loop() {\n     \/\/se existe algum dispositivo conectado\n    if (deviceConnected) {\n      \/\/chamamos o m\u00e9todo \"read\" do sensor para realizar a leitura da temperatura\n      \/\/read retornar\u00e1 1 caso consiga realizar a leitura, ou 0 caso contr\u00e1rio \n      if (1)\n      {         \n          char txString&#91;8]=\"555\"; \/\/ make sure this is big enuffz         \n          characteristicTX->setValue(txString); \/\/seta o valor que a caracteristica notificar\u00e1 (enviar)       \n          characteristicTX->notify(); \/\/ Envia o valor para o smartphone\n          Serial.print(\"*** Sent Value: \");\n          Serial.print(txString);\n          Serial.println(\" ***\");\n      }\n  \n\/*\n    delay(1000);\n\n\n\n    characteristicTX2->setValue(\"Hello!\"); \/\/ Sending a test message\n    characteristicTX2->notify(); \/\/ Send the value to the app!*\/\n\n  }\n  delay(1000);\n\n}\n<\/code><\/pre>\n\n\n\n<p>\u8cc7\u6599\u4f86\u6e90\uff1ahttps:\/\/www.instructables.com\/ESP32-Bluetooth-Low-Energy\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u524d\u90fd\u8aaa\u5728\u73a9ESP32\u7684\u85cd\u82bd\uff0c\u5176\u5be6\u90fd\u662fBT Serial \u5c6c\u65bcBT2.0\uff0c\u6700\u8fd1\u6709\u4e00\u500b\u65b0\u6848\u5b50\uff0c\u8981\u6539\u7528BLE\uff0c\u7db2\u8def [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-729","post","type-post","status-publish","format-standard","hentry","category-esp32"],"blocksy_meta":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/comments?post=729"}],"version-history":[{"count":1,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":730,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/729\/revisions\/730"}],"wp:attachment":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}