from base64 import b16decode import time import os from datetime import datetime from requests import request try: from rpi_ws281x import * import RPi.GPIO as GPIO from flask import Flask, render_template, request, redirect, url_for, flash, jsonify import json from argparse import ArgumentParser import threading import paho.mqtt.client as mqtt 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 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 elif power == "False": 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) def mqtt_on_connect(client, userdata, flags, rc): #sub to status client.subscribe("server-rack-led-1/power/status") client.subscribe("server-rack-led-1/mode/status") client.subscribe("server-rack-led-1/r/status") client.subscribe("server-rack-led-1/g/status") client.subscribe("server-rack-led-1/b/status") def mqtt_on_message(client, userdata, msg): print(f"Message received [{msg.topic}]: {msg.payload}") if __name__ == '__main__': app = Flask(__name__) #api @app.route('/api/') def index(): return ''' Server Rack LED stripe

My Server Rack LED stripe control API


How to use the API:

/power?power=True
/power?power=False
/mode?mode=0
/mode?mode=1&r=0&g=0&b=0 ''' @app.route('/api/mode', methods=['GET']) def mode(): global mode global r global g global b args = request.args mode = str(args.get('mode')) r = str(args.get('r')) g = str(args.get('g')) b = str(args.get('b')) if mode is None: return 'No mode specified' elif mode == "": return 'No mode specified' elif mode == "0": mode = 0 return 'Rainbow Cycle' elif mode == "1": 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) @app.route('/api/power', methods=['GET']) def power(): global power power = request.args.get('power') if power is None: return 'No power argument specified' elif power == "": return 'No power argument specified' elif power == "True": power = "True" return 'Power: ON' elif power == "False": power = "False" return 'Power: OFF' strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL) strip.begin() mode = 0 power = "True" 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.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 threading.Thread(target=lambda: client.loop_forever()) try: while True: if power == "True": client.publish("server-rack-led-1/power/status","True") if mode == 0: client.publish("server-rack-led-1/mode/status","0") rainbowCycle(strip) elif mode == 1: client.publish("server-rack-led-1/mode/status","1") client.publish("server-rack-led-1/r/status",str(r)) client.publish("server-rack-led-1/g/status",str(g)) client.publish("server-rack-led-1/b/status",str(b)) setColor(strip, Color(int(r), int(g), int(b))) time.sleep(1) else: client.publish("server-rack-led-1/power/status","False") setColor(strip, Color(0,0,0)) time.sleep(1) except KeyboardInterrupt: client.disconnect() setColor(strip, Color(0,0,0)) GPIO.cleanup()