parent
b2825bed3d
commit
3d0c6561a8
139
main.py
139
main.py
|
@ -2,22 +2,10 @@ from base64 import b16decode
|
|||
import time
|
||||
from datetime import datetime
|
||||
from random import *
|
||||
from ctypes.wintypes import VARIANT_BOOL
|
||||
from multiprocessing.spawn import old_main_modules
|
||||
import os
|
||||
from unicodedata import decimal
|
||||
import requests
|
||||
current_path = os.path.dirname(os.path.abspath(__file__))
|
||||
try:
|
||||
from rpi_ws281x import *
|
||||
import RPi.GPIO as GPIO
|
||||
import paho.mqtt.client as mqtt
|
||||
import spotipy
|
||||
import urllib.request
|
||||
import colorgram
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
from dotenv import load_dotenv
|
||||
except ImportError:
|
||||
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
|
||||
exit()
|
||||
|
@ -44,29 +32,6 @@ def debug_print(message: str):
|
|||
current_time = now.strftime("%H:%M:%S")
|
||||
print('[DEBUG]['+current_time+'] '+message)
|
||||
|
||||
def convertColor(hexCode):
|
||||
R = int(hexCode[:2],16)
|
||||
G = int(hexCode[2:4],16)
|
||||
B = int(hexCode[4:6],16)
|
||||
total = R + G + B
|
||||
if R == 0:
|
||||
firstPos = 0
|
||||
else:
|
||||
firstPos = R / total
|
||||
if G == 0:
|
||||
secondPos = 0
|
||||
else:
|
||||
secondPos = G / total
|
||||
return [firstPos, secondPos]
|
||||
|
||||
def getRandomHex():
|
||||
return "%06x" % random.randint(0, 0xFFFFFF)
|
||||
|
||||
def extract_colors(url, n=4):
|
||||
urllib.request.urlretrieve(url, current_path+r"/cover.jpg")
|
||||
img = current_path+r"/cover.jpg"
|
||||
return colorgram.extract(img, n)
|
||||
|
||||
def wheel(pos):
|
||||
if pos < 85:
|
||||
return Color(pos * 3, 255 - pos * 3, 0)
|
||||
|
@ -155,73 +120,49 @@ if __name__ == '__main__':
|
|||
mode = 3
|
||||
elif payload == "Random Color per LED":
|
||||
mode = 4
|
||||
|
||||
if os.path.isfile(current_path+r'/.env') == False:
|
||||
# If not create it and add content
|
||||
with open(current_path+r'/.env', 'w') as f:
|
||||
var_lines = ["SPOTIPY_CLIENT_ID = ", "SPOTIPY_CLIENT_SECRET = ", "SPOTIPY_USER_NAME = ", "SPOTIPY_REDIRECT_URI = "]
|
||||
for var_line in var_lines:
|
||||
f.write(var_line+'\n')
|
||||
print('Please fill out the variables in the .env file ( '+current_path+r'/.env )')
|
||||
exit()
|
||||
else:
|
||||
load_dotenv()
|
||||
|
||||
sp_scopes = 'user-read-currently-playing user-read-playback-state streaming user-read-email user-read-private user-read-currently-playing user-read-playback-position user-read-playback-state'
|
||||
global sp
|
||||
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=sp_scopes))
|
||||
one_device_is_active = False
|
||||
for x in sp.devices()['devices']:
|
||||
if x['is_active'] == True:
|
||||
try:
|
||||
sp.start_playback(x['id'])
|
||||
except:
|
||||
pass
|
||||
one_device_is_active = True
|
||||
break
|
||||
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
|
||||
strip.begin()
|
||||
|
||||
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
|
||||
strip.begin()
|
||||
mode = 0
|
||||
power = "True"
|
||||
global r
|
||||
r = 255
|
||||
global g
|
||||
g = 255
|
||||
global b
|
||||
b = 255
|
||||
|
||||
mode = 0
|
||||
power = "True"
|
||||
global r
|
||||
r = 255
|
||||
global g
|
||||
g = 255
|
||||
global b
|
||||
b = 255
|
||||
mqttBroker ="homeassistant.ping-mee.local"
|
||||
client = mqtt.Client("server_rack_led")
|
||||
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
|
||||
|
||||
mqttBroker ="homeassistant.ping-mee.local"
|
||||
client = mqtt.Client("server_rack_led")
|
||||
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
|
||||
client.publish("server-rack-led/power", "True")
|
||||
client.publish("server-rack-led/mode", "Rainbow")
|
||||
|
||||
client.publish("server-rack-led/power", "True")
|
||||
client.publish("server-rack-led/mode", "Rainbow")
|
||||
try:
|
||||
client.loop_start()
|
||||
while True:
|
||||
if power == "True":
|
||||
if mode == 0:
|
||||
rainbowCycle(strip)
|
||||
elif mode == 1:
|
||||
setColor(strip, Color(int(r), int(g), int(b)))
|
||||
elif mode == 2:
|
||||
strobe(strip, Color(int(r), int(g), int(b)))
|
||||
elif mode == 3:
|
||||
randomColor(strip)
|
||||
elif mode == 4:
|
||||
randomColorPerLED(strip)
|
||||
else:
|
||||
setColor(strip, Color(0,0,0))
|
||||
|
||||
try:
|
||||
client.loop_start()
|
||||
while True:
|
||||
if power == "True":
|
||||
if mode == 0:
|
||||
rainbowCycle(strip)
|
||||
elif mode == 1:
|
||||
setColor(strip, Color(int(r), int(g), int(b)))
|
||||
elif mode == 2:
|
||||
strobe(strip, Color(int(r), int(g), int(b)))
|
||||
elif mode == 3:
|
||||
randomColor(strip)
|
||||
elif mode == 4:
|
||||
randomColorPerLED(strip)
|
||||
else:
|
||||
setColor(strip, Color(0,0,0))
|
||||
|
||||
except KeyboardInterrupt:
|
||||
client.publish("server-rack-led/power", "False")
|
||||
client.loop_stop()
|
||||
setColor(strip, Color(0,0,0))
|
||||
GPIO.cleanup()
|
||||
except KeyboardInterrupt:
|
||||
client.publish("server-rack-led/power", "False")
|
||||
client.loop_stop()
|
||||
setColor(strip, Color(0,0,0))
|
||||
GPIO.cleanup()
|
||||
|
|
Loading…
Reference in New Issue