├── .gitignore ├── README.md ├── app.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This sample app shows how to connect with [mosquitto](https://pypi.python.org/pypi/mosquitto/) to [CloudMQTT](http://www.cloudmqtt.com) and both publish and subscribe messages. 2 | 3 | $ pip install -r requirements.txt 4 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2010,2011 Roger Light 2 | # All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are met: 6 | # 7 | # 1. Redistributions of source code must retain the above copyright notice, 8 | # this list of conditions and the following disclaimer. 9 | # 2. Redistributions in binary form must reproduce the above copyright 10 | # notice, this list of conditions and the following disclaimer in the 11 | # documentation and/or other materials provided with the distribution. 12 | # 3. Neither the name of mosquitto nor the names of its 13 | # contributors may be used to endorse or promote products derived from 14 | # this software without specific prior written permission. 15 | # 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 20 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | # POSSIBILITY OF SUCH DAMAGE. 27 | 28 | import paho.mqtt.client as mqtt 29 | import os, urlparse 30 | 31 | # Define event callbacks 32 | def on_connect(client, userdata, flags, rc): 33 | print("rc: " + str(rc)) 34 | 35 | def on_message(client, obj, msg): 36 | print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) 37 | 38 | def on_publish(client, obj, mid): 39 | print("mid: " + str(mid)) 40 | 41 | def on_subscribe(client, obj, mid, granted_qos): 42 | print("Subscribed: " + str(mid) + " " + str(granted_qos)) 43 | 44 | def on_log(client, obj, level, string): 45 | print(string) 46 | 47 | mqttc = mqtt.Client() 48 | # Assign event callbacks 49 | mqttc.on_message = on_message 50 | mqttc.on_connect = on_connect 51 | mqttc.on_publish = on_publish 52 | mqttc.on_subscribe = on_subscribe 53 | 54 | # Uncomment to enable debug messages 55 | #mqttc.on_log = on_log 56 | 57 | # Parse CLOUDMQTT_URL (or fallback to localhost) 58 | url_str = os.environ.get('CLOUDMQTT_URL', 'mqtt://localhost:1883') 59 | url = urlparse.urlparse(url_str) 60 | topic = url.path[1:] or 'test' 61 | 62 | # Connect 63 | mqttc.username_pw_set(url.username, url.password) 64 | mqttc.connect(url.hostname, url.port) 65 | 66 | # Start subscribe, with QoS level 0 67 | mqttc.subscribe(topic, 0) 68 | 69 | # Publish a message 70 | mqttc.publish(topic, "my message") 71 | 72 | # Continue the network loop, exit when an error occurs 73 | rc = 0 74 | while rc == 0: 75 | rc = mqttc.loop() 76 | print("rc: " + str(rc)) 77 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | paho-mqtt==1.3.0 2 | 3 | --------------------------------------------------------------------------------