From 441cdd892d7d23db06a4add206e744c139ad62cb Mon Sep 17 00:00:00 2001 From: thirdr Date: Mon, 4 Nov 2024 16:26:15 +0000 Subject: [PATCH] Plasma 2350 W: cheerlight example --- .../examples/plasma_2350_w/cheerlights.py | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 micropython/examples/plasma_2350_w/cheerlights.py diff --git a/micropython/examples/plasma_2350_w/cheerlights.py b/micropython/examples/plasma_2350_w/cheerlights.py new file mode 100644 index 0000000..15ee921 --- /dev/null +++ b/micropython/examples/plasma_2350_w/cheerlights.py @@ -0,0 +1,74 @@ +import plasma +from plasma import plasma2040 +import network +import time +import requests + +# Total number of LEDs on our LED strip +NUM_LEDS = 60 + +# How long between cheerslight updates in seconds +INTERVAL = 60 + +# Check and import the SSID and Password from secrets.py +try: + from secrets import WIFI_SSID, WIFI_PASSWORD + if WIFI_SSID == "": + raise ValueError("WIFI_SSID in 'secrets.py' is empty!") + if WIFI_PASSWORD == "": + raise ValueError("WIFI_PASSWORD in 'secrets.py' is empty!") +except ImportError: + raise ImportError("'secrets.py' is missing from your Plasma 2350 W!") +except ValueError as e: + print(e) + +wlan = network.WLAN(network.STA_IF) + + +def connect(): + # Connect to the network specified in secrets.py + wlan.active(True) + wlan.connect(WIFI_SSID, WIFI_PASSWORD) + while wlan.isconnected() is False: + print("Attempting connection to {}".format(WIFI_SSID)) + time.sleep(1) + + +# APA102 / DotStarâ„¢ LEDs +# led_strip = plasma.APA102(NUM_LEDS, 0, 0, plasma2040.DAT, plasma2040.CLK) + +# WS2812 / NeoPixelâ„¢ LEDs +led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT) + +# Start connection to the network +connect() + +# Store the local IP address +ip_addr = wlan.ipconfig('addr4')[0] + +# Let the user know the connection has been successful +# and display the current IP address of the Plasma 2350 W +print("Successfully connected to {}. Your Plasma 2350 W's IP is: {}".format(WIFI_SSID, ip_addr)) + +# Start updating the LED strip +led_strip.start() + +while True: + if wlan.isconnected(): + try: + print("Getting new colour...") + req = requests.get("http://api.thingspeak.com/channels/1417/field/2/last.json", timeout=None) + json = req.json() + req.close() + print("Success!") + + colour = tuple(int(json['field2'][i:i + 2], 16) for i in (1, 3, 5)) + + for i in range(NUM_LEDS): + led_strip.set_rgb(i, *colour) + except OSError: + print("Error: Failed to get new colour") + else: + print("Lost connection to network {}".format(WIFI_SSID)) + + time.sleep(INTERVAL)