Forked from tomikazi/gist:1bb9d002292870b9c4c1e2b13996a001
Created
May 16, 2021 06:41
-
-
Save anthonylavado/ae9a2c072ab6a79597f9e9461860e850 to your computer and use it in GitHub Desktop.
Simple python script to control RGB WS2812B LED strip mounted under the top crossbar of on my Ender 3 Pro. Runs as a service on the OctoPi and integrates with Home Assistant as MQTT light.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import paho.mqtt.client as mqtt | |
import time | |
import board | |
import neopixel | |
led = neopixel.NeoPixel(board.D18, 15) | |
bright = 0.75 | |
led.brightness = bright | |
led.fill((255,173,145)) | |
mqc = mqtt.Client(client_id = "ender") | |
def onConnect(client, controller, flags, rc): | |
print("MQTT connected") | |
mqc.subscribe("ender/#") | |
def onMessage(client, controller, msg): | |
global bright | |
val = str(msg.payload.decode("utf-8")) | |
print("%s:%s" % (msg.topic, val)) | |
if msg.topic == "ender/light": | |
if val == "on": | |
led.brightness = bright | |
mqc.publish("ender/light/state", "on", 2, False) | |
else: | |
led.brightness = 0 | |
mqc.publish("ender/light/state", "off", 2, False) | |
elif msg.topic == "ender/brightness": | |
bright = float(val)/255.0 | |
led.brightness = bright | |
mqc.publish("ender/brightness/state", val, 2, False) | |
elif msg.topic == "ender/rgb": | |
rgb = val.split(",") | |
led.fill((int(rgb[0]), int(rgb[1]), int(rgb[2]))) | |
led.brightness = bright | |
mqc.publish("ender/rgb/state", val, 2, False) | |
elif msg.topic == "ender/hexrgb": | |
led.fill(int(val[1:], 16)) | |
led.brightness = bright | |
mqc.publish("ender/rgb/state", val, 2, False) | |
mqc.on_connect = onConnect | |
mqc.on_message = onMessage | |
mqc.username_pw_set(mqtt_user, password=mqtt_password) | |
mqc.connect_async(mqtt_broker_ip) | |
mqc.loop_start() | |
while True: | |
time.sleep(15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment