{"id":103,"date":"2020-10-21T18:50:55","date_gmt":"2020-10-21T09:50:55","guid":{"rendered":"http:\/\/practical.kr\/?p=103"},"modified":"2020-10-21T21:04:32","modified_gmt":"2020-10-21T12:04:32","slug":"esp32-micropython-mqtt","status":"publish","type":"post","link":"http:\/\/practical.kr\/?p=103","title":{"rendered":"ESP32, MicroPython, MQTT"},"content":{"rendered":"\n<p>\uc544\ub798\uc640 \uac19\uc740 \uc0bd\uc9c8\uc744 \ud588\ub2e4. \uc0bd\uc9c8\uc740 \uae30\ub85d\uc774\ub77c\uace0&#8230; \ub300\ucda9 \uc815\ub9ac\ud568<\/p>\n\n\n\n<p>ESP32\uc5d0\uc11c MicroPython\uc744 \uc5b8\uc820\uac00 \ud55c\ubc88 \uc0ac\uc6a9\ud574\ubd10\uc57c\uc9c0 \ud558\uace0 \uc788\uc5c8\ub294\ub370 \uae30\uc655 \ud558\ub294 \uae40\uc5d0 MQTT\uae4c\uc9c0  \uc5f0\uacb0\ud574\ubd24\ub2e4.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/practical.kr\/wp-content\/uploads\/2020\/10\/NODEMCU-ESP32.jpg\" alt=\"\" class=\"wp-image-105\" width=\"297\" height=\"297\"\/><\/figure><\/div>\n\n\n\n<ol class=\"wp-block-list\"><li>ESP32 \uc5d0 MicroPython\uc744 \uc124\uce58\ud55c\ub2e4.<\/li><li>ESP32\uc5d0\uc11c MicroPython\uc73c\ub85c MQTT Client\ub97c \ub9cc\ub4e0\ub2e4<\/li><li>\ub9e5\ubd81\uc5d0 MQTT \uc11c\ubc84\ub97c \uc124\uce58\ud55c\ub2e4 <\/li><li>\ub9e5\ubd81\uc5d0\uc11c Python\uc744 \uc774\uc6a9\ud574\uc11c MQTT Client\ub97c \ub9cc\ub4e0\ub2e4.<\/li><li>ESP32 &#8211; \ub9e5\ubd81\uc0ac\uc774\uc758 MQTT Client\uc640 \ub370\uc774\ud130\ub97c \uc8fc\uace0 \ubc1b\uae30 \ud14c\uc2a4\ud2b8 <\/li><\/ol>\n\n\n\n<p class=\"has-large-font-size\">ESP32\uc5d0 MicroPython \uc124\uce58\ud558\uae30<\/p>\n\n\n\n<p>ESP32\uc5d0 MicroPython\uc744 \uc124\uce58\ud558\ub824\uba74 firmware\ub97c \ub2e4\uc6b4 \ubc1b\uc544\uc11c \uc124\uce58\ud574\uc57c \ud55c\ub2e4. \ub2e4\uc6b4\ub85c\ub4dc\ub294 \uc544\ub798 \ub9c1\ud06c\uc5d0\uc11c \ubc1b\uc744 \uc218 \uc788\ub2e4. \uac00\uc9c0\uace0 \uc788\ub294 \ud558\ub4dc\uc6e8\uc5b4\uc5d0 \ub9de\uac8c \ubc84\uc804\uc744 \uc120\ud0dd\ud574\uc11c \ub2e4\uc6b4 \ubc1b\uc73c\uba74 \ub41c\ub2e4.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/micropython.org\/download\/esp32\/\" target=\"_blank\">https:\/\/micropython.org\/download\/esp32\/<\/a><\/p>\n\n\n\n<p>\ub2e4\uc6b4\ubc1b\uc740 \ud30c\uc77c\uc744 \uc124\uce58\ud558\uae30 \uc704\ud574\uc11c\ub294 esptool\uc744 \uc0ac\uc6a9\ud560 \uc218 \uc788\ub2e4. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>$ pip install esptool<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/github.com\/espressif\/esptool\">https:\/\/github.com\/espressif\/esptool<\/a><\/p>\n\n\n\n<p>\ub514\ubc14\uc774\uc2a4\uac00 \uc5f0\uacb0\ub41c \ud3ec\ud2b8\ub97c \ud655\uc778\ud558\uace0 (ex: \/dev\/tty.SLAB_USBtoUART) \uc544\ub798\uc640 \uac19\uc774 firmware\ub97c \uc124\uce58\ud55c\ub2e4.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\uae30\uc874\ub370\uc774\ud130\ub97c \uc9c0\uc6b0\uace0\n$ esptool.py --port \/dev\/tty.SLAB_USBtoUART erase_flash \n\n\uc124\uce58\ud55c\ub2e4\n$ esptool.py --chip esp32 --port \/dev\/ttyUSB0 write_flash -z 0x1000 esp32-idf3-20200902-v1.13.bin<\/pre>\n\n\n\n<p class=\"has-large-font-size\">ESP32\uc5d0 MQTT Client \ub9cc\ub4e4\uae30<\/p>\n\n\n\n<p>\uac1c\ubc1c\uc740 VSCode\ub97c \uc774\uc6a9\ud574\uc11c Pymakr \ub77c\ub294 Extension\uc744 \uc124\uce58\ud558\uace0 \ucf54\ub4dc\ub97c \uc4f0\uace0 \uc5c5\ub85c\ub4dc\uc640 \uc2e4\ud589 \ud14c\uc2a4\ud2b8\ub97c \ud560 \uc218 \uc788\ub2e4. \uc124\uce58\ud558\uba74 VSCode \uc544\ub798 \uba54\ub274\ubc14\uc5d0 Run, Upload\ub4f1\uc758 \uba54\ub274\ub97c \ud655\uc778\ud560 \uc218 \uc788\ub2e4.  <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Pymakr\uc740 \uc544\ub798 \ub9c1\ud06c \ucc38\uc870\n<a rel=\"noreferrer noopener\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=pycom.Pymakr\" target=\"_blank\">https:\/\/marketplace.visualstudio.com\/items?itemName=pycom.Pymakr<\/a><\/pre>\n\n\n\n<p>\uc544\ub798 \ucf54\ub4dc\ub294 <a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/micropython-mqtt-esp32-esp8266\/\" target=\"_blank\">https:\/\/randomnerdtutorials.com\/micropython-mqtt-esp32-esp8266\/<\/a> \uc5d0\uc11c \ucc38\uace0 \ud588\ub2e4. boot.py\ub294 ESP32 \uac00 \uc2dc\uc791\ud560\ub54c Wifi\uc5d0 \uc5f0\uacb0\ud558\uace0 main.py\uc5d0\uc11c MQTT Client\ub97c \uc0dd\uc131\ud574\uc11c \ub370\uc774\ud130\ub97c \uc218\uc2e0\ud55c\ub2e4. <\/p>\n\n\n\n<p>boot.py<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import time\nfrom umqttsimple import MQTTClient\nimport ubinascii\nimport machine\nimport micropython\nimport network\nimport esp\nesp.osdebug(None)\nimport gc\ngc.collect()\n\nssid = 'SSID'\npassword = 'PASSWORD'\nmqtt_server = '192.168.0.7'\nclient_id = ubinascii.hexlify(machine.unique_id())\ntopic_sub = b'hello'\ntopic_pub = b'notification'\n\nstation = network.WLAN(network.STA_IF)\nstation.active(True)\nstation.connect(ssid, password)\n\nwhile station.isconnected() == False:\n   pass\n\nprint('Connection successful')\nprint(station.ifconfig())<\/pre>\n\n\n\n<p>main.py<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def sub_cb(topic, msg):\n    print((topic, msg))\n\ndef connect_and_subscribe():\n    global client_id, mqtt_server, topic_sub\n    client = MQTTClient(client_id, mqtt_server)\n    client.set_callback(sub_cb)\n    client.connect()\n    client.subscribe(topic_sub)\n    print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub))\n    return client\n\ndef restart_and_reconnect():\n    print('Failed to connect to MQTT broker. Reconnecting...')\n    time.sleep(10)\n    machine.reset()\n\ntry:\n    client = connect_and_subscribe()\n    client.publish(topic=\"hello\", msg=\"Send By ESP32\")\nexcept OSError as e:\n    restart_and_reconnect()\n\nwhile True:\n    try:\n        new_message = client.check_msg()\n        if new_message != 'None':\n            client.publish(topic_pub, b'received')\n        time.sleep(1)\n    except OSError as e:\n        restart_and_reconnect()<\/pre>\n\n\n\n<p class=\"has-large-font-size\">\ub9e5\ubd81\uc5d0 MQTT \uc11c\ubc84 \uc124\uce58<\/p>\n\n\n\n<p>MQTT \uc11c\ubc84\ub294 \uc0c1\uc138\uc815\ubcf4\ub294 \uc544\ub798 \ub9c1\ud06c\uc5d0\uc11c \ucc38\uc870 \ud560 \uc218 \uc788\uace0 \uc8fc\ub85c IOT \uc7a5\ube44\uc758 \ub124\ud2b8\uc6cd\uc744 \uad6c\uc131\ud558\ub294\ub370 \ub9ce\uc774 \uc0ac\uc6a9\ud55c\ub2e4. \ub098\ub294 brew\ub97c \uc774\uc6a9\ud574 \ubaa8\uc2a4\ud0a4\ud1a0(Mosquitto)\ub97c \uc124\uce58\ud588\ub2e4.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/medium.com\/@jspark141515\/mqtt%EB%9E%80-314472c246ee\" target=\"_blank\">https:\/\/medium.com\/@jspark141515\/mqtt%EB%9E%80-314472c246ee<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\uc11c\ube44\uc2a4 \uc124\uce58\nbrew install mosquitto\n\n\uc11c\ube44\uc2a4 \uc2e4\ud589\nbrew services start mosquitto\n\n\uc11c\ube44\uc2a4 \uc911\uc9c0\nbrew services stop mosquitto<\/pre>\n\n\n\n<p class=\"has-large-font-size\">\ub9e5\ubd81\uc5d0\uc11c \uc1a1, \uc218\uc2e0\uc744 \ud655\uc778 \ud560 \uc218 \uc788\ub294 MQTT Client<\/p>\n\n\n\n<p>Python\uc6a9 MQTT \ud328\ud0a4\uc9c0\ub97c \uba3c\uc800 \uc124\uce58\ud55c\ub2e4.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><a href=\"https:\/\/pypi.org\/project\/paho-mqtt\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/pypi.org\/project\/paho-mqtt\/<\/a>\n\n$ <strong>pip install paho-mqtt<\/strong><\/pre>\n\n\n\n<p>Subscribe.py<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import paho.mqtt.client as mqtt\n\ndef on_connect(client, userdata, flags, rc):\nprint (\"Connected with result code \" + str(rc))\nclient.subscribe(\"hello\")\n\ndef on_message(client, userdata, msg):\nprint (\"Topic: \", msg.topic + '\\nMessage: ' + str(msg.payload))\n\nclient = mqtt.Client() \nclient.on_connect = on_connect \nclient.on_message = on_message \nclient.connect(\"192.168.0.7\", 1883, 60) \n\nclient.loop_forever()<\/pre>\n\n\n\n<p>Publish.py<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import paho.mqtt.client as mqtt\n\nmqttc = mqtt.Client(\"python_pub\") \nmqttc.connect(\"192.168.0.7\", 1883) \nmqttc.publish(\"hello\", \"Hello Billy\")\nmqttc.loop(2)<\/pre>\n\n\n\n<p>Publish.py\ub97c \uc2e4\ud589\ud558\uba74 Subscribe.py\uc640 ESP32\uc5d0\uc11c &#8220;Hello Billy&#8221; \uba54\uc138\uc9c0\ub97c \uc218\uc2e0\ud558\ub294 \uac83\uc744 \ud655\uc778 \ud560 \uc218 \uc788\uc5c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc544\ub798\uc640 \uac19\uc740 \uc0bd\uc9c8\uc744 \ud588\ub2e4. \uc0bd\uc9c8\uc740 \uae30\ub85d\uc774\ub77c\uace0&#8230; \ub300\ucda9 \uc815\ub9ac\ud568 ESP32\uc5d0\uc11c MicroPython\uc744 \uc5b8\uc820\uac00 \ud55c\ubc88 \uc0ac\uc6a9\ud574\ubd10\uc57c\uc9c0 \ud558\uace0 \uc788\uc5c8\ub294\ub370 \uae30\uc655 \ud558\ub294 \uae40\uc5d0 MQTT\uae4c\uc9c0 \uc5f0\uacb0\ud574\ubd24\ub2e4. ESP32 \uc5d0 MicroPython\uc744 \uc124\uce58\ud55c\ub2e4. ESP32\uc5d0\uc11c MicroPython\uc73c\ub85c MQTT Client\ub97c \ub9cc\ub4e0\ub2e4 \ub9e5\ubd81\uc5d0 MQTT \uc11c\ubc84\ub97c \uc124\uce58\ud55c\ub2e4 \ub9e5\ubd81\uc5d0\uc11c Python\uc744 \uc774\uc6a9\ud574\uc11c MQTT Client\ub97c \ub9cc\ub4e0\ub2e4. ESP32 &#8211; \ub9e5\ubd81\uc0ac\uc774\uc758 MQTT Client\uc640 \ub370\uc774\ud130\ub97c \uc8fc\uace0 \ubc1b\uae30 \ud14c\uc2a4\ud2b8 ESP32\uc5d0 MicroPython \uc124\uce58\ud558\uae30 ESP32\uc5d0 MicroPython\uc744 \uc124\uce58\ud558\ub824\uba74 firmware\ub97c<\/p>\n<div class=\"more-link\">\n\t\t\t\t <a href=\"http:\/\/practical.kr\/?p=103\" class=\"link-btn theme-btn\"><span>Read More <\/span> <i class=\"fa fa-caret-right\"><\/i><\/a>\n\t\t\t<\/div>\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":[31,33,32,34,35],"tags":[],"class_list":["post-103","post","type-post","status-publish","format-standard","hentry","category-esp32","category-iot","category-micropython","category-mqtt","category-python"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/practical.kr\/index.php?rest_route=\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/practical.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/practical.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/practical.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/practical.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=103"}],"version-history":[{"count":18,"href":"http:\/\/practical.kr\/index.php?rest_route=\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":123,"href":"http:\/\/practical.kr\/index.php?rest_route=\/wp\/v2\/posts\/103\/revisions\/123"}],"wp:attachment":[{"href":"http:\/\/practical.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/practical.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/practical.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}