{"id":6719,"date":"2017-02-25T23:12:49","date_gmt":"2017-02-26T06:12:49","guid":{"rendered":"http:\/\/www.apolonio.com\/blog\/?p=6719"},"modified":"2017-02-25T23:12:49","modified_gmt":"2017-02-26T06:12:49","slug":"python-and-mqtt","status":"publish","type":"post","link":"https:\/\/www.apolonio.com\/blog\/?p=6719","title":{"rendered":"Python and MQTT"},"content":{"rendered":"<p>I got python to subscribe to an MQTT broker from a CentOS 6 box.<\/p>\n<p>First off you need to install the mqtt libraries for python. \u00a0In this example we use the paho-mqtt libraries.<\/p>\n<p>You don&#8217;t use YUM, you use easy_install. \u00a0Here is the command<\/p>\n<pre>easy_install paho-mqtt<\/pre>\n<p>After that you can connect via python whether this is a script or in the interpreter<\/p>\n<p>Here is the script scrubbed of usernames and passwords<\/p>\n<pre>import paho.mqtt.client as mqtt\r\n\r\ndef on_connect(mqttc, userdata, rc):\r\n   print('connected...rc=' + str(rc))\r\n   mqttc.subscribe(topic='<strong>home\/message<\/strong>', qos=0)\r\n\r\ndef on_disconnect(mqttc, userdata, rc):\r\n   print('disconnected...rc=' + str(rc))\r\n\r\ndef on_message(mqttc, userdata, msg):\r\n   print('message received...')\r\n   print('topic: ' + msg.topic + ', qos: ' + \r\n   str(msg.qos) + ', message: ' + str(msg.payload))\r\n\r\ndef on_subscribe(mqttc, userdata, mid, granted_qos):\r\n   print('subscribed (qos=' + str(granted_qos) + ')')\r\n\r\ndef on_unsubscribe(mqttc, userdata, mid, granted_qos):\r\n   print('unsubscribed (qos=' + str(granted_qos) + ')')\r\n\r\nmqttc = mqtt.Client()\r\nmqttc.on_connect = on_connect\r\nmqttc.on_disconnect = on_disconnect\r\nmqttc.on_message = on_message\r\nmqttc.on_subscribe = on_subscribe\r\nmqttc.on_unsubscribe = on_unsubscribe\r\nmqttc.username_pw_set('<strong>usernamehere<\/strong>', '<strong>passwordhere<\/strong>')\r\nmqttc.connect(host='<strong>mqttbroker.example.local<\/strong>', port=1883)\r\nmqttc.loop_forever()<\/pre>\n<p>This was pretty easy for subscribing.<\/p>\n<p>Here is the code that matters, just need to inject what the script will do.<\/p>\n<pre>def on_message(mqttc, userdata, msg):\r\n   print('message received...')\r\n   print('topic: ' + msg.topic + ', qos: ' + \r\n   str(msg.qos) + ', message: ' + str(msg.payload))<\/pre>\n<p>Was pretty easy to get this subscribe working, not sure how the publish will work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I got python to subscribe to an MQTT broker from a CentOS 6 box. First off you need to install the mqtt libraries for python. \u00a0In this example we use the paho-mqtt libraries. You don&#8217;t use YUM, you use easy_install. &hellip; <a href=\"https:\/\/www.apolonio.com\/blog\/?p=6719\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,3],"tags":[],"class_list":["post-6719","post","type-post","status-publish","format-standard","hentry","category-technical","category-training"],"_links":{"self":[{"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6719"}],"version-history":[{"count":1,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6719\/revisions"}],"predecessor-version":[{"id":6720,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6719\/revisions\/6720"}],"wp:attachment":[{"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.apolonio.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}