{"id":1930,"date":"2022-12-27T18:16:50","date_gmt":"2022-12-27T10:16:50","guid":{"rendered":"https:\/\/104.es\/?p=1930"},"modified":"2023-11-22T09:33:44","modified_gmt":"2023-11-22T01:33:44","slug":"raspberry-pi-pico-w-mqtt","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2022\/12\/27\/raspberry-pi-pico-w-mqtt\/","title":{"rendered":"Raspberry Pi Pico W + MQTT"},"content":{"rendered":"\n<p>\u53ef\u4ee5\u9023\u4e0a\u7db2\u8def\u7684\u7269\u806f\u7db2\u88dd\u7f6e\uff0c\u4e0b\u4e00\u6b65\u7576\u7136\u662f\u8981\u5229\u7528\u7db2\u8def\u529f\u80fd\uff0c\u900f\u904eMQTT\u628a\u611f\u6e2c\u5230\u7684\u8cc7\u6599\u50b3\u905e\u51fa\u4f86\uff0c\u624d\u80fd\u53eb\u505a\u7269\u806f\u7db2\u88dd\u7f6e\u554a\u3002\u5728Pico W\u4e0a\u4f7f\u7528\u7269\u806f\u7db2\u4e5f\u975e\u5e38\u5bb9\u6613\uff0c\u73fe\u5728\u5c31\u4f86\u770b\u770b\u5982\u4f55\u4f7f\u7528\u5427\u3002<\/p>\n\n\n\n<p>\u540c\u6a23\u5730\uff0cMQTT\u7684\u9023\u7dda\u51fd\u5f0f\u6211\u5011\u4e5f\u53ea\u8981\u4f7f\u7528\u73fe\u6210\u7684\uff0c\u5728\u4e0b\u9762\u9019\u500b\u7db2\u7ad9\u4e2d\u53ef\u4ee5\u627e\u5230\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-peppe-8-o wp-block-embed-peppe-8-o\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"8S0AHvKzNN\"><a href=\"https:\/\/peppe8o.com\/mqtt-and-raspberry-pi-pico-w-start-with-mosquitto-micropython\/\">MQTT and Raspberry PI Pico W: Start with Mosquitto (MicroPython)<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;MQTT and Raspberry PI Pico W: Start with Mosquitto (MicroPython)&#8221; &#8212; peppe8o\" src=\"https:\/\/peppe8o.com\/mqtt-and-raspberry-pi-pico-w-start-with-mosquitto-micropython\/embed\/#?secret=zLB2HKdXOv#?secret=8S0AHvKzNN\" data-secret=\"8S0AHvKzNN\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\u4ed4\u7d30\u770b\u7db2\u9801\u4e2d\u7684\u5167\u5bb9\uff0c\u5176\u4e2d\u6709\u4e00\u500bumqttsimple.py\u7684\u4e0b\u8f09\u9023\u7d50\uff0c\u628a\u8a72\u7a0b\u5f0f\u4e0b\u8f09\uff0c\u7136\u5f8c\u5229\u7528Thonny\u5132\u5b58\u5230Pico W\u7684\u677f\u5b50\u4e2d\uff0c\u5982\u4e0b\u5716\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"803\" height=\"411\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2022\/12\/image-23.png?w=803\" alt=\"\" class=\"wp-image-1934\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2022\/12\/image-23.png 803w, https:\/\/104.es\/wp-content\/uploads\/2022\/12\/image-23-300x154.png 300w, https:\/\/104.es\/wp-content\/uploads\/2022\/12\/image-23-768x393.png 768w\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" \/><\/figure>\n\n\n\n<p>\u7136\u5f8c\uff0c\u5229\u7528\u4ee5\u4e0b\u7684\u7a0b\u5f0f\uff0c\u5c31\u53ef\u4ee5\u8f15\u6613\u5730\u628a\u8cc7\u6599\u50b3\u9001\u51fa\u53bb\u4e86\uff08\u6b64\u7a0b\u5f0f\u9700\u8981\u5728\u6709\u7db2\u8def\u9023\u7dda\u7684\u60c5\u6cc1\u4e0b\u624d\u80fd\u5920\u9806\u5229\u57f7\u884c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">from umqttsimple import MQTTClient\n\nmqtt = MQTTClient(\"00001\", \"your ip of mqtt server\")\nmqtt.connect()\nmqtt.publish(\"minhuang\/picow\", \"Hi\")<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5728Raspberry Pi\u4e2d\u57f7\u884cmosquitto\u7684subscribe\u7a0b\u5f0f\u6240\u63a5\u6536\u5230\u7684\u5167\u5bb9\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"748\" height=\"91\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2022\/12\/image-24.png?w=748\" alt=\"\" class=\"wp-image-1936\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2022\/12\/image-24.png 748w, https:\/\/104.es\/wp-content\/uploads\/2022\/12\/image-24-300x36.png 300w\" sizes=\"auto, (max-width: 748px) 100vw, 748px\" \/><\/figure>\n\n\n\n<p>\u6709\u4e86\u9019\u500bMQTT\u7684\u767c\u4f48\u7a0b\u5f0f\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u628a\u76ee\u524d\u611f\u6e2c\u5668\u4e2d\u53d6\u5f97\u7684\u8cc7\u6599\uff0c\u5229\u7528MQTT\u50b3\u905e\u51fa\u53bb\uff0c\u5982\u679c\u9019\u500b\u7a0b\u5f0f\u628a\u5b83\u547d\u540d\u70bamain.py\uff0c\u90a3\u9ebc\u5b83\u5c31\u53ef\u4ee5\u4e86\u901a\u96fb\u4e4b\u5f8c\u81ea\u884c\u904b\u4f5c\u4e86\u3002<\/p>\n\n\n\n<p>\u7531\u65bc\u4f7f\u7528MQTT\u4e00\u5b9a\u8981\u5728\u7db2\u8def\u662f\u53ef\u4ee5\u6b63\u5e38\u9023\u7dda\u7684\u60c5\u6cc1\u4e0b\u624d\u80fd\u5920\u904b\u4f5c\uff0c\u6240\u4ee5\u4e00\u822c\u4f86\u8aaa\uff0c\u6211\u5011\u6703\u628a\u9023\u7db2\u7684\u7a0b\u5f0f\u78bc\u4e00\u4f75\u653e\u5728\u767c\u4f48\u7684\u7a0b\u5f0f\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">import network\nfrom umqttsimple import MQTTClient\n\nwlan = network.WLAN(network.STA_IF)\nwlan.active(True)\nwlan.connect(\"ssid\", \"password\")\nprint(\"connecting...\")\nwhile not wlan.isconnected():\n    pass\nprint(\"connected\")\nmqtt = MQTTClient(\"00001\", \"your mqtt broker IP address\")\nmqtt.connect()\nmqtt.publish(\"minhuang\/picow\", \"Hi\")\n<\/code><\/pre>\n\n\n\n<p>\u7d50\u5408\u524d\u4e00\u7bc7\u6587\u7ae0\u7684SSD1306\u4ee5\u53ca\u6eab\u6fd5\u5ea6\u611f\u6e2c\u5668\uff0c\u7a0b\u5f0f\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">import network\nimport time, ntptime\nfrom machine import Pin, I2C\nfrom dht import DHT11\nimport ssd1306\nfrom umqttsimple import MQTTClient\n\npin = Pin(22, Pin.OUT, Pin.PULL_UP)\nsensor = DHT11(pin)\ni2c = I2C(0, sda=Pin(16), scl=Pin(17))\ndisp = ssd1306.SSD1306_I2C(128, 64, i2c)\nwlan = network.WLAN(network.STA_IF)\nwlan.active(True)\nwlan.connect(\"ssid\", \"password\")\ndisp.fill(0)\ndisp.text(\"connecting...\", 0, 0)\ndisp.show()\nwhile not wlan.isconnected():\n    pass\nmqtt = MQTTClient(\"0001\", \"your MQTT borker's IP\")\nmqtt.connect()\ndisp.fill(0)\ndisp.text(\"connected\", 0, 0)\ndisp.show()\ntry:\n    ntptime.settime()\nexcept:\n    pass\nwhile True:\n    temp = time.mktime(time.localtime())\n    temp += 8*3600\n    y, m, d, h, M, s, _, _ = time.localtime(temp)\n    try:   \n        sensor.measure()\n        temperature  = (sensor.temperature)\n        humidity = (sensor.humidity)\n        mqtt.publish(\"minhuang\/picow\/temperature\", str(temperature))\n        mqtt.publish(\"minhuang\/picow\/humidity\", str(humidity))\n        disp.fill(0)\n        disp.text('NKUST CCET', 0, 0)\n        disp.text('Min-Huang Ho', 0, 10)\n        disp.text('{}-{}-{}'.format(y, m, d), 0, 25)\n        disp.text('{}:{}:{}'.format(h, M, s), 0, 40)\n        disp.text('{}, {}%'.format(temperature, humidity), 0, 55)\n    except:\n        pass\n    disp.show()\n    time.sleep(1)\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e0a\u8ff0\u7a0b\u5f0f\u57f7\u884c\u4e4b\u5f8c\uff0c\u6211\u5011\u5728Raspberry Pi\u4e0a\u4ee5mosquitto_sub\u63a5\u6536\u7684\u6210\u679c\u756b\u9762\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"890\" height=\"169\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2022\/12\/image-25.png?w=890\" alt=\"\" class=\"wp-image-1945\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2022\/12\/image-25.png 890w, https:\/\/104.es\/wp-content\/uploads\/2022\/12\/image-25-300x57.png 300w, https:\/\/104.es\/wp-content\/uploads\/2022\/12\/image-25-768x146.png 768w\" sizes=\"auto, (max-width: 890px) 100vw, 890px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u9023\u4e0a\u7db2\u8def\u7684\u7269\u806f\u7db2\u88dd\u7f6e\uff0c\u4e0b\u4e00\u6b65\u7576\u7136\u662f\u8981\u5229\u7528\u7db2\u8def\u529f\u80fd\uff0c\u900f\u904eMQTT\u628a\u611f\u6e2c\u5230\u7684\u8cc7\u6599\u50b3\u905e\u51fa\u4f86\uff0c\u624d\u80fd\u53eb\u505a\u7269\u806f\u7db2\u88dd\u7f6e\u554a\u3002\u5728Pico W\u4e0a\u4f7f\u7528\u7269\u806f\u7db2\u4e5f\u975e\u5e38\u5bb9\u6613\uff0c\u73fe\u5728\u5c31\u4f86\u770b\u770b\u5982\u4f55\u4f7f\u7528\u5427\u3002<\/p>\n","protected":false},"author":1,"featured_media":1862,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1930","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-rasbperry-pi-pico"],"jetpack_featured_media_url":"https:\/\/104.es\/wp-content\/uploads\/2022\/12\/image-3.png","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1930","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=1930"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1930\/revisions"}],"predecessor-version":[{"id":2976,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1930\/revisions\/2976"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/1862"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=1930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=1930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=1930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}