{"id":1184,"date":"2017-10-03T22:16:44","date_gmt":"2017-10-03T21:16:44","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=1184"},"modified":"2017-10-03T22:27:36","modified_gmt":"2017-10-03T21:27:36","slug":"micropython-and-mqtt","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2017\/micropython-and-mqtt","title":{"rendered":"micropython and MQTT"},"content":{"rendered":"<p>First tests with micropython and MQTT.<\/p>\n<p>On Ubuntu 17.04:<\/p>\n<p>If we want to use our system as a MQTT broker, we need the mosquitto daemon. The mosquitto-clients package include commands to test publishing and subscribing from the command line:<\/p>\n<pre>sudo apt-get install mosquitto mosquitto-clients<\/pre>\n<p>To install micropython just follow <a href=\"https:\/\/github.com\/micropython\/micropython\/wiki\/Getting-Started#debian-ubuntu-mint-and-variants\">instructions for Debian-based systems<\/a>.<\/p>\n<p>Then we need to install a MQTT library for micropython. Used &#8216;<a href=\"https:\/\/github.com\/micropython\/micropython-lib\/tree\/master\/umqtt.simple\">umqtt.simple<\/a>&#8216;:<\/p>\n<pre>.\/micropython -m upip install micropython-umqtt.simple<\/pre>\n<p>They have some examples, let&#8217;s test &#8216;<a href=\"https:\/\/github.com\/micropython\/micropython-lib\/blob\/master\/umqtt.simple\/example_pub.py\">example_pub.py<\/a>&#8216;:<\/p>\n<pre>from umqtt.simple import MQTTClient\r\n\r\ndef main(server=\"localhost\"):\r\n    c = MQTTClient(\"umqtt_client\", server)\r\n    c.connect()\r\n    c.publish(b\"foo_topic\", b\"hello\")\r\n    c.disconnect()\r\n\r\nif __name__ == \"__main__\":\r\n    main()<\/pre>\n<p>This example publish &#8220;hello&#8221; message to topic &#8220;foo_topic&#8221; on a broker on our localhost. If we have installed &#8216;mosquitto&#8217;, it is already started (at least on Ubuntu) so we just need to subscribe for messages under &#8220;foo_topic&#8221;:<\/p>\n<pre>mosquitto_sub -h localhost -t foo_topic<\/pre>\n<p>We can now run the example (on another terminal):<\/p>\n<pre>.\/micropython example_pub.py<\/pre>\n<p>and we should get:<\/p>\n<pre>hello<\/pre>\n<p>&nbsp;<\/p>\n<p>Now let&#8217;s use a public MQTT broker. There are <a href=\"https:\/\/github.com\/mqtt\/mqtt.github.io\/wiki\/public_brokers\">several available<\/a> but since we are already using mosquitto client we will also use mosquitto own public broker (i.e. &#8216;<a href=\"http:\/\/test.mosquitto.org\">test.mosquitto.org<\/a>&#8216;).<\/p>\n<p>Since it is a public broker, it has considerable traffic so we use a less general topic:<\/p>\n<pre>from umqtt.simple import MQTTClient\r\n\r\nSERVER='test.mosquitto.org'\r\n\r\ndef main(server=SERVER):\r\n    c = MQTTClient(\"umqtt_client\", server)\r\n    c.connect()\r\n    print(\"Connected\")\r\n    c.publish(b\"ev3dev_topic\", b\"Make my day!\")\r\n    c.disconnect()\r\n    print(\"Sent\")\r\n\r\nif __name__ == \"__main__\":\r\n    main()<\/pre>\n<p>And we subscribe to it:<\/p>\n<pre>mosquitto_sub -h test.mosquitto.org -t ev3dev_topic<\/pre>\n<p>and it still works. It&#8217;s not instant messaging but it sure takes less than a second (please note that a public broker cannot assure 100% availability or performance; even it the service is available performance can be degraded if lots of publishers and subscribers are connected passing lots of messages).<\/p>\n<p>Now let&#8217;s do the opposite &#8211; subscribe with micropython. We use another example, &#8216;example_sub.py&#8217;:<\/p>\n<p>&nbsp;<\/p>\n<pre>import time\r\nfrom umqtt.simple import MQTTClient\r\n\r\n# Publish test messages e.g. with:\r\n# mosquitto_pub -t foo_topic -m hello\r\n\r\nSERVER = \"test.mosquitto.org\"\r\n\r\n# Received messages from subscriptions will be delivered to this callback\r\ndef sub_cb(topic, msg):\r\n    print((topic, msg))\r\n\r\ndef main(server=SERVER\"):\r\n    c = MQTTClient(\"umqtt_client\", server)\r\n    c.set_callback(sub_cb)\r\n    c.connect()\r\n    c.subscribe(b\"ev3dev_topic\")\r\n    while True:\r\n    if True:\r\n        # Blocking wait for message\r\n        c.wait_msg()\r\n    else:\r\n        # Non-blocking wait for message\r\n        c.check_msg()\r\n\r\n        # Then need to sleep to avoid 100% CPU usage (in a real\r\n        # app other useful actions would be performed instead)\r\n        time.sleep(1)\r\n\r\n    c.disconnect()\r\n\r\nif __name__ == \"__main__\":\r\n    main()<\/pre>\n<p>&nbsp;<\/p>\n<p>For this script to run we also need &#8216;time&#8217; library for micropython:<\/p>\n<pre>.\/micropython -m upip install micropython-time<\/pre>\n<p>So if we now publish something:<\/p>\n<pre>mosquitto_pub -h test.mosquitto.org -t ev3dev_topic -m Yahoo!<\/pre>\n<p>we&#8217;ll get:<\/p>\n<pre>$ .\/micropython testsub.py \r\n(b'ev3dev_topic', b'Yahoo!')<\/pre>\n<p>Next: port this to ev3dev.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Now the first test:<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First tests with micropython and MQTT. On Ubuntu 17.04: If we want to use our system as a MQTT broker, we need the mosquitto daemon. The mosquitto-clients package include commands to test publishing and subscribing from the command line: sudo apt-get install mosquitto mosquitto-clients To install micropython just follow instructions for Debian-based systems. Then we &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2017\/micropython-and-mqtt\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;micropython and MQTT&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"series":[],"class_list":["post-1184","post","type-post","status-publish","format-standard","hentry","category-sem-categoria"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-j6","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=1184"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/1184\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=1184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=1184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=1184"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=1184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}