{"id":2885,"date":"2023-08-06T18:18:17","date_gmt":"2023-08-06T10:18:17","guid":{"rendered":"https:\/\/104.es\/?p=2885"},"modified":"2023-08-06T18:18:18","modified_gmt":"2023-08-06T10:18:18","slug":"nodemcu-post-data-to-thingspeak","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2023\/08\/06\/nodemcu-post-data-to-thingspeak\/","title":{"rendered":"\u3010NodeMCU\u3011\u628a\u8cc7\u6599\u50b3\u9001\u5230ThingSpeak.com"},"content":{"rendered":"\n<p>\u5728\u524d\u4e00\u500b\u55ae\u5143\u4e2d\uff0c\u6211\u5011\u5df2\u7d93\u70baNodeMCU\u52a0\u4e0a\u7db2\u8def\u9023\u7dda\u7684\u80fd\u529b\u4e86\uff0c\u73fe\u5728\uff0c\u662f\u6642\u5019\u628aNodeMCU\u6240\u5075\u6e2c\u5230\u7684\u611f\u6e2c\u5668\u8cc7\u6599\u900f\u904e WiFi\u7db2\u8def\u50b3\u9001\u5230\u7db2\u8def\u4e0a\u7684\u4e3b\u6a5f\u4e86\u3002<\/p>\n\n\n\n<p>\u5728\u524d\u4e00\u500b\u55ae\u5143\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 network \u4e2d\u7684\u51fd\u6578\u53ef\u4ee5\u8b93NodeMCU\u9023\u7dda\u5230\u7121\u7dda\u57fa\u5730\u53f0\uff0c\u9019\u8868\u793aNodeMCU\u5df2\u5177\u6709\u53ef\u4ee5\u9023\u7dda\u4e0a\u7db2\u7684\u80fd\u529b\u4e86\u3002\u5728\u9019\u500b\u55ae\u5143\u4e2d\uff0c\u6211\u5011\u6253\u7b97\u5229\u7528\u9019\u500b\u7db2\u8def\u7684\u9023\u7dda\u80fd\u529b\uff0c\u628a\u91cf\u6e2c\u5230\u7684\u8cc7\u6599\u4e0a\u50b3\u5230\u8457\u540d\u7684ThingSpeak.com\u7684\u4f3a\u670d\u5668\u4e0a\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u8b93\u6211\u5011\u4f86\u4fee\u6539\u4e00\u4e0bNodeMCU\u7684boot.py\u7684\u5167\u5bb9\uff0c\u8b93\u6b64NodeMCU\u7684\u677f\u5b50\u80fd\u5920\u5728\u4e00\u63a5\u4e0a\u96fb\u6e90\u7684\u6642\u5019\u5c31\u53ef\u4ee5\u99ac\u4e0a\u9023\u4e0aWiFi\u57fa\u5730\u53f0\uff0c\u4ee5\u4e0b\u662fboot.py\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># This file is executed on every boot (including wake-boot from deepsleep)\r\nimport esp\r\nesp.osdebug(None)\r\nimport gc\r\nimport network\r\n\r\ndef connectAP(ssid, pwd):\r\n    wlan = network.WLAN(network.STA_IF)\r\n    if not wlan.isconnected():\r\n        wlan.active(True)\r\n        wlan.connect(ssid, pwd)\r\n\r\n        while not wlan.isconnected():\r\n            pass\r\n\r\nconnectAP(\"YourSSID\", \"YourSSIDPWD\")\r\n#import webrepl\r\n#webrepl.start()\r\ngc.collect()\r\n<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c\u7b2c16\u884c\u9700\u8981\u8f38\u5165\u7121\u7dda\u57fa\u5730\u53f0\u7684SSID\u4ee5\u53ca\u9023\u7dda\u5bc6\u78bc\u3002\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u6a94\u6848\u662fboot.py\uff0c\u9700\u8981\u900f\u904e\u4ee5\u4e0b\u7684\u6307\u4ee4\u4e0a\u50b3\u5230NodeMCU\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ampy --port COM3 get boot.py<\/code><\/pre>\n\n\n\n<p>\u4e0a\u50b3\u4e4b\u5f8c\u53ea\u8981\u7a0b\u5f0f\u80fd\u5920\u9806\u5229\u5730\u57f7\u884c\uff0c\u7576\u4e0b\u6b21\u6211\u5011\u4ee5PuTTY\u9023\u7dda\u5230NodeMCU\u7684\u6642\u5019\uff0c\u5728\u4e00\u9032\u5165\u4ecb\u9762\u6642\u5c31\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7684\u6307\u4ee4\u67e5\u8a62\u76ee\u524d\u7684\u9023\u7dda\u72c0\u6cc1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> wlan = network.WLAN(network.STA_IF)\r\n>>> wlan.status()\r\n5\r\n>>> wlan.ifconfig()\r\n('192.168.43.127', '255.255.255.0', '192.168.43.1', '192.168.43.1')<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u6709\u770b\u5230IP\u4f4d\u5740\uff0c\u90a3\u5c31\u8868\u793a\u53ef\u4ee5\u9806\u5229\u5730\u9023\u4e0a\u57fa\u5730\u53f0\uff0c\u4e0a\u7db2\u7576\u7136\u5c31\u6c92\u6709\u554f\u984c\u4e86\u3002ThingSpeak\u5e33\u865f\u7684\u7533\u8acb\uff0c\u8acb\u53c3\u8003\u300c<a href=\"https:\/\/104.es\/index.php\/2023\/07\/30\/thingspeak-registration\/\" data-type=\"post\" data-id=\"2437\">\u9019\u7bc7\u6587\u7ae0<\/a>\u300d\u3002<\/p>\n\n\n\n<p>\u5728\u5efa\u7acb\u597d\u5e33\u865f\u53caChannel\u4e4b\u5f8c\uff0c\u6703\u5728\u7db2\u9801\u4e0a\u5f97\u5230\u4e00\u500b\u50cf\u662f\u4e0b\u9762\u9019\u6a23\u7684\u7db2\u5740\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET https:\/\/api.thingspeak.com\/update?api_key=6JQFR*******TB&amp;field1=0<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2dGET\u6307\u7684\u662f\u5354\u5b9a\uff0c\u5728\u7a0b\u5f0f\u4e2d\u6211\u5011\u53ea\u8981\u4f7f\u7528\u5f8c\u9762\u7684\u7db2\u5740\u5c31\u53ef\u4ee5\u4e86\uff0c\u800cfield1\u6307\u7684\u662f\u7b2c\u4e00\u500b\u6b04\u4f4d\uff0c\u800cfield2\u5247\u662f\u7b2c\u4e8c\u500b\u6b04\u4f4d\u7684\u8cc7\u6599\u9805\u76ee\uff0c\u4f9d\u6b64\u985e\u63a8\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u7684\u7db2\u5740\u5176\u5be6\u5c31\u662f\u4e00\u500b\u89f8\u767c\u7684\u52d5\u4f5c\uff0c\u4e00\u822c\u4f86\u8aaa\u53ea\u8981\u4f7f\u7528\u700f\u89bd\u5668\u53bb\u5b58\u53d6\u9019\u500b\u7db2\u5740\u5c31\u53ef\u4ee5\u66f4\u65b0\u5728\u7db2\u7ad9\u4f3a\u670d\u5668\u4e0a\u7684\u8cc7\u6599\u5eab\u5167\u5bb9\uff0c\u96d6\u7136\u6211\u5011\u5728NodeMCU\u4e2d\u6c92\u6709\u700f\u89bd\u5668\u53ef\u4ee5\u7528\uff0c\u4f46\u662f\u537b\u6709\u4e00\u500b\u7a0b\u5f0f\u5eab\u53ef\u4ee5\u5e6b\u6211\u5011\u505a\u985e\u4f3c\u700f\u89bd\u5668\u53bb\u7db2\u8def\u53d6\u5f97\u7db2\u5740\u6240\u63d0\u4f9b\u7684\u8cc7\u6599\u7684\u52d5\u4f5c\uff0c\u9019\u500b\u7a0b\u5f0f\u5eab\u5c31\u662furequests\u6a21\u7d44\uff0c\u5728\u9019\u88e1\u6211\u5011\u4f7f\u7528\u7684\u662f\u5b83\u7684get\u51fd\u6578\uff0c\u7bc4\u4f8b\u7a0b\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from machine import Pin, Timer\r\nimport dht\r\nimport urequests as req\r\n\r\ndef m(t):\r\n    d.measure()\r\n    url = \"https:\/\/api.thingspeak.com\/update?api_key={}&amp;field1={}&amp;field2={}\".format(\r\n        \"6JQFRE******TB\", \r\n        d.temperature(), d.humidity())\r\n    req.get(url)\r\n\r\nd = dht.DHT22(Pin(5))\r\nt = Timer(0)\r\nt.init(period=60000, mode=Timer.PERIODIC, callback=m)\r\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u8acb\u5b58\u6210main.py\uff0c\u7136\u5f8c\u4e0a\u50b3\u5230NodeMCU\u5373\u53ef\u3002\u7576\u7136\uff0c\u7b2c8\u884c\u7684API Key\u8981\u4f7f\u7528\u7684\u662f\u4f60\u81ea\u5df1\u7533\u8acb\u7684Channel\u4e2d\u7684API\u624d\u884c\u3002<\/p>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u6bcf\u96941\u5206\u9418\u5373\u6703\u66f4\u65b0\u4e00\u7b46\u8cc7\u6599\u5230ThingSpeak\u7db2\u7ad9\uff0c\u904e\u4e86\u4e00\u6bb5\u6642\u9593\u4e4b\u5f8c\u7684Channel\u5716\u5f62\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"417\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-57-1024x417.png\" alt=\"\" class=\"wp-image-2886\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-57-1024x417.png 1024w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-57-300x122.png 300w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-57-768x313.png 768w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-57-600x244.png 600w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-57.png 1372w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u4e00\u500b\u55ae\u5143\u4e2d\uff0c\u6211\u5011\u5df2\u7d93\u70baNodeMCU\u52a0\u4e0a\u7db2\u8def\u9023\u7dda\u7684\u80fd\u529b\u4e86\uff0c\u73fe\u5728\uff0c\u662f\u6642\u5019\u628aNodeMCU\u6240\u5075\u6e2c\u5230\u7684\u611f\u6e2c\u5668\u8cc7\u6599\u900f\u904e WiFi\u7db2\u8def\u50b3\u9001\u5230\u7db2\u8def\u4e0a\u7684\u4e3b\u6a5f\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":2886,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,15],"tags":[],"class_list":["post-2885","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodemcu","category-python"],"jetpack_featured_media_url":"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-57.png","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2885","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=2885"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2885\/revisions"}],"predecessor-version":[{"id":2887,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2885\/revisions\/2887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/2886"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}