Skip to content

Instantly share code, notes, and snippets.

@Kartoffel
Last active May 19, 2020 18:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kartoffel/7c31d3daace1c01abf03aa0bcdcf716c to your computer and use it in GitHub Desktop.
Save Kartoffel/7c31d3daace1c01abf03aa0bcdcf716c to your computer and use it in GitHub Desktop.
#!/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