|
| 1 | +import time |
| 2 | + |
| 3 | +import urequests |
| 4 | +from ezwifi import connect |
| 5 | +from machine import Pin |
| 6 | + |
| 7 | +import plasma |
| 8 | + |
| 9 | +""" |
| 10 | +This Plasma Stick example sets your LED strip to the current #cheerlights colour. |
| 11 | +Find out more about the Cheerlights API at https://cheerlights.com/ |
| 12 | +""" |
| 13 | + |
| 14 | +URL = 'http://api.thingspeak.com/channels/1417/field/2/last.json' |
| 15 | +UPDATE_INTERVAL = 120 # refresh interval in secs. Be nice to free APIs! |
| 16 | + |
| 17 | +# Set how many LEDs you have |
| 18 | +NUM_LEDS = 50 |
| 19 | + |
| 20 | +# Set the brightness |
| 21 | +BRIGHTNESS = 0.5 |
| 22 | + |
| 23 | + |
| 24 | +# if no wifi connection, you get spooky rainbows. Bwahahaha! |
| 25 | +def wifi_failed(message=""): |
| 26 | + print(f'Wifi connection failed! {message}') |
| 27 | + spooky_rainbows() |
| 28 | + |
| 29 | + |
| 30 | +# Print out WiFi connection messages for debugging |
| 31 | +def wifi_message(wifi, message): |
| 32 | + print(message) |
| 33 | + |
| 34 | + |
| 35 | +def spooky_rainbows(): |
| 36 | + print('SPOOKY RAINBOWS!') |
| 37 | + HUE_START = 30 # orange |
| 38 | + HUE_END = 140 # green |
| 39 | + SPEED = 0.3 # bigger = faster (harder, stronger) |
| 40 | + |
| 41 | + distance = 0.0 |
| 42 | + direction = SPEED |
| 43 | + while True: |
| 44 | + for i in range(NUM_LEDS): |
| 45 | + # generate a triangle wave that moves up and down the LEDs |
| 46 | + j = max(0, 1 - abs(distance - i) / (NUM_LEDS / 3)) |
| 47 | + hue = HUE_START + j * (HUE_END - HUE_START) |
| 48 | + |
| 49 | + led_strip.set_hsv(i, hue / 360, 1.0, BRIGHTNESS) |
| 50 | + |
| 51 | + # reverse direction at the end of colour segment to avoid an abrupt change |
| 52 | + distance += direction |
| 53 | + if distance > NUM_LEDS: |
| 54 | + direction = - SPEED |
| 55 | + if distance < 0: |
| 56 | + direction = SPEED |
| 57 | + |
| 58 | + time.sleep(0.01) |
| 59 | + |
| 60 | + |
| 61 | +def hex_to_rgb(hex): |
| 62 | + # converts a hex colour code into RGB |
| 63 | + h = hex.lstrip('#') |
| 64 | + r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4)) |
| 65 | + return r, g, b |
| 66 | + |
| 67 | + |
| 68 | +# set up the Pico W's onboard LED |
| 69 | +pico_led = Pin('LED', Pin.OUT) |
| 70 | + |
| 71 | +# set up the WS2812 / NeoPixel™ LEDs |
| 72 | +led_strip = plasma.WS2812(NUM_LEDS, color_order=plasma.COLOR_ORDER_RGB) |
| 73 | + |
| 74 | +# start updating the LED strip |
| 75 | +led_strip.start() |
| 76 | + |
| 77 | +# set up wifi |
| 78 | +try: |
| 79 | + connect(failed=wifi_failed, info=wifi_message, warning=wifi_message, error=wifi_message) |
| 80 | +except ValueError as e: |
| 81 | + wifi_failed(e) |
| 82 | + |
| 83 | +while True: |
| 84 | + # open the json file |
| 85 | + print(f'Requesting URL: {URL}') |
| 86 | + r = urequests.get(URL) |
| 87 | + # open the json data |
| 88 | + j = r.json() |
| 89 | + print('Data obtained!') |
| 90 | + r.close() |
| 91 | + |
| 92 | + # flash the onboard LED after getting data |
| 93 | + pico_led.value(True) |
| 94 | + time.sleep(0.2) |
| 95 | + pico_led.value(False) |
| 96 | + |
| 97 | + # extract hex colour from the data |
| 98 | + hex = j['field2'] |
| 99 | + |
| 100 | + # and convert it to RGB |
| 101 | + r, g, b = hex_to_rgb(hex) |
| 102 | + |
| 103 | + # adjust the brightness |
| 104 | + r, g, b = (int(i * BRIGHTNESS) for i in (r, g, b)) |
| 105 | + |
| 106 | + # light up the LEDs |
| 107 | + for i in range(NUM_LEDS): |
| 108 | + led_strip.set_rgb(i, r, g, b) |
| 109 | + print(f'LEDs set to {hex}') |
| 110 | + |
| 111 | + # sleep |
| 112 | + print(f'Sleeping for {UPDATE_INTERVAL} seconds.') |
| 113 | + time.sleep(UPDATE_INTERVAL) |
0 commit comments