2022-06-29 19:59:06 +00:00
|
|
|
from base64 import b16decode
|
2021-11-08 20:36:55 +00:00
|
|
|
import time
|
2022-03-11 10:18:23 +00:00
|
|
|
from datetime import datetime
|
2022-07-27 13:25:04 +00:00
|
|
|
from random import *
|
2022-03-26 19:14:20 +00:00
|
|
|
try:
|
|
|
|
from rpi_ws281x import *
|
|
|
|
import RPi.GPIO as GPIO
|
2022-07-07 05:23:20 +00:00
|
|
|
import paho.mqtt.client as mqtt
|
2022-03-26 19:14:20 +00:00
|
|
|
except ImportError:
|
|
|
|
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
|
|
|
|
exit()
|
2022-03-11 10:18:23 +00:00
|
|
|
|
2021-11-08 20:36:55 +00:00
|
|
|
# LED strip configuration:
|
|
|
|
LED_COUNT = 60 # Number of LED pixels.
|
|
|
|
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
|
|
|
|
#LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
|
|
|
|
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
|
|
|
|
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
|
|
|
|
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
|
|
|
|
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
|
|
|
|
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
|
|
|
|
|
2022-07-02 08:28:24 +00:00
|
|
|
global animation_r
|
|
|
|
animation_r = 255
|
|
|
|
global animation_g
|
|
|
|
animation_g = 0
|
|
|
|
global animation_b
|
|
|
|
animation_b = 0
|
2022-06-29 19:59:06 +00:00
|
|
|
|
2022-03-11 10:18:23 +00:00
|
|
|
def debug_print(message: str):
|
|
|
|
now = datetime.now()
|
|
|
|
current_time = now.strftime("%H:%M:%S")
|
|
|
|
print('[DEBUG]['+current_time+'] '+message)
|
|
|
|
|
2022-07-28 16:25:57 +00:00
|
|
|
def fade_in_animation(strip, mode, color):
|
|
|
|
if mode == 0:
|
2022-07-28 16:27:56 +00:00
|
|
|
for i in range(strip.numPixels()):
|
2022-07-28 16:31:58 +00:00
|
|
|
strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels())) & 255))
|
2022-07-28 16:27:56 +00:00
|
|
|
strip.show()
|
2022-07-28 16:25:57 +00:00
|
|
|
elif mode == 1:
|
|
|
|
for i in range(strip.numPixels()):
|
|
|
|
strip.setPixelColor(i, color)
|
|
|
|
strip.show()
|
|
|
|
time.sleep(float(0.01))
|
|
|
|
elif mode == 2:
|
|
|
|
for i in range(strip.numPixels()):
|
|
|
|
strip.setPixelColor(i, color)
|
|
|
|
strip.show()
|
|
|
|
time.sleep(float(0.01))
|
|
|
|
elif mode == 3:
|
|
|
|
color = Color(randint(0, 255), randint(0, 255), randint(0, 255))
|
|
|
|
for i in range(strip.numPixels()):
|
|
|
|
strip.setPixelColor(i, color)
|
|
|
|
strip.show()
|
|
|
|
time.sleep(float(0.01))
|
|
|
|
time.sleep(1)
|
|
|
|
elif mode == 4:
|
|
|
|
for i in range(strip.numPixels()):
|
|
|
|
strip.setPixelColor(i, Color(randint(0, 255), randint(0, 255), randint(0, 255)))
|
|
|
|
strip.show()
|
|
|
|
time.sleep(1)
|
|
|
|
|
2022-07-02 08:31:26 +00:00
|
|
|
def wheel(pos):
|
2021-11-08 20:36:55 +00:00
|
|
|
if pos < 85:
|
|
|
|
return Color(pos * 3, 255 - pos * 3, 0)
|
|
|
|
elif pos < 170:
|
|
|
|
pos -= 85
|
|
|
|
return Color(255 - pos * 3, 0, pos * 3)
|
|
|
|
else:
|
|
|
|
pos -= 170
|
|
|
|
return Color(0, pos * 3, 255 - pos * 3)
|
|
|
|
|
2022-07-25 17:41:33 +00:00
|
|
|
def rainbowCycle(strip, wait_ms=5, iterations=10):
|
2021-11-08 20:36:55 +00:00
|
|
|
for j in range(256*iterations):
|
2022-05-16 18:56:11 +00:00
|
|
|
if mode > 0:
|
2022-03-11 10:18:23 +00:00
|
|
|
break
|
2022-05-16 19:41:37 +00:00
|
|
|
elif power == "False":
|
|
|
|
break
|
2021-11-08 20:36:55 +00:00
|
|
|
for i in range(strip.numPixels()):
|
2022-07-02 08:32:00 +00:00
|
|
|
strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels()) + j) & 255))
|
2021-11-08 20:36:55 +00:00
|
|
|
strip.show()
|
|
|
|
time.sleep(wait_ms/1000.0)
|
|
|
|
|
2022-03-11 10:18:23 +00:00
|
|
|
def setColor(strip, color, wait_ms=10):
|
2021-11-08 20:36:55 +00:00
|
|
|
for i in range(strip.numPixels()):
|
|
|
|
strip.setPixelColor(i, color)
|
2022-07-27 13:27:35 +00:00
|
|
|
strip.show()
|
|
|
|
time.sleep(float(0.01))
|
2021-11-08 20:36:55 +00:00
|
|
|
time.sleep(wait_ms/1000.0)
|
|
|
|
|
2022-07-25 17:41:33 +00:00
|
|
|
def strobe(strip, color):
|
|
|
|
for i in range(strip.numPixels()):
|
|
|
|
strip.setPixelColor(i, color)
|
|
|
|
strip.show()
|
2022-07-25 17:43:55 +00:00
|
|
|
time.sleep(float(0.05))
|
2022-07-25 17:41:33 +00:00
|
|
|
for i in range(strip.numPixels()):
|
|
|
|
strip.setPixelColor(i, Color(0, 0, 0))
|
|
|
|
strip.show()
|
2022-07-25 17:43:55 +00:00
|
|
|
time.sleep(float(0.05))
|
2022-07-25 17:41:33 +00:00
|
|
|
|
2022-07-27 13:29:43 +00:00
|
|
|
def randomColorPerLED(strip):
|
2022-07-27 13:25:04 +00:00
|
|
|
for i in range(strip.numPixels()):
|
|
|
|
strip.setPixelColor(i, Color(randint(0, 255), randint(0, 255), randint(0, 255)))
|
|
|
|
strip.show()
|
|
|
|
time.sleep(1)
|
|
|
|
|
2022-07-27 13:29:43 +00:00
|
|
|
def randomColor(strip):
|
2022-07-27 13:27:35 +00:00
|
|
|
color = Color(randint(0, 255), randint(0, 255), randint(0, 255))
|
|
|
|
for i in range(strip.numPixels()):
|
|
|
|
strip.setPixelColor(i, color)
|
|
|
|
strip.show()
|
|
|
|
time.sleep(1)
|
|
|
|
|
2022-06-29 19:59:06 +00:00
|
|
|
def mqtt_on_connect(client, userdata, flags, rc):
|
2022-07-01 21:19:42 +00:00
|
|
|
client.subscribe("server-rack-led/power")
|
|
|
|
client.subscribe("server-rack-led/mode")
|
|
|
|
client.subscribe("server-rack-led/rgb")
|
2022-06-29 19:59:06 +00:00
|
|
|
|
|
|
|
|
2022-05-16 18:56:11 +00:00
|
|
|
if __name__ == '__main__':
|
2022-07-28 16:25:57 +00:00
|
|
|
global singleExecute
|
|
|
|
singleExecute = True
|
2022-07-01 20:49:15 +00:00
|
|
|
def mqtt_on_message(client, userdata, msg):
|
|
|
|
print("topic: " ,str(msg.topic))
|
|
|
|
print("payload: " ,str(msg.payload.decode("utf-8")))
|
|
|
|
topic = str(msg.topic)
|
|
|
|
payload = str(msg.payload.decode("utf-8"))
|
2022-07-01 21:19:42 +00:00
|
|
|
if topic == "server-rack-led/power":
|
2022-07-01 20:55:57 +00:00
|
|
|
global power
|
2022-07-01 20:49:15 +00:00
|
|
|
power = payload
|
2022-07-28 16:29:11 +00:00
|
|
|
global singleExecute
|
2022-07-28 16:25:57 +00:00
|
|
|
singleExecute = True
|
2022-07-01 20:49:15 +00:00
|
|
|
|
2022-07-01 21:19:42 +00:00
|
|
|
elif topic == "server-rack-led/rgb":
|
2022-07-01 22:23:32 +00:00
|
|
|
splitted_payload = payload.split(",")
|
2022-07-01 20:55:57 +00:00
|
|
|
global r
|
2022-07-01 22:23:32 +00:00
|
|
|
r = str(splitted_payload[0])
|
2022-07-01 21:22:50 +00:00
|
|
|
global g
|
2022-07-01 22:23:32 +00:00
|
|
|
g = str(splitted_payload[1])
|
2022-07-01 21:22:50 +00:00
|
|
|
global b
|
2022-07-01 22:23:32 +00:00
|
|
|
b = str(splitted_payload[2])
|
2022-07-01 20:49:15 +00:00
|
|
|
|
2022-07-01 21:19:42 +00:00
|
|
|
elif topic == "server-rack-led/mode":
|
2022-07-01 22:42:10 +00:00
|
|
|
if payload == "Rainbow":
|
2022-07-01 20:55:57 +00:00
|
|
|
global mode
|
2022-07-01 20:49:15 +00:00
|
|
|
mode = 0
|
2022-07-01 22:42:10 +00:00
|
|
|
elif payload == "Custom Color":
|
2022-07-01 20:49:15 +00:00
|
|
|
mode = 1
|
2022-07-25 17:42:41 +00:00
|
|
|
elif payload == "Strobe":
|
|
|
|
mode = 2
|
2022-07-27 13:25:04 +00:00
|
|
|
elif payload == "Random Color":
|
|
|
|
mode = 3
|
2022-07-27 13:27:35 +00:00
|
|
|
elif payload == "Random Color per LED":
|
|
|
|
mode = 4
|
2022-07-27 13:49:29 +00:00
|
|
|
|
|
|
|
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
|
|
|
|
strip.begin()
|
|
|
|
|
|
|
|
mode = 0
|
|
|
|
power = "True"
|
|
|
|
global r
|
|
|
|
r = 255
|
|
|
|
global g
|
|
|
|
g = 255
|
|
|
|
global b
|
|
|
|
b = 255
|
|
|
|
|
|
|
|
mqttBroker ="homeassistant.ping-mee.local"
|
|
|
|
client = mqtt.Client("server_rack_led")
|
|
|
|
client.username_pw_set("mqtt", "pmMQTT_11!")
|
|
|
|
debug_print("Connecting to MQTT Broker "+str(mqttBroker))
|
|
|
|
client.connect(mqttBroker)
|
|
|
|
client.on_connect = mqtt_on_connect
|
|
|
|
client.on_message = mqtt_on_message
|
|
|
|
|
|
|
|
client.publish("server-rack-led/power", "True")
|
|
|
|
client.publish("server-rack-led/mode", "Rainbow")
|
|
|
|
|
|
|
|
try:
|
|
|
|
client.loop_start()
|
|
|
|
while True:
|
|
|
|
if power == "True":
|
2022-07-28 16:25:57 +00:00
|
|
|
if singleExecute == True:
|
|
|
|
fade_in_animation(strip, mode, Color(int(r), int(g), int(b)))
|
|
|
|
singleExecute = False
|
|
|
|
|
2022-07-27 13:49:29 +00:00
|
|
|
if mode == 0:
|
|
|
|
rainbowCycle(strip)
|
|
|
|
elif mode == 1:
|
|
|
|
setColor(strip, Color(int(r), int(g), int(b)))
|
|
|
|
elif mode == 2:
|
|
|
|
strobe(strip, Color(int(r), int(g), int(b)))
|
|
|
|
elif mode == 3:
|
|
|
|
randomColor(strip)
|
|
|
|
elif mode == 4:
|
|
|
|
randomColorPerLED(strip)
|
|
|
|
else:
|
2022-07-28 16:25:57 +00:00
|
|
|
if singleExecute == True:
|
|
|
|
singleExecute = False
|
2022-07-27 13:49:29 +00:00
|
|
|
setColor(strip, Color(0,0,0))
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
client.publish("server-rack-led/power", "False")
|
|
|
|
client.loop_stop()
|
|
|
|
setColor(strip, Color(0,0,0))
|
|
|
|
GPIO.cleanup()
|