Skip to content

Commit

Permalink
Added basic wireless module examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ZodiusInfuser committed Dec 16, 2024
1 parent 5d976a7 commit 816228e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 16 deletions.
33 changes: 33 additions & 0 deletions examples/modules/rm2_wireless/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# RM2 Wireless Module - Micropython Examples <!-- omit in toc -->

<img src="https://shop.pimoroni.com/cdn/shop/files/wireless-1_1500x1500_crop_center.jpg" width="500">

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.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
import network
import requests
from pimoroni_yukon import Yukon
Expand Down Expand Up @@ -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]}')
Expand Down
25 changes: 25 additions & 0 deletions examples/modules/rm2_wireless/detect_module.py
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions examples/modules/rm2_wireless/wifi_scan.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions examples/wireless/README.md

This file was deleted.

0 comments on commit 816228e

Please sign in to comment.