#!/usr/bin/env python2 from blinkt import set_pixel, show, clear import paho.mqtt.client as mqtt """ Based on the 'mqtt.py' script provided by Pimoroni. This one talks to a Mosquitto broker on the same host and assumes that the Blinkt is connected to the local machine. """ MQTT_SERVER = "localhost" MQTT_PORT = 1883 MQTT_TOPIC = "pimoroni/blinkt" # Set these to use authorisation MQTT_USER = None MQTT_PASS = None def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) client.subscribe(MQTT_TOPIC) def on_message(client, userdata, msg): data = msg.payload.split(',') command = data.pop(0) if command == "clr" and len(data) == 0: clear() show() return if command == "rgb" and len(data) == 4: try: pixel = data.pop(0) if pixel == "*": pixel = None else: pixel = int(pixel) if pixel > 7: print("Pixel out of range: " + str(pixel)) return r, g, b = [int(x) & 0xff for x in data] print(command, pixel, r, g, b) except ValueError: print("Malformed command: " + str(msg.payload)) return if pixel is None: for x in range(8): set_pixel(x, r, g, b) else: set_pixel(pixel, r, g, b) show() return client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message if MQTT_USER is not None and MQTT_PASS is not None: print("Using username: {un} and password: {pw}".format(un=MQTT_USER, pw="*" * len(MQTT_PASS))) client.username_pw_set(username=MQTT_USER, password=MQTT_PASS) client.connect(MQTT_SERVER, MQTT_PORT, 60) client.loop_forever()