From 7326553fc924b631e7c924e20701f6e008bbb3d7 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Thu, 10 Oct 2024 12:29:09 +0100 Subject: [PATCH] Tidied up random wireless example --- examples/tiny_fx_w/README.md | 16 ++-- .../tiny_fx_w/examples/wifi/random_colour.py | 63 ---------------- .../tiny_fx_w/examples/wireless/random.py | 74 +++++++++++++++++++ examples/tiny_fx_w/secrets.py | 3 +- 4 files changed, 86 insertions(+), 70 deletions(-) delete mode 100644 examples/tiny_fx_w/examples/wifi/random_colour.py create mode 100644 examples/tiny_fx_w/examples/wireless/random.py diff --git a/examples/tiny_fx_w/README.md b/examples/tiny_fx_w/README.md index 50ac6f6..28e8606 100644 --- a/examples/tiny_fx_w/README.md +++ b/examples/tiny_fx_w/README.md @@ -4,13 +4,19 @@ These are micropython examples for the wireless functionality of the Pimoroni [T For examples that show off the rest of the board's functions, refer to the regular [TinyFX Micropython Examples](../tiny_fx/README.md) -- [WiFi Examples](#wifi-examples) - - [Random Colour](#random-colour) +- [Wireless Examples](#wireless-examples) + - [Random](#random) -## WiFi Examples +## Wireless Examples -### Random Colour -[wifi/random_colour.py](examples/wifi/random_colour.py) +These examples requires a `secrets.py` file to be on your board's file system with the credentials of your WiFi network. + +### Random +[wireless/random.py](examples/wireless/random.py) Show the state of TinyFX's Boot button on its RGB output. +Show random colours and patterns obtained from the internet on TinyFX's outputs. + + + diff --git a/examples/tiny_fx_w/examples/wifi/random_colour.py b/examples/tiny_fx_w/examples/wifi/random_colour.py deleted file mode 100644 index a63790a..0000000 --- a/examples/tiny_fx_w/examples/wifi/random_colour.py +++ /dev/null @@ -1,63 +0,0 @@ -import time -import network -import requests -from tiny_fx import TinyFX - -""" -Press "Boot" to exit the program. -""" -# secrets.py should contain: -# WIFI_SSID = "" -# WIFI_PASSWORD = "" - -try: - from secrets import WIFI_SSID, WIFI_PASSWORD -except ImportError: - print("Create secrets.py with your WiFi credentials") - - -# Constants -MONO_NAMES = ("One", "Two", "Three", "Four", "Five", "Six") -COLOUR_NAMES = ("R", "G", "B") - -# Variables -tiny = TinyFX() # Create a new TinyFX object to interact with the board - - -# Connect to WLAN -wlan = network.WLAN(network.STA_IF) -wlan.active(True) -wlan.connect(WIFI_SSID, WIFI_PASSWORD) - -while wlan.isconnected() == False: - print('Waiting for connection...') - time.sleep(1) - -ip = wlan.ifconfig()[0] -print(f'Connected on {ip}') - - -# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) -try: - while True: - req = requests.get("https://random-flat-colors.vercel.app/api/random?count=2").json() - - mono = tuple(int(req['colors'][0][i:i+1], 16) / 15 for i in range(1, 7)) - colour = tuple(int(req['colors'][1][i:i+2], 16) for i in (1, 3, 5)) - - for i in range(len(tiny.outputs)): - tiny.outputs[i].brightness(mono[i]) - print(f"{MONO_NAMES[i]} = {round(mono[i], 2)}", end=", ") - - tiny.rgb.set_rgb(*colour) - for i in range(len(colour)): - print(f"{COLOUR_NAMES[i]} = {colour[i]}", end=", ") - - print() - - time.sleep(5) - -# Turn off all the outputs -finally: - tiny.shutdown() - wlan.disconnect() \ No newline at end of file diff --git a/examples/tiny_fx_w/examples/wireless/random.py b/examples/tiny_fx_w/examples/wireless/random.py new file mode 100644 index 0000000..62bb471 --- /dev/null +++ b/examples/tiny_fx_w/examples/wireless/random.py @@ -0,0 +1,74 @@ +import time +import network +import requests +from tiny_fx import TinyFX + +""" +Show random colours and patterns obtained from the internet on TinyFX's outputs. + +This example requires a secrets.py file to be on your board's file system with the credentials of your WiFi network. + +Press "Boot" to exit the program. +""" + +try: + from secrets import WIFI_SSID, WIFI_PASSWORD +except ImportError: + print("Create secrets.py with your WiFi credentials") + raise + + +# Constants +MONO_NAMES = ("One", "Two", "Three", "Four", "Five", "Six") +COLOUR_NAMES = ("R", "G", "B") +CONNECTION_INTERVAL = 1.0 # The time to sleep between each connection check +REQUEST_INTERVAL = 5.0 # The time to sleep between each internet request + +# Variables +tiny = TinyFX() # Create a new TinyFX object to interact with the board +wlan = network.WLAN(network.STA_IF) # Create a new network object for interacting with WiFI + + +# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) +try: + # Connect to WLAN + wlan.active(True) + wlan.connect(WIFI_SSID, WIFI_PASSWORD) + + # Wait until the connection is established + while not wlan.isconnected(): + print('Waiting for connection...') + time.sleep(CONNECTION_INTERVAL) + + # Print out our IP address + print(f'Connected on {wlan.ifconfig()[0]}') + + # Loop forever + while True: + # Get two colours from the internet + req = requests.get("https://random-flat-colors.vercel.app/api/random?count=2").json() + + # Use the first to get brightness values for the six mono outputs + mono = tuple(int(req['colors'][0][i:i + 1], 16) / 15 for i in range(1, 7)) + + # Use the second to get the colour components for the RGB output + colour = tuple(int(req['colors'][1][i:i + 2], 16) for i in (1, 3, 5)) + + # Set the mono outputs, and print the values + for i in range(len(tiny.outputs)): + tiny.outputs[i].brightness(mono[i]) + print(f"{MONO_NAMES[i]} = {round(mono[i], 2)}", end=", ") + + # Set the colour output, and print the values + tiny.rgb.set_rgb(*colour) + for i in range(len(colour)): + print(f"{COLOUR_NAMES[i]} = {colour[i]}", end=", ") + + print() + + time.sleep(REQUEST_INTERVAL) + +# Turn off all the outputs +finally: + tiny.shutdown() + wlan.disconnect() diff --git a/examples/tiny_fx_w/secrets.py b/examples/tiny_fx_w/secrets.py index ad19206..f290ec1 100644 --- a/examples/tiny_fx_w/secrets.py +++ b/examples/tiny_fx_w/secrets.py @@ -1,3 +1,2 @@ -# secrets.py should contain: WIFI_SSID = "" -WIFI_PASSWORD = "" \ No newline at end of file +WIFI_PASSWORD = ""