|
| 1 | +# Adafruit IO MQTT 'debug' view to see all messages. Will display every |
| 2 | +# message published for an account on AIO. Useful for debugging when other |
| 3 | +# devices are writing to AIO for an account. |
| 4 | +# |
| 5 | +# Copyright (c) 2014 Adafruit Industries |
| 6 | +# Author: Tony DiCola |
| 7 | + |
| 8 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +# of this software and associated documentation files (the "Software"), to deal |
| 10 | +# in the Software without restriction, including without limitation the rights |
| 11 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +# copies of the Software, and to permit persons to whom the Software is |
| 13 | +# furnished to do so, subject to the following conditions: |
| 14 | + |
| 15 | +# The above copyright notice and this permission notice shall be included in all |
| 16 | +# copies or substantial portions of the Software. |
| 17 | + |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +# SOFTWARE. |
| 25 | +import logging |
| 26 | + |
| 27 | +import paho.mqtt.client as mqtt |
| 28 | + |
| 29 | + |
| 30 | +# Edit below to include your AIO account details. |
| 31 | +USERNAME = 'YOUR AIO USERNAME' |
| 32 | +KEY = 'YOUR AIO KEY' |
| 33 | + |
| 34 | +# Adafruit IO MQTT service details. |
| 35 | +SERVER = 'io.adafruit.com' |
| 36 | +PORT = 1883 |
| 37 | +KEEPALIVE = 3600 # One minute |
| 38 | + |
| 39 | +# Path to subscribe to for messages. By default this is the '<username>/#' path |
| 40 | +# which means all messages for that user will be seen. |
| 41 | +PATH = USERNAME + '/#' |
| 42 | + |
| 43 | + |
| 44 | +# Setup message handlers for connect, disconnect and message received. |
| 45 | +def on_connect(client, userdata, flags, rc): |
| 46 | + print 'Connected!' |
| 47 | + client.subscribe(PATH) |
| 48 | + print 'Subscribed to path {0}!'.format(PATH) |
| 49 | + |
| 50 | +def on_disconnect(client, userdata, rc): |
| 51 | + print 'Disconnected!' |
| 52 | + |
| 53 | +def on_message(client, userdata, msg): |
| 54 | + print 'Received on {0}: {1}'.format(msg.topic, msg.payload.decode('utf-8')) |
| 55 | + |
| 56 | + |
| 57 | +# Create MQTT client and connect to Adafruit IO. |
| 58 | +client = mqtt.Client() |
| 59 | +client.username_pw_set(USERNAME, KEY) |
| 60 | +client.on_connect = on_connect |
| 61 | +client.on_disconnect = on_disconnect |
| 62 | +client.on_message = on_message |
| 63 | +client.connect(SERVER, port=PORT, keepalive=KEEPALIVE) |
| 64 | + |
| 65 | +print 'Press Ctrl-C to quit.' |
| 66 | +client.loop_forever() |
0 commit comments