2022-06-29 19:59:06 +00:00
|
|
|
from base64 import b16decode
|
2021-11-08 20:36:55 +00:00
|
|
|
import time
|
|
|
|
import os
|
2022-03-11 10:18:23 +00:00
|
|
|
from datetime import datetime
|
2022-05-16 19:08:45 +00:00
|
|
|
|
|
|
|
from requests import request
|
2022-03-26 19:14:20 +00:00
|
|
|
try:
|
|
|
|
from rpi_ws281x import *
|
|
|
|
import RPi.GPIO as GPIO
|
2022-05-16 19:13:14 +00:00
|
|
|
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
|
2022-05-16 18:56:11 +00:00
|
|
|
import json
|
2022-05-16 19:10:53 +00:00
|
|
|
from argparse import ArgumentParser
|
2022-05-16 19:31:43 +00:00
|
|
|
import threading
|
2022-06-29 19:59:06 +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-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)
|
|
|
|
|
2021-11-08 20:36:55 +00:00
|
|
|
def wheel(pos):
|
|
|
|
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)
|
|
|
|
|
2021-11-09 22:16:38 +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()):
|
|
|
|
strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels()) + j) & 255))
|
|
|
|
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)
|
|
|
|
strip.show()
|
|
|
|
time.sleep(wait_ms/1000.0)
|
|
|
|
|
2022-06-29 19:59:06 +00:00
|
|
|
def mqtt_on_connect(client, userdata, flags, rc):
|
2022-07-01 19:52:00 +00:00
|
|
|
#sub to status
|
2022-07-01 20:26:11 +00:00
|
|
|
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")
|
2022-07-01 20:32:30 +00:00
|
|
|
|
|
|
|
def mqtt_on_message(client, userdata, msg):
|
2022-07-01 20:42:07 +00:00
|
|
|
print("topic: " ,str(msg.topic))
|
2022-07-01 20:39:56 +00:00
|
|
|
print("payload: " ,str(msg.payload.decode("utf-8")))
|
2022-07-01 20:42:07 +00:00
|
|
|
topic = str(msg.topic)
|
2022-07-01 20:39:56 +00:00
|
|
|
payload = str(msg.payload.decode("utf-8"))
|
|
|
|
if topic == "server-rack-led-1/power":
|
|
|
|
power = payload
|
2022-07-01 20:32:30 +00:00
|
|
|
|
2022-07-01 20:39:56 +00:00
|
|
|
elif topic == "server-rack-led-1/r":
|
|
|
|
r = payload
|
2022-07-01 20:32:30 +00:00
|
|
|
|
2022-07-01 20:39:56 +00:00
|
|
|
elif topic == "server-rack-led-1/g":
|
|
|
|
g = payload
|
2022-07-01 20:32:30 +00:00
|
|
|
|
2022-07-01 20:39:56 +00:00
|
|
|
elif topic == "server-rack-led-1/b":
|
|
|
|
b = payload
|
2022-07-01 20:32:30 +00:00
|
|
|
|
2022-07-01 20:39:56 +00:00
|
|
|
elif topic == "server-rack-led-1/mode":
|
|
|
|
if payload == "0":
|
2022-07-01 20:32:30 +00:00
|
|
|
mode = 0
|
2022-07-01 20:39:56 +00:00
|
|
|
elif payload == "1":
|
2022-07-01 20:32:30 +00:00
|
|
|
mode = 1
|
2022-06-29 19:59:06 +00:00
|
|
|
|
|
|
|
|
2022-05-16 18:56:11 +00:00
|
|
|
if __name__ == '__main__':
|
2022-07-01 20:44:22 +00:00
|
|
|
app = Flask(__name__)
|
2022-06-28 16:07:59 +00:00
|
|
|
|
|
|
|
#api
|
|
|
|
@app.route('/api/')
|
2022-05-16 18:56:11 +00:00
|
|
|
def index():
|
|
|
|
return '''
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Server Rack LED stripe</title>
|
|
|
|
</head>
|
|
|
|
<style>
|
|
|
|
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
|
|
|
|
body {
|
|
|
|
font-family: 'Roboto', sans-serif;
|
|
|
|
background-color: #1E1E1E;
|
|
|
|
color: #fff;
|
|
|
|
}
|
2021-11-08 20:36:55 +00:00
|
|
|
|
2022-05-16 18:56:11 +00:00
|
|
|
code {
|
|
|
|
background-color: #333333;
|
|
|
|
border: 5px solid #333333;
|
|
|
|
border-radius: 10px;
|
2021-11-08 20:36:55 +00:00
|
|
|
|
2022-05-16 18:56:11 +00:00
|
|
|
.head-text {
|
|
|
|
font-size: 25px;
|
|
|
|
#put text in middle of page
|
|
|
|
margin-left: auto;
|
|
|
|
margin-right: auto;
|
|
|
|
left: 50%;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<body>
|
|
|
|
<p class="head-text">My Server Rack LED stripe control API</p>
|
|
|
|
<br>
|
|
|
|
<p>How to use the API:</p>
|
2022-05-16 19:43:31 +00:00
|
|
|
<code>/power?power=True</code>
|
|
|
|
<br>
|
|
|
|
<code>/power?power=False</code>
|
|
|
|
<br>
|
|
|
|
<code>/mode?mode=0</code>
|
|
|
|
<br>
|
|
|
|
<code>/mode?mode=1&r=0&g=0&b=0</code>
|
2022-05-16 18:56:11 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
'''
|
2021-11-09 17:26:35 +00:00
|
|
|
|
2022-06-28 16:07:59 +00:00
|
|
|
@app.route('/api/mode', methods=['GET'])
|
2022-05-16 18:56:11 +00:00
|
|
|
def mode():
|
2022-05-16 19:23:10 +00:00
|
|
|
global mode
|
|
|
|
global r
|
|
|
|
global g
|
|
|
|
global b
|
2022-05-16 19:12:07 +00:00
|
|
|
args = request.args
|
2022-05-16 19:09:43 +00:00
|
|
|
mode = str(args.get('mode'))
|
|
|
|
r = str(args.get('r'))
|
|
|
|
g = str(args.get('g'))
|
|
|
|
b = str(args.get('b'))
|
2022-05-16 19:06:13 +00:00
|
|
|
if mode is None:
|
2022-05-16 18:56:11 +00:00
|
|
|
return 'No mode specified'
|
2022-05-16 19:06:13 +00:00
|
|
|
elif mode == "":
|
2022-05-16 18:56:11 +00:00
|
|
|
return 'No mode specified'
|
|
|
|
elif mode == "0":
|
|
|
|
mode = 0
|
|
|
|
return 'Rainbow Cycle'
|
|
|
|
elif mode == "1":
|
2022-05-16 19:06:13 +00:00
|
|
|
if r is None or g is None or b is None:
|
|
|
|
return 'No color specified'
|
|
|
|
elif r == "" or g == "" or b == "":
|
|
|
|
return 'No color specified'
|
|
|
|
else:
|
|
|
|
mode = 1
|
|
|
|
return 'Color: '+str(r)+','+str(g)+','+str(b)
|
2021-11-08 20:36:55 +00:00
|
|
|
|
2022-06-28 16:07:59 +00:00
|
|
|
@app.route('/api/power', methods=['GET'])
|
2022-05-16 18:56:11 +00:00
|
|
|
def power():
|
2022-05-16 19:36:58 +00:00
|
|
|
global power
|
|
|
|
power = request.args.get('power')
|
|
|
|
if power is None:
|
|
|
|
return 'No power argument specified'
|
|
|
|
elif power == "":
|
|
|
|
return 'No power argument specified'
|
2022-05-16 19:39:21 +00:00
|
|
|
elif power == "True":
|
2022-05-16 19:40:03 +00:00
|
|
|
power = "True"
|
2022-05-16 19:39:21 +00:00
|
|
|
return 'Power: ON'
|
|
|
|
elif power == "False":
|
2022-05-16 19:40:03 +00:00
|
|
|
power = "False"
|
2022-05-16 19:39:21 +00:00
|
|
|
return 'Power: OFF'
|
2022-03-26 19:39:25 +00:00
|
|
|
|
2022-05-16 18:56:11 +00:00
|
|
|
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
|
|
|
|
strip.begin()
|
2022-05-16 18:57:58 +00:00
|
|
|
|
2022-05-16 19:23:10 +00:00
|
|
|
mode = 0
|
2022-05-16 19:38:15 +00:00
|
|
|
power = "True"
|
2022-05-16 19:23:10 +00:00
|
|
|
|
2022-07-01 20:28:36 +00:00
|
|
|
|
2022-05-16 19:31:43 +00:00
|
|
|
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80)).start()
|
2022-05-16 18:57:58 +00:00
|
|
|
|
2022-06-29 19:59:06 +00:00
|
|
|
mqttBroker ="homeassistant.ping-mee.local"
|
2022-07-01 20:26:11 +00:00
|
|
|
client = mqtt.Client("server_rack_led_1")
|
2022-06-29 20:06:39 +00:00
|
|
|
client.username_pw_set("mqtt", "pmMQTT_11!")
|
|
|
|
debug_print("Connecting to MQTT Broker "+str(mqttBroker))
|
2022-06-29 19:59:06 +00:00
|
|
|
client.connect(mqttBroker)
|
|
|
|
client.on_connect = mqtt_on_connect
|
2022-07-01 20:32:30 +00:00
|
|
|
client.on_message = mqtt_on_message
|
2022-07-01 20:26:11 +00:00
|
|
|
|
2022-07-01 20:28:36 +00:00
|
|
|
client.publish("server-rack-led-1/power","True")
|
|
|
|
client.publish("server-rack-led-1/mode","0")
|
|
|
|
|
2022-07-01 20:46:56 +00:00
|
|
|
# threading.Thread(target=lambda: client.loop_start())
|
2022-06-29 19:59:06 +00:00
|
|
|
|
2021-11-08 20:36:55 +00:00
|
|
|
try:
|
2022-07-01 20:46:56 +00:00
|
|
|
client.loop_start()
|
2021-11-08 20:36:55 +00:00
|
|
|
while True:
|
2022-05-16 19:38:15 +00:00
|
|
|
if power == "True":
|
2022-05-16 19:23:10 +00:00
|
|
|
if mode == 0:
|
|
|
|
rainbowCycle(strip)
|
2022-07-01 20:28:36 +00:00
|
|
|
|
2022-05-16 19:23:10 +00:00
|
|
|
elif mode == 1:
|
|
|
|
setColor(strip, Color(int(r), int(g), int(b)))
|
2022-04-28 14:46:41 +00:00
|
|
|
time.sleep(1)
|
2021-11-09 22:16:38 +00:00
|
|
|
else:
|
2022-07-01 20:26:11 +00:00
|
|
|
client.publish("server-rack-led-1/power","False")
|
2021-11-09 22:16:38 +00:00
|
|
|
setColor(strip, Color(0,0,0))
|
|
|
|
time.sleep(1)
|
2022-07-01 20:28:36 +00:00
|
|
|
|
2021-11-08 20:36:55 +00:00
|
|
|
except KeyboardInterrupt:
|
2022-07-01 20:46:56 +00:00
|
|
|
client.loop_stop()
|
2021-11-09 17:29:50 +00:00
|
|
|
setColor(strip, Color(0,0,0))
|
2022-04-28 14:48:23 +00:00
|
|
|
GPIO.cleanup()
|