{"id":735,"date":"2022-12-16T21:28:01","date_gmt":"2022-12-16T13:28:01","guid":{"rendered":"http:\/\/wordpress.loc\/?p=735"},"modified":"2022-12-16T21:28:49","modified_gmt":"2022-12-16T13:28:49","slug":"esp32_ch3-10","status":"publish","type":"post","link":"https:\/\/www.nmking.io\/index.php\/2022\/12\/16\/735\/","title":{"rendered":"ESP32 BLE\u6383\u63cf \u7c21\u6613\u5ba4\u5167\u5b9a\u4f4d\u4e4b-\u6211\u90a3\u72d7\u5b50\u5728\u5bb6\u55ce\uff1f"},"content":{"rendered":"\n<figure class=\"wp-block-pullquote\"><blockquote><p>\u6211\u5bb6\u90a3\u72d7\u5b50\u5728\u5bb6\u55ce\uff1f\u4e0d\u77e5\u9053\u6709\u6c92\u6709\u8d81\u6211\u52a0\u73ed\u6e9c\u51fa\u53bb\u9b3c\u6df7\uff1f<br>\u6211\u60f3\u6bcf\u4e00\u500b\u4eba\u90fd\u6703\u60f3\u77e5\u9053\u53e6\u5916\u4e00\u534a\u6709\u6c92\u6709\u4e56\u4e56\u5728\u5bb6\uff0c\u9664\u4e86\u6253\u96fb\u8a71\u67e5\u52e4\u3001\u8acb\u9130\u5c45\uff08\u95a8\u871c\uff01\uff09\u770b\u7ba1\uff0c\u6216\u8005\u5b89\u88dd\u76e3\u8996\u5668\u96a8\u6642\u9060\u7aef\u67e5\u770b\u4e4b\u5916\uff0c\u9084\u6709\u53e6\u5916\u4e00\u500b\u9078\u64c7<\/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>\u6211\u5bb6\u90a3\u72d7\u5b50\u5728\u5bb6\u55ce\uff1f\u4e0d\u77e5\u9053\u6709\u6c92\u6709\u8d81\u6211\u52a0\u73ed\u6e9c\u51fa\u53bb\u9b3c\u6df7\uff1f<\/p>\n\n\n\n<p>\u6211\u60f3\u6bcf\u4e00\u500b\u4eba\u90fd\u6703\u60f3\u77e5\u9053\u53e6\u5916\u4e00\u534a\u6709\u6c92\u6709\u4e56\u4e56\u5728\u5bb6\uff0c\u9664\u4e86\u6253\u96fb\u8a71\u67e5\u52e4\u3001\u8acb\u9130\u5c45\uff08\u95a8\u871c\uff01\uff09\u770b\u7ba1\uff0c\u6216\u8005\u5b89\u88dd\u76e3\u8996\u5668\u96a8\u6642\u9060\u7aef\u67e5\u770b\u4e4b\u5916\uff0c\u9084\u6709\u53e6\u5916\u4e00\u500b\u9078\u64c7\uff0c\u5c31\u662f\u85cd\u82bd\u6383\u63cf\uff0c\u4e0d\u904e\u9019\u5f97\u4ed6\u6709\u6234\u8457\u72d7\u74b0\uff0c\u6b50\u4e0d\uff0c\u662f\u624b\u74b0\u624d\u884c\u3002<\/p>\n\n\n\n<p>\u73fe\u5728\u7684\u624b\u74b0\u70ba\u4e86\u80fd\u6642\u6642\u8207\u624b\u6a5f\u9023\u7dda\u6216\u8005\u5176\u4ed6\uff08\u6a5f\u5bc6\uff09\u529f\u80fd\u90fd\u6703\u505a\u85cd\u82bdBeacon\u5ee3\u64ad\uff0c\u90a3\u9ebc\u6211\u5011\u5c31\u53ef\u4ee5\u5229\u7528\u9019\u500b\u7279\u6027\uff0c\u5077\u5077\u5728\u80cc\u5f8c\u6536\u96c6\u85cd\u82bd\u5ee3\u64ad\uff0c\u5982\u679c\u627e\u4e0d\u5230\u4ed6\u7684\u624b\u74b0\u5ee3\u64ad\uff0c\u5c31\u53ef\u4ee5\u78ba\u5b9a\u4ed6\u4e0d\u5728\u5bb6\u4e86\uff0c\u81f3\u65bc\u51fa\u53bb\u9b3c\u6df7\uff0c\u9084\u662f\u53ea\u662f\u53bb\u5df7\u53e3711\u8cb7\u676f\u5496\u5561\u9019\u5c31\u662f\u4f60\u5011\u4e4b\u9593\u7684\u554f\u984c\uff0c\u79d1\u6280\u53ea\u80fd\u89e3\u6c7a\u554f\u984c\uff0c\u4f60\u624d\u662f\u90a3\u500b\u80fd\u89e3\u6c7a\u6709\u554f\u984c\u7684\u4eba\u3002<\/p>\n\n\n\n<p>\u672c\u7bc7\u6211\u5011\u900f\u904eESP32\u505aBLE\u6383\u63cf\uff0c\u5982\u679c\u6383\u63cf\u5230\u4f60\u5bb6\u72d7\u5b50\u7684\u624b\u74b0\uff0c\u4e14RSSI\u8d85\u904e-80\u7684\u8a71\uff0c\u5c31\u5224\u5b9a\u70ba\u5728\u5bb6\uff0c\u5426\u5247\u5c31\u5224\u5b9a\u4e0d\u5728\u5bb6\uff0c\u7136\u5f8c\u518d\u5c07\u8a0a\u865f\u50b3\u9001\u5230ThingSpeak\u88fd\u4f5c\u6210\u5716\u8868\uff0c\u9019\u6a23\u4f60\u5c31\u53ef\u4ee5\u76e3\u8996\u9019\u72d7\u5b50\u7684\u884c\u8e64\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pic.pimg.tw\/youyouyou\/1611634138-2475225637-g_n.png?resize=476%2C642&#038;ssl=1\" alt=\"\" width=\"476\" height=\"642\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1.\u7576\u7136\u4f60\u5fc5\u9808\u5148\u53d6\u5f97\u4f60\u5bb6\u72d7\u5b50\u624b\u74b0\u7684Mac address<\/h3>\n\n\n\n<p>\u624b\u6a5fAPP\u5b89\u88dd\u4e00\u4e0bLightblue\u5c31\u53ef\u4ee5\u6383\u63cf\uff0c\u4ed6\u7684\u756b\u9762\u5982\u4e0b\uff0c\u7d05\u8272\u5708\u5708\u5c31\u662f\u6211\u7684\u624b\u74b0\uff0c\u4e0b\u65b9\u7684\u300cE0:E2:0A:25:FE:2F\u300d\u5c31\u662f\u6211\u7684Mac address\uff0c\u4e0d\u904e\u5230\u6642\u5019\u8981\u7528\u5c0f\u5beb\u5594\u300ce0:e2:0a:25:fe:2f\u300d<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pic.pimg.tw\/youyouyou\/1611588327-3489098863-g_n.png?resize=306%2C660&#038;ssl=1\" alt=\"\" width=\"306\" height=\"660\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2.\u5230ThingSpeak\u958b\u555f\u4e00\u500bchannel\u4f86\u6536\u8cc7\u6599<\/h3>\n\n\n\n<p>\u9019\u90e8\u4efd\u8acb\u53c3\u95b1\uff1a<a href=\"https:\/\/www.nmking.io\/index.php\/2022\/11\/22\/esp32_ch1-17\/\">\u7b2c\u5341\u56db\u7bc7 ESP32 \u8cc7\u6599\u5eab\u5b58\u53d6ThingSpeak\u5716\u8868\u88fd\u4f5c<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.\u5c07\u7a0b\u5f0f\u71d2\u9304\u5230ESP32\u4e2d\uff0c\u4e26\u4fee\u6539\u76f8\u95dc\u8a2d\u5b9a\u5373\u53ef\u3002<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;BLEDevice.h>\n#include &lt;BLEUtils.h>\n#include &lt;BLEScan.h>\n#include &lt;BLEAdvertisedDevice.h>\n#include &lt;WiFi.h>\n#include &lt;HTTPClient.h>\nchar ssid&#91;] = \"SSID\"; \/\/\u7121\u7dda\u7db2\u8defSSID\nchar password&#91;] = \"PASSWORD\"; \/\/\u7121\u7dda\u7db2\u8def\u5bc6\u78bc\nString url = \"http:\/\/api.thingspeak.com\/update?api_key=\u4f60\u7684ThingSpeak Key\";\nString Myaddress = \"Beacon Address\"; \/\/Beacon\u6216\u624b\u9336MAC address: xx:xx:xx:xx (\u5c0f\u5beb\u5b57\u6bcd)\nint RSSIThreshold =-80;\/\/\u4ee5RSSI\u5f37\u5ea6\u505a\u7c21\u6613\u6e2c\u8ddd\nint scanTime = 20; \/\/\u8a2d\u5b9a20\u79d2\u6383\u63cf\u4e00\u6b21\n\nBLEScan* pBLEScan;\nclass MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {\n    void onResult(BLEAdvertisedDevice advertisedDevice) {\n      \/\/\u5217\u51faBeacon\u6240\u6709\u8cc7\u8a0a\n      \/\/Serial.printf(\"Advertised Device: %s \\n\", advertisedDevice.toString().c_str());\n      \/\/\u53d6\u5f97address\n      String BeaconAddress = advertisedDevice.getAddress().toString().c_str();\n      Serial.print(\"Address:\");Serial.print(BeaconAddress);\n      \/\/\u53d6\u5f97RSSI\n      int BeaconRSSI = advertisedDevice.getRSSI();\n      Serial.print(\",RSSI:\");Serial.println(BeaconRSSI);\n\n      if (BeaconAddress == Myaddress) {\n        if ( BeaconRSSI >= RSSIThreshold) {\n          Serial.println(\"\u5728\u5bb6\");\n          HTTPClient http;\n          String url1 = url + \"&amp;field1=1\";\n          \/\/http client\u53d6\u5f97\u7db2\u9801\u5167\u5bb9\n          http.begin(url1);\n          int httpCode = http.GET();\n          if (httpCode == HTTP_CODE_OK)      {\n            \/\/\u8b80\u53d6\u7db2\u9801\u5167\u5bb9\u5230payload\n            String payload = http.getString();\n            \/\/\u5c07\u5167\u5bb9\u986f\u793a\u51fa\u4f86\n            Serial.print(\"\u7db2\u9801\u5167\u5bb9=\");\n            Serial.println(payload);\n          } else {\n            \/\/\u8b80\u53d6\u5931\u6557\n            Serial.println(\"\u7db2\u8def\u50b3\u9001\u5931\u6557\");\n          }\n          http.end();\n        }\n        else {\n          Serial.println(\"\u4e0d\u5728\u5bb6\");\n          HTTPClient http;\n          String url1 = url + \"&amp;field1=0\";\n          \/\/http client\u53d6\u5f97\u7db2\u9801\u5167\u5bb9\n          http.begin(url1);\n          int httpCode = http.GET();\n          if (httpCode == HTTP_CODE_OK)      {\n            \/\/\u8b80\u53d6\u7db2\u9801\u5167\u5bb9\u5230payload\n            String payload = http.getString();\n            \/\/\u5c07\u5167\u5bb9\u986f\u793a\u51fa\u4f86\n            Serial.print(\"\u7db2\u9801\u5167\u5bb9=\");\n            Serial.println(payload);\n          } else {\n            \/\/\u8b80\u53d6\u5931\u6557\n            Serial.println(\"\u7db2\u8def\u50b3\u9001\u5931\u6557\");\n          }\n        }\n      }\n    }\n};\n\nvoid setup() {\n  Serial.begin(115200);\n  Serial.println(\"\u958b\u59cb\u9023\u7ddaWiFi\u7db2\u8def\");\n  WiFi.mode(WIFI_STA);\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    Serial.print(\".\");\n    delay(1000);\n  }\n  Serial.println(\"WiFi\u9023\u7dda\u5b8c\u6210\");\n  \/\/\u8a2d\u5b9aBLE\u6383\u63cf\n  BLEDevice::init(\"\");\n  pBLEScan = BLEDevice::getScan(); \/\/create new scan\n  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());\n  pBLEScan->setActiveScan(true); \/\/active scan uses more power, but get results faster\n  pBLEScan->setInterval(100);\n  pBLEScan->setWindow(99);  \/\/ less or equal setInterval value\n}\n\nvoid loop() {\n  Serial.print(\"BLE\u6383\u63cf\u958b\u59cb....\");\n  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);\n  Serial.print(\"\u4e00\u5171\u627e\u5230\u88dd\u7f6e: \");\n  Serial.println(foundDevices.getCount());\n  pBLEScan->clearResults();   \/\/ delete results fromBLEScan buffer to release memory\n  delay(100);\n}\n\n\n\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.\u9019\u6642\u4f60\u5c31\u53ef\u4ee5\u67e5\u770b\u4f60\u5bb6\u72d7\u5b50\u662f\u4e0d\u662f\u5728\u5bb6\u4e86\u3002<\/h3>\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\/1611588983-355984372-g.png?resize=842%2C590&#038;ssl=1\" alt=\"\" width=\"842\" height=\"590\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u5bb6\u90a3\u72d7\u5b50\u5728\u5bb6\u55ce\uff1f\u4e0d\u77e5\u9053\u6709\u6c92\u6709\u8d81\u6211\u52a0\u73ed\u6e9c\u51fa\u53bb\u9b3c\u6df7\uff1f\u6211\u60f3\u6bcf\u4e00\u500b\u4eba\u90fd\u6703\u60f3\u77e5\u9053\u53e6\u5916\u4e00\u534a\u6709\u6c92\u6709\u4e56\u4e56\u5728\u5bb6\uff0c\u9664\u4e86\u6253\u96fb\u8a71\u67e5\u52e4 [&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-735","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\/735","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=735"}],"version-history":[{"count":1,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/735\/revisions"}],"predecessor-version":[{"id":736,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/735\/revisions\/736"}],"wp:attachment":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/media?parent=735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/categories?post=735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/tags?post=735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}