Skip to content

Instantly share code, notes, and snippets.

@jbott jbott/
Last active Aug 29, 2015

What would you like to do?
MQTT Tree in python, very rough
#pip install paho-mqtt
#pip install 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):
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("", 1883)
while True:
if data != {}:
print tr(data)
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.