{"id":1496,"date":"2022-05-23T09:48:37","date_gmt":"2022-05-23T01:48:37","guid":{"rendered":"https:\/\/104.es\/?p=1496"},"modified":"2022-05-23T09:48:37","modified_gmt":"2022-05-23T01:48:37","slug":"nodemcu-ssd1306-mqtt-applications","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2022\/05\/23\/nodemcu-ssd1306-mqtt-applications\/","title":{"rendered":"NodeMCU + SSD1306 + MQTT\u6574\u5408\u61c9\u7528"},"content":{"rendered":"\n<p>\u5c0d\u5f88\u591a\u4eba\u4f86\u8aaa\uff0c\u4f7f\u7528\u7269\u806f\u7db2\u88dd\u7f6e\u9023\u63a5\u4e86\u611f\u6e2c\u5668\u4e4b\u5f8c\uff0c\u6700\u91cd\u8981\u7684\u5c31\u662f\u80fd\u5920\u8b93\u611f\u6e2c\u7684\u7d50\u679c\u53ef\u4ee5\u986f\u793a\u51fa\u4f86\uff0c\u540c\u6642\u53ef\u4ee5\u653e\u5230\u7db2\u8def\u4e0a\u4ee5\u4fbf\u5f8c\u7e8c\u7684\u61c9\u7528\u3002\u5728\u672c\u6587\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5c31\u4f86\u5b78\u7fd2\u5982\u4f55\u628a\u6eab\u6fd5\u5ea6\u7684\u611f\u6e2c\u503c\uff0c\u5229\u7528SSD1306\u9019\u500b0.96\u540b\u7684\u5c0f\u986f\u793a\u5668\u628a\u5b83\u986f\u793a\u51fa\u4f86\uff0c\u540c\u6642\u4e5f\u5229\u7528MQTT\u8b93\u9019\u4e9b\u611f\u6e2c\u5230\u7684\u8cc7\u8a0a\u4e5f\u53ef\u4ee5\u50b3\u905e\u5230\u7db2\u969b\u7db2\u8def\u4e0a\u3002<\/p>\n\n\n\n<p>\u5148\u4f86\u770b\u770b\u786c\u9ad4\u88dd\u7f6e\u7684\u9023\u63a5\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"977\" height=\"881\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2022\/05\/image-7.png?w=977\" alt=\"\" class=\"wp-image-1500\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2022\/05\/image-7.png 977w, https:\/\/104.es\/wp-content\/uploads\/2022\/05\/image-7-300x271.png 300w, https:\/\/104.es\/wp-content\/uploads\/2022\/05\/image-7-768x693.png 768w\" sizes=\"auto, (max-width: 977px) 100vw, 977px\" \/><\/figure>\n\n\n\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86DHT11\u7528\u4f86\u611f\u6e2c\u6eab\u6fd5\u5ea6\uff0c\u5b83\u7684\u8cc7\u6599\u7dda\u9023\u63a5\u7684\u662fNoduMCU\u7684d0\uff0c\u63a5\u8457\uff0cSSD1306\u7684SCL\u9023\u63a5\u5230d1\uff0cSDA\u9023\u63a5\u5230d2\uff0c\u5176\u5b83\u7684\u5c31\u53ea\u662f\u96fb\u6e90\u7684\u90e8\u4efd\uff0c\u4f9d\u6b63\u8ca0\u6975\u9023\u63a5\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u7a0b\u5f0f\u78bc\uff0c\u540c\u5b78\u5011\u5728\u7df4\u7fd2\u7684\u6642\u5019\uff0c\u5225\u5fd8\u4e86\u8981\u628a\u81ea\u5df1\u7684\u57fa\u5730\u53f0SSID\u548cpassword\u586b\u5165\uff0c\u624d\u80fd\u5920\u9023\u63a5\u4e0a\u7db2\u969b\u7db2\u8def\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import dht, time, network, ssd1306\nfrom umqtt.simple import MQTTClient\nfrom machine import I2C, Pin\nfrom ntptime import settime\ni2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)\noled = ssd1306.SSD1306_I2C(128, 64, i2c)\nd = dht.DHT11(Pin(16))\nmqttc = MQTTClient(\"umqtt_client\", \"192.168.69.126\")\n\ndef show_message(msg, x, y):\n    oled.fill(0)\n    oled.text(msg, x, y)\n    oled.show()\n\nshow_message(\"Starting...\", 0, 0)\nwlan = network.WLAN(network.STA_IF)\nwlan.active()\nwlan.connect(\"ssid\", \"password\")\nshow_message(\"Connecting...\", 0, 0)\noled.show()\nwhile not wlan.isconnected():\n    pass\nshow_message(\"Connected!\", 0, 0)\n\nwhile True:\n    settime()\n    utc = time.mktime(time.localtime())\n    year, month, day, hour, minute, second, _, _ = time.localtime(utc+28800)\n    d.measure()\n    t = d.temperature()\n    h = d.humidity()\n    oled.fill(0)\n    oled.text(\"NKUST\", 0, 0)\n    oled.text(\"{}-{}-{}\".format(year, month, day), 0, 16)\n    oled.text(\"{}:{}:{}\".format(hour, minute, second), 0, 24)\n    oled.text(\"{},{}%\".format(t, h), 0, 32)\n    oled.show()\n    mqttc.connect()\n    mqttc.publish(b\"nkust\/minhuang\", b\"{}:{}:{}-{},{}%\".format(hour, minute, second, t, h))\n    mqttc.disconnect()\n    \n    time.sleep(2)<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u4e2d\uff0c\u6211\u5011\u7684MQTT\u4f3a\u670d\u5668\u662f192.168.69.126\uff0c\u5b83\u662f\u5229\u7528\u6a39\u8393\u6d3e\u6240\u5efa\u7acb\u7684mosquitto\u4f3a\u670d\u5668\uff0c\u7531\u65bc\u50b3\u9001\u7aef\u548c\u4f3a\u670d\u5668\u662f\u4e0d\u540c\u7684\u6a5f\u5668\uff0c\u6240\u4ee5\u5728\u5efa\u7acbmosquitto\u4f3a\u670d\u5668\u6642\uff0c\u9084\u9700\u8981\u628a\u5b83\u76e3\u807d\u7684IP\u8a2d\u5b9a\u70baanonymous\u624d\u884c\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u5728NodeMCU\u4e0a\u4e5f\u53ef\u4ee5\u4e0d\u9700\u8981\u5b89\u88dd\u4efb\u4f55\u6a21\u7d44\u7684\u65b9\u5f0f\u76f4\u63a5\u767c\u4f48MQTT\u7684\u8a0a\u606f\uff0c\u4f46\u662f\u5982\u679c\u4f60\u5728\u57f7\u884c\u4e0b\u5217\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u6642\u51fa\u73fe\u627e\u4e0d\u5230umqtt\u6a21\u7d44\u7684\u8a0a\u606f\uff0c\u6216\u662f\u4f60\u4f7f\u7528\u7684\u662fNodeMCU\u7684\u8a71\uff0c\u90a3\u9ebc\u53ef\u80fd\u8981\u4ee5\u5982\u4e0b\u6240\u793a\u7684\u6307\u4ee4\uff08\u5728Mu Editor\u7684REPL\u6a21\u5f0f\u6216\u662f\u4ee5PuTTY\u9023\u7dda\u7684\u65b9\u5f0f\u4e0b\u9054\u6307\u4ee4\uff09\u5b89\u88ddumqtt\u6a21\u7d44\uff08\u4e0b\u9762\u7684\u300c&gt;&gt;&gt;\u300d\u662f\u547d\u4ee4\u63d0\u793a\u5b57\u5143\uff0c\u8acb\u52ff\u8f38\u5165\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; import upip\n&gt;&gt;&gt; upip.install(\"micropython-umqtt.simple\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0d\u5f88\u591a\u4eba\u4f86\u8aaa\uff0c\u4f7f\u7528\u7269\u806f\u7db2\u88dd\u7f6e\u9023\u63a5\u4e86\u611f\u6e2c\u5668\u4e4b\u5f8c\uff0c\u6700\u91cd\u8981\u7684\u5c31\u662f\u80fd\u5920\u8b93\u611f\u6e2c\u7684\u7d50\u679c\u53ef\u4ee5\u986f\u793a\u51fa\u4f86\uff0c\u540c\u6642\u53ef\u4ee5\u653e\u5230\u7db2\u8def\u4e0a\u4ee5\u4fbf\u5f8c\u7e8c\u7684\u61c9\u7528\u3002\u5728\u672c\u6587\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5c31\u4f86\u5b78\u7fd2\u5982\u4f55\u628a\u6eab\u6fd5\u5ea6\u7684\u611f\u6e2c\u503c\uff0c\u5229\u7528SSD1306\u9019\u500b0.96\u540b\u7684\u5c0f\u986f\u793a\u5668\u628a\u5b83\u986f\u793a\u51fa\u4f86\uff0c\u540c\u6642\u4e5f\u5229\u7528MQTT\u8b93\u9019\u4e9b\u611f\u6e2c\u5230\u7684\u8cc7\u8a0a\u4e5f\u53ef\u4ee5\u50b3\u905e\u5230\u7db2\u969b\u7db2\u8def\u4e0a\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":[13,15],"tags":[45,56],"class_list":["post-1496","post","type-post","status-publish","format-standard","hentry","category-nodemcu","category-python","tag-mqtt","tag-ssd1306"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1496","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=1496"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1496\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=1496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=1496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=1496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}