Skip to content

Instantly share code, notes, and snippets.

@jbott jbott/mqtt-tree.py
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.