{"id":2879,"date":"2023-08-06T18:14:09","date_gmt":"2023-08-06T10:14:09","guid":{"rendered":"https:\/\/104.es\/?p=2879"},"modified":"2023-08-06T18:14:11","modified_gmt":"2023-08-06T10:14:11","slug":"nodemcu-periodic-measurement","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2023\/08\/06\/nodemcu-periodic-measurement\/","title":{"rendered":"\u3010NodeMCU\u3011\u8a08\u6642\u5668\u8207\u611f\u6e2c\u8cc7\u6599\u7684\u5b9a\u671f\u91cf\u6e2c"},"content":{"rendered":"\n<p>\u5c0d\u65bc\u4e00\u500b\u611f\u6e2c\u5668\u7684\u6a21\u7d44\u4f86\u8aaa\uff0c\u80fd\u5920\u5b9a\u6642\u5730\u91cf\u6e2c\u8cc7\u8a0a\u662f\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u4e4b\u4e00\u3002\u5728\u9019\u4e00\u500b\u55ae\u5143\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528MicroPython \u4e2d\u7684\u8a08\u6642\u5668\u529f\u80fd\uff0c\u8b93NodeMCU\u53ef\u4ee5\u8a02\u6642\u5730\u91cf\u6e2c\u611f\u6e2c\u5668\u4e0a\u7684\u8cc7\u8a0a\uff0c\u4e26\u4e14\u628a\u5b83\u5011\u90fd\u8a18\u9304\u4e0b\u4f86\u3002<\/p>\n\n\n\n<p>\u70ba\u4e86\u6e2c\u8a66\u8a08\u6642\u5668\u529f\u80fd\uff0c\u8acb\u5148\u4f7f\u7528NodeMCU\u4ee5\u53ca\u64f4\u5145\u677f\uff0c\u9023\u63a52\u500b\u7c21\u55ae\u7684LED\u8f38\u51fa\u3002\u70ba\u4e86\u65b9\u4fbf\u8d77\u898b\uff0c\u5728\u9019\u88e1\u6211\u5011\u5c31\u4e0d\u53e6\u5916\u63a52\u500b\u4e0d\u540c\u7684LED\uff0c\u800c\u662f\u5229\u7528\u5167\u5efa\u6709\u9650\u6d41\u96fb\u963b\u7684RGB LED\u4f86\u505a\u7df4\u7fd2\uff0c\u63a5\u7dda\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"604\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-53.png\" alt=\"\" class=\"wp-image-2880\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-53.png 800w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-53-300x227.png 300w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-53-768x580.png 768w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-53-600x453.png 600w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>\u6211\u5011\u628aNodeMCU\u7684D1\u63a5\u4e0aLED\u7684R\u7aef\uff0cD2\u63a5\u5230LED\u7684B\u7aef\uff0c\u7136\u5f8c\u628aNodeMCU\u7684GND\u9023\u63a5\u5230LED\u7684\u8ca0\u6975\u3002\u4ed4\u7d30\u770bLED\u7aef\u7684\u9023\u63a5\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-54.png\" alt=\"\" class=\"wp-image-2881\" width=\"362\" height=\"200\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-54.png 600w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-54-300x166.png 300w\" sizes=\"auto, (max-width: 362px) 100vw, 362px\" \/><\/figure>\n<\/div>\n\n\n<p>\u800c\u5728NodeMCU\u7aef\u7684\u9023\u7dda\u5247\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"450\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-55.png\" alt=\"\" class=\"wp-image-2882\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-55.png 800w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-55-300x169.png 300w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-55-768x432.png 768w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-55-600x338.png 600w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>\u9023\u63a5\u5b8c\u6210\u4e4b\u5f8c\uff0c\u8acb\u628a\u6b64NodeMCU\u9023\u63a5\u4e0a\u96fb\u8166\uff0c\u7136\u5f8c\u4f7f\u7528\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u78bc\u6e2c\u8a66\u4e00\u4e0b\u662f\u5426\u80fd\u5920\u6b63\u5e38\u5730\u4f9d\u5e8f\u9ede\u4eae\u4e26\u7184\u6ec5\u7d05\u8272\u548c\u85cd\u8272\u7684\u71c8\u865f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from machine import Pin\r\nimport time\r\n\r\nledR = Pin(5, Pin.OUT)\r\nledB = Pin(4, Pin.OUT)\r\n\r\nfor i in range(10):\r\n    ledR.on()\r\n    time.sleep(0.5)\r\n    ledR.off()\r\n    ledB.on()\r\n    time.sleep(0.5)\r\n    ledB.off()<\/code><\/pre>\n\n\n\n<p>\u57f7\u884c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"NodeMCU \u7d05\u85cdLED\u9583\u720d\u6e2c\u8a66\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/tvXYPQ2qJr0?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>\n<\/div><\/figure>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u53ef\u4ee5\u9806\u5229\u5730\u57f7\u884c\u71c8\u5149\u9583\u720d\u7684\u52d5\u4f5c\uff0c\u4f46\u662f\u5982\u679c\u540c\u5b78\u5229\u7528REPL\u7684\u65b9\u5f0f\u57f7\u884c\u6b64\u7a0b\u5f0f\u7684\u6642\u5019\u6703\u767c\u73fe\uff0c\u56e0\u70ba\u662f\u4f7f\u7528for\u8ff4\u5708\u5728\u5c08\u5fc3\u5730\u57f7\u884c\u9583\u720d\u7684\u52d5\u4f5c\uff0c\u6b64\u6642\u5728\u8ff4\u5708\u4e4b\u5916\u4e26\u6c92\u6709\u8fa6\u6cd5\u505a\u4e9b\u4ec0\u9ebc\u5176\u5b83\u7684\u4e8b\u3002<\/p>\n\n\n\n<p>\u70ba\u4e86\u907f\u514d\u6b64\u7a2e\u5b9a\u671f\u57f7\u884c\u7684\u4f5c\u696d\u4f54\u7528\u7cfb\u7d71\u8cc7\u6e90\u7684\u60c5\u5f62\u767c\u751f\uff0c\u6211\u5011\u9700\u8981\u8a08\u6642\u5668\u7684\u5e6b\u5fd9\u3002<\/p>\n\n\n\n<p>\u8a08\u6642\u5668\u7684\u6a21\u7d44\u662fTimer\uff0c\u5b83\u6700\u91cd\u8981\u7684\u5169\u500b\u51fd\u6578\u662f init()\u4ee5\u53cadeinit()\u3002\u5176\u4e2dinit()\u51fd\u6578\u662f\u7528\u4f86\u8a2d\u5b9a\u8a08\u6642\u5668\u7684\u51fd\u6578\uff0c\u6b64\u51fd\u6578\u9700\u89813\u500b\u53c3\u6578\uff0c\u5206\u5225\u662f\u5b9a\u6642\u7684\u9031\u671f\uff08\u4ee5\u6beb\u79d2\u70ba\u55ae\u4f4d\uff09\u3001\u57f7\u884c\u7684\u6a21\u5f0f\uff08\u53ea\u57f7\u884c\u4e00\u6b21Timer.ONE_SHOT\u6216\u9031\u671f\u6027\u7684\u5faa\u74b0Timer.PERIODIC\uff09\u3001\u4ee5\u53ca\u56de\u547c\u51fd\u6578\uff0c\u4e5f\u5c31\u662f\u6642\u9593\u5230\u4e86\u8981\u57f7\u884c\u7684\u662f\u54ea\u4e00\u500b\u51fd\u6578\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u8a08\u6642\u5668\u7684\u65b9\u5f0f\u4f86\u5be6\u4f5c\u7d05\u85cdLED\u9583\u720d\u7684\u7a0b\u5f0f\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from machine import Pin, Timer\r\n\r\ndef blink(t):\r\n    ledR.value(not ledR.value())\r\n    ledB.value(not ledB.value())\r\n\r\nledR = Pin(5, Pin.OUT)\r\nledB = Pin(4, Pin.OUT)\r\n\r\nledR.on()\r\nledB.off()\r\n\r\nt = Timer(0)\r\nt.init(period=200, mode=Timer.PERIODIC, callback=blink)<\/code><\/pre>\n\n\n\n<p>\u6709\u4e86\u8a08\u6642\u5668\u4e4b\u5f8c\uff0c\u63a5\u8457\u6211\u5011\u5c31\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u5b9a\u671f\u91cf\u6e2c\u611f\u6e2c\u5668\u6578\u503c\uff0c\u7136\u5f8c\u628a\u5b83\u8a18\u9304\u4e0b\u4f86\u3002\u9996\u5148\uff0c\u8acb\u5148\u518d\u4e00\u6b21\u9023\u63a5\u6eab\u6fd5\u5ea6\u611f\u6e2c\u5668\uff0c\u9019\u4e00\u6b21\u6211\u5011\u4f7f\u7528\u662fDHT22\uff0c\u9023\u63a5\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-56.png\" alt=\"\" class=\"wp-image-2883\" width=\"378\" height=\"326\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-56.png 800w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-56-300x259.png 300w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-56-768x663.png 768w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-56-600x518.png 600w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/figure>\n<\/div>\n\n\n<p>\u5982\u4e0a\u5716\u6240\u793a\uff0cDHT22\u5728\u9019\u500b\u65b9\u5411\u7684\u6700\u5de6\u908a\u662fPIN1\uff0c\u8acb\u628a\u5b83\u9023\u63a5\u5230\u6b63\u6975\uff085V\uff09\uff0c\u6700\u53f3\u908a\u662fPIN4\uff0c\u8acb\u628a\u5b83\u9023\u63a5\u5230\u8ca0\u6975\uff08GND\uff09\u3002PIN3\u8acb\u7559\u7a7a\uff0c\u81f3\u65bcPIN2\u5728\u9023\u63a5\u4e4b\u524d\uff0c\u8acb\u5148\u9023\u63a5\u4e00\u500b5K\u6b50\u59c6\u7684\u96fb\u963b\uff0c\u518d\u9023\u63a5\u5230\u6b63\u6975\uff0c\u540c\u6642\u4e5f\u8981\u5f9ePIN2\u8655\u9023\u63a5\u4e00\u689d\u7dda\u5230NodeMCU\u7684D1\u3002<\/p>\n\n\n\n<p>\u9023\u63a5\u5b8c\u6210\u4e4b\u5f8c\u53ef\u4ee5\u4f7f\u7528\u9032\u5165REPL\uff0c\u4f7f\u7528\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u6e2c\u8a66\u4e00\u4e0b\u76ee\u524d\u7684\u6eab\u6fd5\u5ea6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> import dht\r\n>>> from machine import Pin\r\n>>> d = dht.DHT22(Pin(5))\r\n>>> d.measure()\r\n>>> d.temperature()\r\n31.1\r\n>>> d.humidity()\r\n64.8<\/code><\/pre>\n\n\n\n<p>\u6e2c\u8a66\u5b8c\u6210\u4e4b\u5f8c\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5229\u7528\u8a08\u6642\u5668\u4f86\u5b8c\u6210\u5b9a\u6642\u91cf\u6e2c\u7684\u5de5\u4f5c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from machine import Pin, Timer\r\nimport dht\r\n\r\ndef m(t):\r\n    d.measure()\r\n    print(\"{},{}\".format(d.temperature(), d.humidity()))\r\n\r\nd = dht.DHT22(Pin(5))\r\nt = Timer(0)\r\nt.init(period=50000, mode=Timer.PERIODIC, callback=m)\r\ntry:\r\n    pass\r\nexcept:\r\n    t.dinit()<\/code><\/pre>\n\n\n\n<p>\u6b64\u7a0b\u5f0f\u5728\u8f38\u5165\u5b8c\u7562\u4e4b\u5f8c \u6bcf\u96945\u79d2\u5c31\u6703\u986f\u793a\u4e00\u6b21\u76ee\u524d\u7684\u6eab\u6fd5\u5ea6\u6578\u503c\uff0c\u76f4\u5230\u4f60\u6309\u4e0bCtrl-Break\u6216\u662f\u6309\u4e0b\u677f\u5b50\u4e0a\u7684Reset\u6309\u9215\u70ba\u6b62\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0d\u65bc\u4e00\u500b\u611f\u6e2c\u5668\u7684\u6a21\u7d44\u4f86\u8aaa\uff0c\u80fd\u5920\u5b9a\u6642\u5730\u91cf\u6e2c\u8cc7\u8a0a\u662f\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u4e4b\u4e00\u3002\u5728\u9019\u4e00\u500b\u55ae\u5143\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528MicroPython \u4e2d\u7684\u8a08\u6642\u5668\u529f\u80fd\uff0c\u8b93NodeMCU\u53ef\u4ee5\u8a02\u6642\u5730\u91cf\u6e2c\u611f\u6e2c\u5668\u4e0a\u7684\u8cc7\u8a0a\uff0c\u4e26\u4e14\u628a\u5b83\u5011\u90fd\u8a18\u9304\u4e0b\u4f86\u3002<\/p>\n","protected":false},"author":1,"featured_media":2883,"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-2879","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-56.png","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2879","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=2879"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2879\/revisions"}],"predecessor-version":[{"id":2884,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2879\/revisions\/2884"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/2883"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}