From 816228ebfe4496289f8ca5a3723595f6177ba76c Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Mon, 16 Dec 2024 16:35:49 +0000 Subject: [PATCH] Added basic wireless module examples --- examples/modules/rm2_wireless/README.md | 33 +++++++++++++ .../rm2_wireless}/cheerlights.py | 3 +- .../modules/rm2_wireless/detect_module.py | 25 ++++++++++ examples/modules/rm2_wireless/wifi_scan.py | 49 +++++++++++++++++++ examples/wireless/README.md | 14 ------ 5 files changed, 108 insertions(+), 16 deletions(-) create mode 100644 examples/modules/rm2_wireless/README.md rename examples/{wireless => modules/rm2_wireless}/cheerlights.py (98%) create mode 100644 examples/modules/rm2_wireless/detect_module.py create mode 100644 examples/modules/rm2_wireless/wifi_scan.py delete mode 100644 examples/wireless/README.md diff --git a/examples/modules/rm2_wireless/README.md b/examples/modules/rm2_wireless/README.md new file mode 100644 index 0000000..2b61310 --- /dev/null +++ b/examples/modules/rm2_wireless/README.md @@ -0,0 +1,33 @@ +# RM2 Wireless Module - Micropython Examples + + + +These are micropython examples for the [RM2 Wireless Module for Yukon](https://shop.pimoroni.com/products/rm2-wireless-module-for-yukon). + +- [Examples](#examples) + - [Detect Module](#detect-module) + - [WiFi Scan](#wifi-scan) + - [Cheerlights](#cheerlights) + + +## Examples + +### Detect Module +[detect_module.py](detect_module.py) + +A boilerplate example showing how to detect if the RM2 Wireless Module is attached to Yukon prior to performing any wireless operations. + + +### WiFi Scan +[wifi_scan.py](wifi_scan.py) + +Periodically scan for available WiFi networks using a RM2 Wireless Module connected to Slot 5, and print out their details. + + +### Cheerlights + +[cheerlights.py](cheerlights.py) + +Obtain the current CheerLights colour from the internet and show it on an LED Strip connected to Yukon. For more information about CheerLights, visit: https://cheerlights.com/ + +This example requires a secrets.py file to be on your board's file system with the credentials of your WiFi network. \ No newline at end of file diff --git a/examples/wireless/cheerlights.py b/examples/modules/rm2_wireless/cheerlights.py similarity index 98% rename from examples/wireless/cheerlights.py rename to examples/modules/rm2_wireless/cheerlights.py index 374e4a2..41d9483 100644 --- a/examples/wireless/cheerlights.py +++ b/examples/modules/rm2_wireless/cheerlights.py @@ -1,4 +1,3 @@ -import time import network import requests from pimoroni_yukon import Yukon @@ -61,7 +60,7 @@ # Wait until the connection is established while not wlan.isconnected(): print('Waiting for connection...') - time.sleep(CONNECTION_INTERVAL) + yukon.monitored_sleep(CONNECTION_INTERVAL) # Print out our IP address print(f'Connected on {wlan.ifconfig()[0]}') diff --git a/examples/modules/rm2_wireless/detect_module.py b/examples/modules/rm2_wireless/detect_module.py new file mode 100644 index 0000000..0ef71b3 --- /dev/null +++ b/examples/modules/rm2_wireless/detect_module.py @@ -0,0 +1,25 @@ +from pimoroni_yukon import Yukon +from pimoroni_yukon import SLOT5 as SLOT +from pimoroni_yukon.modules import WirelessModule + +""" +A boilerplate example showing how to detect if the RM2 Wireless Module +is attached to Yukon prior to performing any wireless operations. +""" + +# Variables +yukon = Yukon() # Create a new Yukon object, with a lower voltage limit set +module = WirelessModule() # Create a WirelessModule object + +# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) +try: + yukon.register_with_slot(module, SLOT) # Register the WirelessModule object with the slot + yukon.verify_and_initialise() # Verify that a WirelessModule is attached to Yukon, and initialise it + + # Do wireless things here + +finally: + # Put the board back into a safe state, regardless of how the program may have ended + yukon.reset() + + # Disconnect from wireless here diff --git a/examples/modules/rm2_wireless/wifi_scan.py b/examples/modules/rm2_wireless/wifi_scan.py new file mode 100644 index 0000000..f820fa8 --- /dev/null +++ b/examples/modules/rm2_wireless/wifi_scan.py @@ -0,0 +1,49 @@ +import network +import binascii +from pimoroni_yukon import Yukon +from pimoroni_yukon import SLOT5 as SLOT +from pimoroni_yukon.modules import WirelessModule + +""" +Periodically scan for available WiFi networks using a RM2 Wireless Module connected to Slot 5, +and print out their details. + +Hold "Boot" to exit the program (can take up to 5 seconds). +""" + +# Constants +SCAN_INTERVAL = 5.0 # The time to sleep between each network scan + +# Variables +yukon = Yukon() # Create a new Yukon object, with a lower voltage limit set +module = WirelessModule() # Create a WirelessModule object + + +# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt) +try: + yukon.register_with_slot(module, SLOT) # Register the WirelessModule object with the slot + yukon.verify_and_initialise() # Verify that a WirelessModule is attached to Yukon, and initialise it + + wlan = network.WLAN(network.STA_IF) # Create a new network object for interacting with WiFi + wlan.active(True) # Turn on WLAN communications + + while not yukon.is_boot_pressed(): + # Scan for nearby networks and print them out + networks = wlan.scan() # Returns a list of tuples with 6 fields: ssid, bssid, channel, RSSI, security, hidden + for i, w in enumerate(networks): + print(i, w[0].decode(), binascii.hexlify(w[1]).decode(), + w[2], w[3], w[4], w[5]) + print() + + # Monitor sensors for a number of seconds, recording the min, max, and average for each + yukon.monitored_sleep(SCAN_INTERVAL) + +finally: + # Put the board back into a safe state, regardless of how the program may have ended + yukon.reset() + + # Attempt to disconnect from WiFi + try: + wlan.disconnect() + except Exception: + pass diff --git a/examples/wireless/README.md b/examples/wireless/README.md deleted file mode 100644 index 251684e..0000000 --- a/examples/wireless/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# Yukon Micropython Wireless Examples - -This folder contains a collection of *Wireless* examples, that combine the RM2 Wireless Module with others to create functional projects. - -- [Cheerlights](#cheerlights) - - -## Cheerlights - -[cheerlights.py](cheerlights.py) - -Obtain the current CheerLights colour from the internet and show it on an LED Strip connected to Yukon. For more information about CheerLights, visit: https://cheerlights.com/ - -This example requires a secrets.py file to be on your board's file system with the credentials of your WiFi network. \ No newline at end of file