server-rack-led/main.py

158 lines
5.4 KiB
Python

import time
import os
from datetime import datetime
try:
from rpi_ws281x import *
import RPi.GPIO as GPIO
from flask import Flask
import json
except ImportError:
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
exit()
# 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
mode = 0
power = True
def debug_print(message: str):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print('[DEBUG]['+current_time+'] '+message)
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)
def rainbowCycle(strip, wait_ms=5, iterations=10):
for j in range(256*iterations):
if mode > 0:
break
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)
def setColor(strip, color, wait_ms=10):
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
time.sleep(wait_ms/1000.0)
if __name__ == '__main__':
app = Flask(__name__)
@app.route('/')
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;
}
code {
background-color: #333333;
border: 5px solid #333333;
border-radius: 10px;
.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>Please use the following links to access the API:</p>
<ul>
<li><a href="/api/send">/api/send</a></li>
</ul>
<br>
<p>How to use the API:</p>
<ul>
<li><code>/api/send</code>: Send a SMS</li>
<p>list of parameters:</p>
<ul>
<li><code>number: The phone number of the recipient</code></li>
<li><code>message: The message to send</code></li>
<ul>
<p>Example:</p>
<ul>
<li><code>/api/send?number=01234567890&message="Hello World"</code></li>
</ul>
</ul>
</body>
</html>
'''
@app.route('/mode', methods=['GET'])
def mode():
args = request.args
mode = args.get('mode')
color = args.get('color')
if mode is None or color is None:
return 'No mode specified'
elif mode == "" or color == "":
return 'No mode specified'
elif mode == "0":
mode = 0
return 'Rainbow Cycle'
elif mode == "1":
mode = 1
return color
# return 'Set Color'
@app.route('/power', methods=['GET'])
def power():
if power == True:
power = False
return "Power off"
else:
power = True
return "Power on"
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
strip.begin()
app.run(host='0.0.0.0', port=80)
try:
while True:
if power == True:
time.sleep(1)
else:
setColor(strip, Color(0,0,0))
time.sleep(1)
except KeyboardInterrupt:
setColor(strip, Color(0,0,0))
GPIO.cleanup()