Pico-W Deep Sleep with Micropython
The last post of this series discussed how a simple mod for Micropython could be developed to support deep sleeping. This post will briefly discuss how this can also now be used with the Pico-W with a short code example.
Note
Deep sleeping and waking from the onboard clock has been merged into Micropython directly with this PR.
There is no need now to use this fork.
TLDR
Skip to release and example here
Raspberry Pi Pico-W
The Raspberry Pi Pico-W was released on the 30th of June 2022. This new version of the Pico is pretty much identical to the first version, but now includes a 2.4GHz 802.11n wireless LAN chip, supporting both WiFi and bluetooth capabilities.
The commit required to modify the master branch of Micropython to add in the sleep modification can be seen here. It serves as a good example of how Micropython can be customised, allowing you to add your own modules and commands. This might be needed to improve performance of certain sections of critical code or to expose parts of the hardware not yet provided for by Micropython.
Example code
Once this modification is compiled and loaded onto the Pico-W it’s then possible to use the following example code:
import network
import time
import machine
import picosleep
ssid = 'ssid_to_connect_to'
password = 'password'
def wait_for_wifi():
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
return wlan.status()
else:
status = wlan.ifconfig()
# Uncomment below to print assigned network ip
# Printing will cause the pico to hang though
# when using picosleep
#print('ip = ' + status[0])
return wlan.status()
# Inital setup and connection
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while (wait_for_wifi() != 3):
time.sleep(1)
wlan.connect(ssid, password)
led = machine.Pin("LED", machine.Pin.OUT)
led.on()
while True:
wlan.disconnect()
wlan.active(False)
time.sleep(5)
led.off()
# deepsleep
picosleep.seconds(5)
led.on()
wlan.active(True)
wlan.connect(ssid, password)
while (wait_for_wifi() != 3):
time.sleep(1)
wlan.connect(ssid, password)
# Perform work here
# Before sleeping again
time.sleep(5)
This code was adapted from here
This code allows a Pico-W to connecto to a WiFi network and periodically wake up from deep sleep to perform some work. If you uncomment this line above:
#print('ip = ' + status[0])
You should see the ip address that your Pico has been assigned. Printing anything to console however appears to break deep sleep, so make sure nothing is printed out when running this standalone on the Pico by saving it as main.py
so it runs at startup.
If you grab the ip address and ping it, you should see the Pico respond and then peridically stop responding when in deep sleep. Remove the lines switching the led on to save even more power when not sleeping.
Rather than using:
picosleep.seconds(5)
The following can be used without this deep sleep mod using the default relese of Micropython:
machine.lightsleep(5000)
Code and release download location
Code can be found here.
Pre-compiled uf2 file can be found here
Usage:
import picosleep
picosleep.seconds(60)