#!/usr/bin/env python # LED demo for MQTTHandsOnPubSub, part of http://tmb.gr/mqtt # Licensed https://tamberg.mit-license.org/ # https://shop.pimoroni.com/products/raspberry-pi-zero-w/ # https://shop.pimoroni.com/products/unicorn-hat # both available at https://pi-shop.ch/ # https://github.com/eclipse/paho.mqtt.python # https://github.com/pimoroni/unicorn-hat # On the Raspberry Pi: $ python light.py # On a Desktop Client: $ mqtt pub -h 'test.mosquitto.org' -t 'color' -m '128,0,64' import paho.mqtt.client as mqtt import unicornhat as leds import time leds.set_layout(leds.AUTO) leds.rotation(0) leds.brightness(0.5) width, height = leds.get_shape() def on_connect(client, userdata, flags, rc): print("Connected with result code " + str(rc)) client.subscribe("color") def on_message(client, userdata, msg): print(msg.topic + " " + str(msg.payload)) color = msg.payload.split(",") r = int(color[0]) g = int(color[1]) b = int(color[2]) for y in range(height): for x in range(width): leds.set_pixel(x, y, r, g, b) time.sleep(0.01) # for "fade" effect leds.show() # show each line client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect("test.mosquitto.org", 1883, 60) client.loop_forever()