From 7eff2c71a36f11f39da36dd1be05af5d12f1150b Mon Sep 17 00:00:00 2001 From: thirdr Date: Mon, 4 Nov 2024 15:03:24 +0000 Subject: [PATCH 1/3] Plasma 2350 W: web based example --- .../examples/plasma_2350_w/html/index.html | 19 +++++ micropython/examples/plasma_2350_w/secrets.py | 2 + micropython/examples/plasma_2350_w/webpage.py | 84 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 micropython/examples/plasma_2350_w/html/index.html create mode 100644 micropython/examples/plasma_2350_w/secrets.py create mode 100644 micropython/examples/plasma_2350_w/webpage.py diff --git a/micropython/examples/plasma_2350_w/html/index.html b/micropython/examples/plasma_2350_w/html/index.html new file mode 100644 index 0000000..734b601 --- /dev/null +++ b/micropython/examples/plasma_2350_w/html/index.html @@ -0,0 +1,19 @@ + + + +Plasma 2350 W + + + +

Plasma 2350 W

+

You can use this webpage to change the colour of your attached LED strip! 🌈

+ +
+ +
+
+ +
+ + + \ No newline at end of file diff --git a/micropython/examples/plasma_2350_w/secrets.py b/micropython/examples/plasma_2350_w/secrets.py new file mode 100644 index 0000000..f290ec1 --- /dev/null +++ b/micropython/examples/plasma_2350_w/secrets.py @@ -0,0 +1,2 @@ +WIFI_SSID = "" +WIFI_PASSWORD = "" diff --git a/micropython/examples/plasma_2350_w/webpage.py b/micropython/examples/plasma_2350_w/webpage.py new file mode 100644 index 0000000..9ea3f08 --- /dev/null +++ b/micropython/examples/plasma_2350_w/webpage.py @@ -0,0 +1,84 @@ +''' +A simple web example that allows you to set the LED Strip colour using a colour picker. +You will need to install Phew for this example to work +In Thonny select Tools > Manage Packages and search for "micropython-phew" +''' + +import network +from pimoroni import RGBLED +from phew import server, connect_to_wifi, is_connected_to_wifi +from phew.template import render_template +import plasma + +# 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) + +# Total number of LEDs on our LED strip +NUM_LEDS = 60 + +# 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, plasma.plasma2040.DAT) + +# Setup the RGB Led +led = RGBLED(16, 17, 18) + +last_hex = 0 + +# Connect our Tiny FX W to the network +connect_to_wifi(WIFI_SSID, WIFI_PASSWORD) + +# If we're connected to the network, print out the IP address in the terminal +if is_connected_to_wifi(): + print("Connected") + print(network.WLAN(network.STA_IF).ifconfig()[0]) + +# Start updating the LED strip +led_strip.start() + + +# Convert our hex value to RGB +# https://www.30secondsofcode.org/python/s/hex-to-rgb/ +def hex_to_rgb(hex): + return tuple(int(hex[i:i + 2], 16) for i in (0, 2, 4)) + + +# We've only got one page in the root so we setup the routing for that request +# and return the index.html +@server.route("/", methods=["GET", "POST"]) +def index(request): + global last_hex + # If a GET request is made, return the page + if request.method == 'GET': + return render_template("html/index.html") + + # If a POST request is made we need to process the information + # and use that to update the LED colour + if request.method == 'POST': + last_hex = request.form["rgbled"] + value = hex_to_rgb(last_hex[1:]) + led.set_rgb(value[0], value[1], value[2]) + for i in range(NUM_LEDS): + led_strip.set_rgb(i, value[0], value[1], value[2]) + return render_template("html/index.html") + + +# catchall +@server.catchall() +def catchall(request): + return "Not found", 404 + + +# Run the server! +server.run() From 441cdd892d7d23db06a4add206e744c139ad62cb Mon Sep 17 00:00:00 2001 From: thirdr Date: Mon, 4 Nov 2024 16:26:15 +0000 Subject: [PATCH 2/3] 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) From 430c5a163832eb0fa2d17a8ae25bb01ce112a2f0 Mon Sep 17 00:00:00 2001 From: thirdr Date: Thu, 21 Nov 2024 16:59:34 +0000 Subject: [PATCH 3/3] Examples: updated for kit compatibility --- micropython/examples/plasma_2350_w/cheerlights.py | 4 ++-- micropython/examples/plasma_2350_w/webpage.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/micropython/examples/plasma_2350_w/cheerlights.py b/micropython/examples/plasma_2350_w/cheerlights.py index 15ee921..ad2e4ff 100644 --- a/micropython/examples/plasma_2350_w/cheerlights.py +++ b/micropython/examples/plasma_2350_w/cheerlights.py @@ -5,7 +5,7 @@ import time import requests # Total number of LEDs on our LED strip -NUM_LEDS = 60 +NUM_LEDS = 66 # How long between cheerslight updates in seconds INTERVAL = 60 @@ -38,7 +38,7 @@ def connect(): # 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) +led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT, color_order=plasma.COLOR_ORDER_BGR) # Start connection to the network connect() diff --git a/micropython/examples/plasma_2350_w/webpage.py b/micropython/examples/plasma_2350_w/webpage.py index 9ea3f08..cf15bc0 100644 --- a/micropython/examples/plasma_2350_w/webpage.py +++ b/micropython/examples/plasma_2350_w/webpage.py @@ -23,13 +23,13 @@ except ValueError as e: print(e) # Total number of LEDs on our LED strip -NUM_LEDS = 60 +NUM_LEDS = 66 # 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, plasma.plasma2040.DAT) +led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma.plasma2040.DAT, color_order=plasma.COLOR_ORDER_BGR) # Setup the RGB Led led = RGBLED(16, 17, 18)