parent
0d9e360468
commit
22ad4a47d4
30
main.py
30
main.py
|
@ -1,3 +1,4 @@
|
||||||
|
from base64 import b16decode
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
@ -10,6 +11,7 @@ try:
|
||||||
import json
|
import json
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
import threading
|
import threading
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
|
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
|
||||||
exit()
|
exit()
|
||||||
|
@ -24,6 +26,7 @@ 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_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
|
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
|
||||||
|
|
||||||
|
|
||||||
def debug_print(message: str):
|
def debug_print(message: str):
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
current_time = now.strftime("%H:%M:%S")
|
current_time = now.strftime("%H:%M:%S")
|
||||||
|
@ -56,11 +59,21 @@ def setColor(strip, color, wait_ms=10):
|
||||||
strip.show()
|
strip.show()
|
||||||
time.sleep(wait_ms/1000.0)
|
time.sleep(wait_ms/1000.0)
|
||||||
|
|
||||||
|
def mqtt_on_connect(client, userdata, flags, rc):
|
||||||
|
client.subscribe("server-rack-led-1/power")
|
||||||
|
client.subscribe("server-rack-led-1/mode")
|
||||||
|
client.subscribe("server-rack-led-1/r")
|
||||||
|
client.subscribe("server-rack-led-1/g")
|
||||||
|
client.subscribe("server-rack-led-1/b")
|
||||||
|
|
||||||
|
def mqtt_on_message(client, userdata, msg):
|
||||||
|
print(f"Message received [{msg.topic}]: {msg.payload}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
#api
|
#api
|
||||||
|
|
||||||
@app.route('/api/')
|
@app.route('/api/')
|
||||||
def index():
|
def index():
|
||||||
return '''
|
return '''
|
||||||
|
@ -159,17 +172,32 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80)).start()
|
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80)).start()
|
||||||
|
|
||||||
|
mqttBroker ="homeassistant.ping-mee.local"
|
||||||
|
client = mqtt.Client("server-rack-led-1")
|
||||||
|
client.connect(mqttBroker)
|
||||||
|
client.on_connect = mqtt_on_connect
|
||||||
|
client.on_message = mqtt_on_message
|
||||||
|
threading.Thread(target=lambda: client.loop_forever())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
if power == "True":
|
if power == "True":
|
||||||
|
client.publish("server-rack-led-1/power","True")
|
||||||
if mode == 0:
|
if mode == 0:
|
||||||
|
client.publish("server-rack-led-1/mode","0")
|
||||||
rainbowCycle(strip)
|
rainbowCycle(strip)
|
||||||
elif mode == 1:
|
elif mode == 1:
|
||||||
|
client.publish("server-rack-led-1/mode","1")
|
||||||
|
client.publish("server-rack-led-1/r",str(r))
|
||||||
|
client.publish("server-rack-led-1/g",str(g))
|
||||||
|
client.publish("server-rack-led-1/b",str(b))
|
||||||
setColor(strip, Color(int(r), int(g), int(b)))
|
setColor(strip, Color(int(r), int(g), int(b)))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
else:
|
else:
|
||||||
|
client.publish("server-rack-led-1/power","False")
|
||||||
setColor(strip, Color(0,0,0))
|
setColor(strip, Color(0,0,0))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
client.disconnect()
|
||||||
setColor(strip, Color(0,0,0))
|
setColor(strip, Color(0,0,0))
|
||||||
GPIO.cleanup()
|
GPIO.cleanup()
|
||||||
|
|
Loading…
Reference in New Issue