{"id":2806,"date":"2018-06-09T16:30:15","date_gmt":"2018-06-09T08:30:15","guid":{"rendered":"https:\/\/drho.club\/?p=2251"},"modified":"2018-06-09T16:30:15","modified_gmt":"2018-06-09T08:30:15","slug":"arduino-bluetooth-read-sensor-value","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2018\/06\/09\/arduino-bluetooth-read-sensor-value\/","title":{"rendered":"\u3010Arduino\u3011\u624b\u6a5fAPP\u900f\u904e\u85cd\u7259\u8b80\u53d6Arduino\u4e0a\u7684\u6eab\u6fd5\u5ea6"},"content":{"rendered":"<p>\u5728\u4e0a\u4e00\u500b\u5be6\u7fd2\u4e2d\u6211\u5011\u5b78\u6703\u4e86\u5982\u4f55\u4f7f\u7528\u624b\u6a5f\u900f\u904e\u85cd\u7259\u63a7\u5236Arduino\u4e0a\u7684LED\uff0c\u5728\u9019\u500b\u5be6\u7fd2\u4e2d\u6211\u5011\u5c07\u518d\u9032\u4e00\u6b65\u4f86\u770b\u770b\uff0c\u5982\u4f55\u900f\u904e\u85cd\u7259\u8b80\u53d6\u5728Arduino\u4e0a\u6eab\u6fd5\u5ea6\u611f\u6e2c\u5668\u4e0a\u7684\u8cc7\u8a0a\u3002\u70ba\u4e86\u8b93\u6211\u5011\u7684\u5be6\u7fd2\u5167\u5bb9\u6709\u8cc7\u6599\u53ef\u4ee5\u8b80\u53d6\uff0c\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\u6211\u5011\u52a0\u4e0a\u4e86\u4e00\u500bDHT22\u6eab\u6fd5\u5ea6\u611f\u6e2c\u5668\u7684\u9023\u63a5\uff0c\u5982\u4e0b\u5716\u6240\u793a\uff1a<\/p>\n<p><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09-12.04.45.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2260 aligncenter\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09-12.04.45.jpg\" alt=\"\" width=\"700\" height=\"525\" \/><\/a><\/p>\n<p>\u4e0a\u5716\u4e2d\u9593\u7684\u90a3\u500b\u7db1\u72c0\u7684\u611f\u6e2c\u5668DHT22\uff0c\u5b83\u5171\u67094\u652f\u63a5\u8173\uff0c\u9762\u5411\u6211\u5011\u7684\u9019\u4e00\u5074\u5de6\u908a\u662f5V\u96fb\u6e90\u63a5\u8173\u8655\uff0c\u63a5\u8457\u662f\u8cc7\u6599\u63a5\u8173\uff08\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u628a\u5b83\u63a5\u5230Arduino\u7684D3\u63a5\u8173\uff09\uff0c\u7136\u5f8c\u662f\u7a7a\u7684\u7121\u7528\u63a5\u8173\uff0c\u6700\u53f3\u908a\u7684\u662fGND\u63a5\u5730\u3002\u4ee5\u4e0b\u662f\u5f9e\u53e6\u5916\u4e00\u500b\u89d2\u5ea6\u7684\u756b\u9762\uff1a<\/p>\n<p><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09-12.04.34.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2259 aligncenter\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09-12.04.34.jpg\" alt=\"\" width=\"700\" height=\"525\" \/><\/a><\/p>\n<p>\u5148\u4f86\u770b\u770b\u6210\u679c\u5f71\u7247\uff1a<\/p>\n<p><iframe loading=\"lazy\" title=\"\u624b\u6a5f\u900f\u904e\u85cd\u7259\u8b80\u53d6Arduino\u4e0a\u7684\u6eab\u6fd5\u5ea6\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/RfOXQJfa54Y?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u4ee5\u4e0b\u662f\u5728Arduino\u7aef\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>[code]<br \/>\n#include &lt;SoftwareSerial.h&gt;<br \/>\n#include &quot;DHT.h&quot;<br \/>\n#define DHTPIN 3<br \/>\n#define DHTTYPE DHT22<\/p>\n<p>DHT dht(DHTPIN, DHTTYPE);<\/p>\n<p>#include &lt;Wire.h&gt;<\/p>\n<p>int LED = 2;<br \/>\nSoftwareSerial BT(10,11); <\/p>\n<p>void setup() {<br \/>\n  BT.begin(38400);<br \/>\n  pinMode(LED, OUTPUT);<br \/>\n  Serial.begin(9600);<br \/>\n  dht.begin();<br \/>\n}<\/p>\n<p>void loop() {<br \/>\n  byte cmd[20];<br \/>\n  int strsize;<br \/>\n  while(true){<br \/>\n    if ((strsize=(BT.available()))&gt;0){<br \/>\n      for (int i=0; i&lt;strsize; i++){<br \/>\n        cmd[i]=char(BT.read());<br \/>\n      }<br \/>\n      Serial.println(cmd[0]);<br \/>\n    }<br \/>\n    if (cmd[0]==&#8217;a&#8217;) {<br \/>\n      digitalWrite(LED, HIGH);<br \/>\n    } else if (cmd[0]==&#8217;b&#8217;) {<br \/>\n      digitalWrite(LED, LOW);<br \/>\n    } else if (cmd[0]==&#8217;c&#8217;) {<br \/>\n      float h = dht.readHumidity();<br \/>\n      float t = dht.readTemperature();<br \/>\n      char hum_buf[10], tem_buf[10];<br \/>\n      if (!isnan(h) &amp;&amp; !isnan(t)) {<br \/>\n        dtostrf(h, 3, 2, hum_buf);<br \/>\n        dtostrf(t, 3, 2, tem_buf);<br \/>\n        for(int i=0; i&lt;5; i++) {<br \/>\n          BT.write(hum_buf[i]);<br \/>\n        }<br \/>\n        for(int i=0; i&lt;5; i++) {<br \/>\n          BT.write(tem_buf[i]);<br \/>\n        }<br \/>\n      }<br \/>\n      cmd[0]=&#8217;d&#8217;;<br \/>\n    }<br \/>\n  } <\/p>\n<p>}<br \/>\n[\/code]<\/p>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u662f\u5f9e\u524d\u4e00\u500b\u5be6\u7fd2\u4e2d\u589e\u52a0\u7a0b\u5f0f\u78bc\u800c\u4f86\u7684\u3002\u5176\u4e2dDHT\u9019\u500b\u985e\u5225\u662f\u7528\u4f86\u8b80\u53d6\u6eab\u6fd5\u5ea6\u611f\u6e2c\u5668\u6578\u503c\u7528\u7684\uff0c\u4f7f\u7528DHT dht(DHTPIN, DHTTYPE\uff09\u5efa\u7acbdht\u9019\u500b\u5be6\u4f8b\u8b8a\u6578\u4e4b\u5f8c\uff0c\u65bcsetup()\u4e2d\u57f7\u884cdht.begin()\u51fd\u6578\u521d\u59cb\u5316\u611f\u6e2c\u5668\u7684\u8a2d\u5b9a\uff0c\u7136\u5f8c\u5728\u7a0b\u5f0f\u4e2d\u5c31\u80fd\u5920\u4ee5dht.readHumidity()\u8b80\u53d6\u4e00\u500b\u6d6e\u9ede\u6578\u578b\u614b\u7684\u6fd5\u5ea6\u6578\u503c\uff0c\u4ee5\u53ca\u4f7f\u7528dht.readTemperature()\u8b80\u53d6\u6eab\u5ea6\u503c\uff0c\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5206\u5225\u628a\u6eab\u5ea6\u53ca\u6fd5\u5ea6\u653e\u5728\u6d6e\u9ede\u8b8a\u6578t\u548ch\u4e2d\u3002<\/p>\n<p>\u70ba\u4e86\u7c21\u6613\u793a\u7bc4\u8d77\u898b\uff0c\u5728\u8b80\u53d6\u5230\u6eab\u6fd5\u5ea6\u4e4b\u5f8c\uff0c\u6211\u5011\u628a\u9019\u5169\u500b\u6578\u503c\u5148\u4ee5dtostrf()\u51fd\u6578\u628a\u5b83\u5011\u8f49\u63db\u6210\u5b57\u5143\u578b\u614b\uff0c\u518d\u900f\u904eBT.write()\u51fd\u6578\uff0c\u9010\u4e00\u628a\u5b57\u5143\u5beb\u5230\u85cd\u7259\u63a5\u6536\u5668\uff0c\u4e5f\u5c31\u662f\u624b\u6a5f\u4e2d\u3002<\/p>\n<p>\u70ba\u4e86\u7dad\u6301\u4e0a\u4e00\u500b\u5be6\u7fd2\u4e2d\u7684\u529f\u80fd\uff0c\u6211\u5011\u7684Arduino\u9084\u662f\u6703\u5148\u8b80\u53d6\u4f86\u81ea\u65bc\u624b\u6a5f\u7684\u8a0a\u606f\uff0c\u5982\u679c\u5f97\u5230\u7684\u662f\u5b57\u5143a\uff0c\u5c31\u628aLED\u6253\u958b\uff0c\u548c\u4e0a\u4e00\u500b\u4f8b\u5b50\u4e00\u6a23\uff0c\u5982\u679c\u5f97\u5230\u7684\u662f\u5b57\u5143b\uff0c\u5c31\u628aLED\u95dc\u9589\uff0c\u5982\u679c\u662f\u5b57\u5143c\u7684\u8a71\uff0c\u624d\u662f\u53bb\u8b80\u53d6\u76ee\u524d\u7684\u6eab\u6fd5\u5ea6\uff0c\u518d\u628a\u8cc7\u6599\u4f9d\u5e8f\u5beb\u56de\u624b\u6a5f\u7684APP\u4e2d\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u65b9\u9762\uff0c\u56de\u5230APP Inventor\u53bb\u4fee\u6539\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u9019\u4e00\u6b21\u6211\u5011\u5728\u624b\u6a5f\u7684\u756b\u9762\u4e2d\u52a0\u4e0a\u5169\u500b\u986f\u793a\u6eab\u6fd5\u5ea6\u7684\u6a19\u7c64\uff0c\u4ee5\u53ca\u4e00\u500b\u8a08\u6642\u5668\uff0c\u756b\u9762\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09_1619.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2261 aligncenter\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09_1619.png\" alt=\"\" width=\"295\" height=\"480\" \/><\/a><\/p>\n<p>\u5143\u4ef6\u6e05\u55ae\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09_1619_001.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2262\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09_1619_001.png\" alt=\"\" width=\"162\" height=\"240\" \/><\/a><\/p>\n<p>\u6b64APP\u7684\u91cd\u9ede\u662f\u591a\u4e86\u4e00\u500b\u8a08\u6642\u5668\uff0c\u4e00\u958b\u59cb\u7684\u6642\u5019\u5148\u8b93\u5b83\u8655\u5728\u4e0d\u662f\u555f\u7528\u7684\u72c0\u614b\uff0c\u6240\u4ee5\u5728\u5c6c\u6027\u7684\u5730\u65b9\u8981\u9019\u6a23\u8a2d\u5b9a\uff1a<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09_1621.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2263\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09_1621.png\" alt=\"\" width=\"182\" height=\"213\" \/><\/a><\/p>\n<p>\u4e26\u8a2d\u5b9a\u5b83\u7684\u8a08\u6642\u9593\u9694\u70ba100\uff0c\u4e5f\u5c31\u662f\u7576\u555f\u7528\u4e4b\u5f8c\uff0c\u53ef\u4ee5\u6bcf0.1\u79d2\u5c31\u89f8\u767c\u4e00\u6b21\uff0c\u76f8\u7576\u65bc\u662f\u6bcf\u79d2\u8b80\u53d610\u6b21\u8cc7\u6599\u3002\u5728Screen1\u521d\u59cb\u5316\u7684\u4e8b\u4ef6\u4e2d\u4e5f\u8981\u9032\u884c\u76f8\u95dc\u7684\u8a2d\u5b9a\uff1a<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/screenshot-2018-06-09-\u4e0b\u534812.06.19.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2253\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/screenshot-2018-06-09-\u4e0b\u534812.06.19.png\" alt=\"\" width=\"394\" height=\"193\" \/><\/a><\/p>\n<p>\u5982\u540c\u4e0a\u4e00\u500b\u4f8b\u5b50\uff0c\u5728\u6e05\u55ae\u9078\u64c7\u5668\u4e4b\u524d\u52a0\u4e0a\u4e00\u500b\u641c\u5c0b\u85cd\u7259\u88dd\u7f6e\u7684\u6307\u4ee4\uff1a<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/screenshot-2018-06-09-\u4e0b\u534812.06.27.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/screenshot-2018-06-09-\u4e0b\u534812.06.27.png\" alt=\"\" width=\"521\" height=\"95\" \/><\/a><\/p>\n<p>\u5728\u9078\u64c7\u8981\u9023\u7dda\u7684\u88dd\u7f6e\u4e4b\u5f8c\uff0c\u9032\u884c\u4ee5\u4e0b\u7684\u76f8\u95dc\u8a2d\u5b9a\uff1a<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/screenshot-2018-06-09-\u4e0b\u534812.06.33.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2255\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/screenshot-2018-06-09-\u4e0b\u534812.06.33.png\" alt=\"\" width=\"540\" height=\"252\" \/><\/a><\/p>\n<p>\u4e0a\u9762\u5c31\u662f\u5728\u9806\u5229\u9023\u7dda\u4e4b\u5f8c\u958b\u555f\u8a08\u6642\u5668\u7684\u529f\u80fd\uff0c\u6b64\u6642\u9664\u4e86\u539f\u6709\u7684\u5169\u500bLED\u958b\u95dc\u7528\u7684\u6309\u9215\u4e4b\u5916\uff1a<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09_1625.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2264\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/2018-06-09_1625.png\" alt=\"\" width=\"304\" height=\"186\" \/><\/a><\/p>\n<p>\u8a08\u6642\u5668\u6bcf\u6b21\u89f8\u767c\u7684\u6642\u5019\u6703\u505a\u4ee5\u4e0b\u7684\u4e8b\u60c5\uff1a<\/p>\n<p><a href=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/screenshot-2018-06-09-\u4e0b\u534812.06.51.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2257 aligncenter\" src=\"https:\/\/104.es\/wp-content\/uploads\/2018\/06\/screenshot-2018-06-09-\u4e0b\u534812.06.51.png\" alt=\"\" width=\"712\" height=\"380\" \/><\/a><\/p>\n<p>\u5728\u7a0b\u5f0f\u4e2d\u6211\u5011\u5c0dArduino\u767c\u9001\u5b57\u5143&#8217;c&#8217;\uff0c\u5982\u4e4b\u524d\u7684Arduino\u7a0b\u5f0f\u4e2d\u7684\u64cd\u4f5c\uff0c\u5728\u6536\u5230&#8217;c&#8217;\u4e4b\u5f8c\u5c31\u6703\u56de\u50b3\u54045\u500b\u5b57\u5143\u7684\u6eab\u6fd5\u5ea6\u5b57\u4e32\uff0c\u56e0\u6b64\u5728\u9019\u88e1\u5c31\u5148\u6aa2\u67e5\u662f\u5426\u6709\u53ef\u4ee5\u6536\u7684\u5b57\u5143\uff0c\u5982\u679c\u6709\u7684\u8a71\uff0c\u5c31\u5206\u5225\u63a5\u6536\u6fd5\u5ea6\u548c\u6eab\u5ea6\u5b57\u5143\uff0c\u7136\u5f8c\u653e\u5230\u6211\u5011\u5728\u756b\u9762\u4e2d\u6e96\u5099\u597d\u7684\u6a19\u7c64\u4e2d\uff0c\u5982\u6b64\u5c31\u5927\u529f\u544a\u6210\u4e86\u3002<\/p>\n<p>\u5982\u540c\u793a\u7bc4\u5f71\u7247\u6240\u793a\u7684\uff0c\u5728\u6b64APP\u4e2d\u4e00\u6a23\u53ef\u4ee5\u958b\u95dcLED\uff0c\u800c\u4e14\u6703\u6bcf0.1\u79d2\u5c31\u5728\u624b\u6a5f\u7684\u756b\u9762\u4e0a\u66f4\u65b0\u76ee\u524d\u7684\u6eab\u6fd5\u5ea6\u503c\u3002\u6709\u4e5f\u9019\u4e9b\u8cc7\u6599\u4e4b\u5f8c\uff0c\u540c\u5b78\u5011\u53ef\u4ee5\u60f3\u60f3\uff0c\u90a3\u5982\u4f55\u628a\u9019\u4e9b\u8cc7\u6599\u5132\u5b58\u5728\u624b\u6a5f\u4e2d\uff0c\u6216\u662f\u76f4\u63a5\u4e0a\u50b3\u5230ThingSpeak\u7db1\u8def\u4e2d\u5462\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e0a\u4e00\u500b\u5be6\u7fd2\u4e2d\u6211\u5011\u5b78\u6703\u4e86\u5982\u4f55\u4f7f\u7528\u624b\u6a5f\u900f\u904e\u85cd\u7259\u63a7\u5236Arduino\u4e0a\u7684LED\uff0c\u5728\u9019\u500b\u5be6\u7fd2\u4e2d\u6211\u5011\u5c07\u518d\u9032\u4e00\u6b65\u4f86\u770b\u770b\uff0c\u5982\u4f55\u900f\u904e\u85cd\u7259\u8b80\u53d6\u5728Arduino\u4e0a\u6eab\u6fd5\u5ea6\u611f\u6e2c\u5668\u4e0a\u7684\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,3],"tags":[],"class_list":["post-2806","post","type-post","status-publish","format-standard","hentry","category-app-inventor","category-arduino"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=2806"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2806\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}