Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python3
import paho.mqtt.client as mqtt
from sty import fg, bg, rs
width = height = 5
mqtt_server = "test.mosquitto.org"
mqtt_topic = "matrixflut"
def on_connect(client, userdata, flags, rc):
print("Connected (rc={})".format(rc))
client.subscribe(mqtt_topic)
def on_subscribe(client, userdata, mid, granted_qos):
print('Subscribed')
def on_message(client, userdata, msg):
values = list(msg.payload)
# "Validation" of data
if len(values) != (width * height * 3):
return print("Invalid payload ({} bytes)".format(len(values)))
display = bg(0,0,0)
for i in range(width * height):
ii = i * 3
if (ii % (width * 3)) == 0:
display += rs.bg + '\n' + bg(0,0,0)
rgb = values[ii:(ii+3)]
if max(rgb) > 255:
return print("Invalid RGB value")
display += fg(*rgb) + '■ '
display += rs.fg + rs.bg
print(display)
client = mqtt.Client()
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message
client.connect(mqtt_server)
client.loop_forever()
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.