Adafruit Playground is a wonderful place to share what you find interesting. Have a cool project you are working on? Have a bit of code that you think others will find useful? Want to show off your electronics workbench? You have come to the right place.
Adafruit Playground is a safe place to share with the wonderful Adafruit community of makers and doers.
-
Using Multiple WiFi Access Points with CircuitPython
Introduction:
If you have the need your WiFi project to operate at various locations with different WiFi SSID/PASSWORD settings at each location, read on. If you are using an MCU with built-in WiFi that CircuitPython 9.0.0 or later supports, there may be a solution to your issue.
Overview of the project:
This article will provide you with two tools to get you started.
- A code.py defined function (def) that will cycle through the WiFi networks defined in settings.toml. It will also show a sorted list of available WiFi access points found locally.
- A sample settings.toml to get you started.
- There are additional functions and features that will be covered as we go along.
- There will be a description of how each function works and interacts.
Why would you need multiple WiFi SSIDs in IoT projects?:
Let’s say you have a project that you must develop at home, show friends how it works at your bridge club, test it under various situations, and demonstrate its features to a customer. Having all the SSIDs and PASSWORDs predefined and having your project cycle through them without your intervention, except the first time you add them, would speed things up.
Prerequisites:
The system requirements are simple. An MCU with built-in WiFi that is supported by Circuit Python 9.0.0 or newer. My tests were run on a Raspberry Pi Pico W. All the libraries are built into CP 9.x.x that the sample code.py needs. They are: import os, wifi, random, binascii. The additional libraries used by the diagnostic code are import time, board, digitalio, ipaddress, supervisor, microcontroller and are also built-in.
If your MCU is listed at the web site below, your board is probably supported.
https://docs.circuitpython.org/en/latest/shared-bindings/wifi/index.html
Click on Available on these boards for a full listing. Considering the length of the boards listed, I have not tested, nor can I guarantee this code will work with any or all of them.
-
WiFi Power Management for the Raspberry Pi Pico W
With CircuitPython 8.x.x the new cyw43 library was introduced. The CYW43 supports controlling the power and efficiency of the CYW43439A0 WiFi chip on the Raspberry Pi Pico W microcontroller and other microcontrollers. The pins controlling 3 minor functions were transferred to the CYW43439A0 because Raspberry Pi Pico had used all the RP2040 pins. So the CYW43439A0 WiFi module had to be shoehorned between the RP2040 and the green LED, SMPS_MODE, and VBUS_SENSE functions. These three functions and the CYW43439A0 will be discussed here.
There are four sections to the new cyw43 internal library. This is new to CircuitPython starting with version 8.0.0. The features that import cyw43 will give you access to are:
- CYW43439A WiFi power management
- Access to and control of the onboard Green LED
- Access to and control of the SMPS_MODE option (PFM, PWM)
- Access to whether power is being supplied by way of the USB port by reading VBUS_SENSE
Four WiFi power management modes are predefined with the following:
- PM_STANDARD, 0xa11142: Is the standard power management mode. It enables power management and sets the power conservation timer to 200ms, 0x14.
- PM_AGGRESSIVE, 0xa11c82: It enables power management and sets the power conservation timer to 2000ms, 0xc8. This mode provides optimal power usage at the cost of performance.
- PM_PERFORMANCE, 0x111022: It enables power management and sets the power conservation timer to 20ms, 0x02. This mode uses more power to increase performance.
- PM_DISABLED, 0xa11140: Power management is disabled in this mode. The power management timer is set to 200ms, 0x14. Note: CircuitPython sets this mode at power on and reset because it provides the best connectivity reliability.
-
Alternate Power Settings: There does not seem to be a standard name for what each means. These are from the MicroPython documentation.
PM_NONE = 0x000010 # Like PM_DISABLED above
PM_POWERSAVE = 0x000011 # More power savings than PM_PERFORMANCE above
PM_PERFORMANCE = 0xA11142 # Same as PM_STANDARD above
To see what each bit of the 12 bit word does, check the first document referenced at the end of this document.
The following code snippet shows how to set the mode to the STANDARD mode.
try:
import cyw43 # Also tests for Raspberry Pi Pico W
cyw43.set_power_management(cyw43.PM_STANDARD)
except ImportError:
cyw43 = None
print(hex(cyw43.get_power_management())) # Will show the HEX value.
The challenge for the designers of the Raspberry Pi Pico W to add WiFi services they needed to repurpose three feature pins. The green LED, SMPS_MODE, and VBUS_SENSE control lines are used. To access them on the Pico W, the following code snippets may help you.
To drive the green LED we must now go through the WiFi module. Fortunately, board.LED does that for us.
# Green LED, WL_GPIO0, CYW0
GreenLED = digitalio.DigitalInOut(board.LED)
GreenLED.direction = digitalio.Direction.OUTPUT
GreenLED.value = True # Turn green LED ON
GreenLED.value = False # Turn green LED OFF
print('Green LED ', GreenLED.value) # Test for green LED state
SMPS_MODE sets the way the power management chip converts the supplied power to the 3.3 volts the RP2040 MCU and other devices require. SMPS_MODE controls the PS (Power Save) pin on the RT6150 buck/boost power manager.
When PS is low (0) (the default on Pico) the regulator is in Pulse Frequency Modulation (PFM) mode, which, at light and moderate loads, saves considerable power by only turning on the switching MOSFETs occasionally to keep the output capacitor topped up. PFM outputs a constant wave shape but at different frequencies depending on the load. Higher load >> higher frequency. An inductor is used to store power between the HIGHs to smooth out the ripple introduced by turning the power converter MOSFETs ON and OFF.
Setting PS high (1) forces the regulator into Pulse Width Modulation (PWM) mode. PWM mode forces the SMPS to switch continuously, which reduces the output ripple considerably at light loads (which can be good for some use cases) but at the expense of much worse efficiency. PWM outputs a constant frequency with a varying portion of HIGH versus LOW states. With a higher current demand, the output will increase the duration of the HIGH state and reduce the duration of the LOW state. An inductor is used to store power between HIGH states to smooth out the 3.3 volts sent to the circuits. Improved ripple is achieved, but worse efficiency at light loads will occur. It may improve Analog to Digital Conversion and other stability critical operations.
Note: Under heavy load conditions the switcher will be in PWM mode irrespective of the PS pin state.
# SMPS_MODE, WL_GPIO1, CYW1, 0=PFM Mode (Default, Best Efficiency),
# 1=PWM Mode (Improved ripple, worse efficiency at light loads)
SMPS = digitalio.DigitalInOut(board.SMPS_MODE)
SMPS.direction = digitalio.Direction.OUTPUT
SMPS.value = False # PFM Mode, most efficient
print('SMPS_MODE ', SMPS.value)
By reading the value from VBUS_SENSE you can determine if power is being supplied through the USB connector. If the value is True (1) power is being provided from the USB connector. If it is False (0) power is coming from some other source. It may be ~2.0 volts to 5.5 volts applied to the VSYS pin or 3.3 volts applied to the 3V3 pin. Applying 3.3 volts to the 3V3 pin is not advised unless it is exceptionally well regulated. Momentary startup overvoltage spikes can be lethal to your MCU. If power is applied to VSYS an onboard diode connected between VBUS and VSYS prevents back feeding power to the PC USB port.
# VBUS_SENSE, WL_GPIO2, CYW2
# True = we have power from USB
# False = power via VSYS or 3V3
VBUS = digitalio.DigitalInOut(board.VBUS_SENSE)
VBUS.direction = digitalio.Direction.INPUT
print('VBUS_SENSE', VBUS.value)
Source documents:
https://docs.circuitpython.org/en/latest/shared-bindings/cyw43/index.html
https://datasheets.raspberrypi.com/picow/pico-w-product-brief.pdf