Skip to content

Instantly share code, notes, and snippets.

@jbott
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbott/3be55039c9e23c2a9c9d to your computer and use it in GitHub Desktop.
Save jbott/3be55039c9e23c2a9c9d to your computer and use it in GitHub Desktop.
MQTT Tree in python, very rough
#pip install paho-mqtt
#pip install git+https://github.com/mbr/asciitree.git
import asciitree
import paho.mqtt.client as mqtt
import sys
from collections import defaultdict
from time import sleep
tr = asciitree.LeftAligned()
def tree(): return defaultdict(tree)
data = tree()
def on_connect(client, userdata, flags, rc):
client.subscribe("#")
def on_message(client, userdata, msg):
loc = data
for key in msg.topic.split('/')[:-1]:
loc = loc[key]
loc[msg.topic.split('/')[-1]] = { str(msg.payload): {} }
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("10.36.37.210", 1883)
client.loop_start()
while True:
if data != {}:
sys.stderr.write("\033c")
print tr(data)
sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment