parent
b5f5be60d7
commit
39e4552a36
161
main.py
161
main.py
|
@ -4,19 +4,12 @@ from datetime import datetime
|
||||||
try:
|
try:
|
||||||
from rpi_ws281x import *
|
from rpi_ws281x import *
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
import socket
|
from flask import Flask
|
||||||
|
import json
|
||||||
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()
|
||||||
|
|
||||||
led_power = True
|
|
||||||
idle_mode = 1
|
|
||||||
global power_button_pressed
|
|
||||||
power_button_pressed = False
|
|
||||||
|
|
||||||
SOCKET_BIND_IP = '0.0.0.0'
|
|
||||||
SOCKET_BIND_PORT = 5760
|
|
||||||
|
|
||||||
# LED strip configuration:
|
# LED strip configuration:
|
||||||
LED_COUNT = 60 # Number of LED pixels.
|
LED_COUNT = 60 # Number of LED pixels.
|
||||||
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
|
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
|
||||||
|
@ -27,6 +20,9 @@ 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
|
||||||
|
|
||||||
|
mode = 0
|
||||||
|
power = True
|
||||||
|
|
||||||
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")
|
||||||
|
@ -44,9 +40,7 @@ def wheel(pos):
|
||||||
|
|
||||||
def rainbowCycle(strip, wait_ms=5, iterations=10):
|
def rainbowCycle(strip, wait_ms=5, iterations=10):
|
||||||
for j in range(256*iterations):
|
for j in range(256*iterations):
|
||||||
if power_button_pressed == True:
|
if mode > 0:
|
||||||
break
|
|
||||||
elif idle_mode > 1:
|
|
||||||
break
|
break
|
||||||
for i in range(strip.numPixels()):
|
for i in range(strip.numPixels()):
|
||||||
strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels()) + j) & 255))
|
strip.setPixelColor(i, wheel((int(i * 256 / strip.numPixels()) + j) & 255))
|
||||||
|
@ -59,67 +53,98 @@ def setColor(strip, color, wait_ms=10):
|
||||||
strip.show()
|
strip.show()
|
||||||
time.sleep(wait_ms/1000.0)
|
time.sleep(wait_ms/1000.0)
|
||||||
|
|
||||||
def blink(strip, color1, color2):
|
|
||||||
for i in range(strip.numPixels()):
|
|
||||||
strip.setPixelColor(i, color1)
|
|
||||||
strip.show()
|
|
||||||
time.sleep(1)
|
|
||||||
for i in range(strip.numPixels()):
|
|
||||||
strip.setPixelColor(i, color2)
|
|
||||||
strip.show()
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
def change_idle_mode(channel):
|
|
||||||
global idle_mode
|
|
||||||
idle_mode += 1
|
|
||||||
if idle_mode == 8:
|
|
||||||
idle_mode = 1
|
|
||||||
debug_print('Change Idlemode executed')
|
|
||||||
|
|
||||||
def power_toggle(channel):
|
|
||||||
if led_power == True:
|
|
||||||
led_power = False
|
|
||||||
debug_print('LED Power OFF')
|
|
||||||
else:
|
|
||||||
led_power = True
|
|
||||||
debug_print('LED Power ON')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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 = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
|
||||||
strip.begin()
|
strip.begin()
|
||||||
GPIO.setmode(GPIO.BOARD)
|
|
||||||
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|
||||||
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|
||||||
GPIO.add_event_detect(13, GPIO.RISING, callback=power_toggle, bouncetime=300)
|
|
||||||
GPIO.add_event_detect(19, GPIO.RISING, callback=change_idle_mode, bouncetime=300)
|
|
||||||
|
|
||||||
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
||||||
# s.bind((SOCKET_BIND_IP, SOCKET_BIND_PORT))
|
|
||||||
# s.listen()
|
|
||||||
# conn, addr = s.accept()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
# socket_data = conn.recv(1024)
|
if power == True:
|
||||||
# print(socket_data)
|
|
||||||
# print(type(socket_data))
|
|
||||||
if led_power == True:
|
|
||||||
if idle_mode == 1:
|
|
||||||
rainbowCycle(strip)
|
|
||||||
elif idle_mode == 2:
|
|
||||||
setColor(strip, Color(255,0,0))
|
|
||||||
elif idle_mode == 3:
|
|
||||||
setColor(strip, Color(0,255,0))
|
|
||||||
elif idle_mode == 4:
|
|
||||||
setColor(strip, Color(0,0,255))
|
|
||||||
elif idle_mode == 5:
|
|
||||||
setColor(strip, Color(125,245,255))
|
|
||||||
elif idle_mode == 6:
|
|
||||||
setColor(strip, Color(255,0,100))
|
|
||||||
elif idle_mode == 7:
|
|
||||||
setColor(strip, Color(0,255,255))
|
|
||||||
else:
|
|
||||||
blink(strip, Color(255,0,0), Color(0,0,0))
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
else:
|
else:
|
||||||
setColor(strip, Color(0,0,0))
|
setColor(strip, Color(0,0,0))
|
||||||
|
|
Loading…
Reference in New Issue