pimoroni-pico/micropython/examples/plasma_stick/cheerlights.py

120 wiersze
3.2 KiB
Python
Czysty Zwykły widok Historia

2022-10-04 18:17:09 +00:00
import WIFI_CONFIG
from network_manager import NetworkManager
import uasyncio
2022-10-05 11:17:00 +00:00
import urequests
2022-10-04 18:17:09 +00:00
import time
import plasma
from plasma import plasma_stick
2022-10-18 14:17:10 +00:00
from machine import Pin
2022-10-04 18:17:09 +00:00
2022-10-13 18:01:02 +00:00
'''
2022-10-04 18:17:09 +00:00
This Plasma Stick example sets your LED strip to the current #cheerlights colour.
Find out more about the Cheerlights API at https://cheerlights.com/
2022-10-13 18:01:02 +00:00
'''
2022-10-04 18:17:09 +00:00
URL = 'http://api.thingspeak.com/channels/1417/field/2/last.json'
UPDATE_INTERVAL = 120 # refresh interval in secs. Be nice to free APIs!
# Set how many LEDs you have
NUM_LEDS = 50
def status_handler(mode, status, ip):
# reports wifi connection status
print(mode, status, ip)
print('Connecting to wifi...')
# flash while connecting
for i in range(NUM_LEDS):
led_strip.set_rgb(i, 255, 255, 255)
time.sleep(0.02)
for i in range(NUM_LEDS):
led_strip.set_rgb(i, 0, 0, 0)
if status is not None:
if status:
2022-10-13 18:01:02 +00:00
print('Wifi connection successful!')
2022-10-04 18:17:09 +00:00
else:
2022-10-13 18:01:02 +00:00
print('Wifi connection failed!')
2022-10-13 17:15:35 +00:00
# if no wifi connection, you get spooky rainbows. Bwahahaha!
spooky_rainbows()
def spooky_rainbows():
2022-10-13 18:01:02 +00:00
print('SPOOKY RAINBOWS!')
2022-10-13 17:15:35 +00:00
HUE_START = 30 # orange
HUE_END = 140 # green
SPEED = 0.3 # bigger = faster (harder, stronger)
distance = 0.0
direction = SPEED
while True:
for i in range(NUM_LEDS):
# generate a triangle wave that moves up and down the LEDs
j = max(0, 1 - abs(distance - i) / (NUM_LEDS / 3))
hue = HUE_START + j * (HUE_END - HUE_START)
led_strip.set_hsv(i, hue / 360, 1.0, 0.8)
# reverse direction at the end of colour segment to avoid an abrupt change
distance += direction
if distance > NUM_LEDS:
direction = - SPEED
if distance < 0:
direction = SPEED
time.sleep(0.01)
2022-10-05 11:17:00 +00:00
2022-10-04 18:17:09 +00:00
def hex_to_rgb(hex):
# converts a hex colour code into RGB
h = hex.lstrip('#')
r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))
return r, g, b
2022-10-18 14:17:10 +00:00
# set up the Pico W's onboard LED
pico_led = Pin('LED', Pin.OUT)
2022-10-04 18:17:09 +00:00
# set up the WS2812 / NeoPixel™ LEDs
2022-10-19 15:31:36 +00:00
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma_stick.DAT, color_order=plasma.COLOR_ORDER_RGB)
2022-10-04 18:17:09 +00:00
# start updating the LED strip
led_strip.start()
# set up wifi
2022-10-13 18:01:02 +00:00
try:
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
except Exception as e:
print(f'Wifi connection failed! {e}')
# if no wifi, then you get...
spooky_rainbows()
2022-10-04 18:17:09 +00:00
while True:
# open the json file
2022-10-13 18:01:02 +00:00
print(f'Requesting URL: {URL}')
2022-10-05 11:17:00 +00:00
r = urequests.get(URL)
# open the json data
j = r.json()
2022-10-13 18:01:02 +00:00
print('Data obtained!')
2022-10-05 11:17:00 +00:00
r.close()
2022-10-04 18:17:09 +00:00
2022-10-18 14:17:10 +00:00
# flash the onboard LED after getting data
pico_led.value(True)
time.sleep(0.2)
pico_led.value(False)
2022-10-04 18:17:09 +00:00
# extract hex colour from the data
2022-10-13 18:01:02 +00:00
hex = j['field2']
2022-10-04 18:17:09 +00:00
# and convert it to RGB
r, g, b = hex_to_rgb(hex)
2022-10-05 11:17:00 +00:00
2022-10-04 18:17:09 +00:00
# light up the LEDs
for i in range(NUM_LEDS):
led_strip.set_rgb(i, r, g, b)
2022-10-13 18:01:02 +00:00
print(f'LEDs set to {hex}')
2022-10-04 18:17:09 +00:00
# sleep
2022-10-13 18:01:02 +00:00
print(f'Sleeping for {UPDATE_INTERVAL} seconds.')
2022-10-05 11:17:00 +00:00
time.sleep(UPDATE_INTERVAL)