MicroPython Sleep is allows you to control when an XBee device enters and exits sleep mode through a MicroPython script. Unlike other predefined sleep modes, this mode gives you more dynamic control, allowing for customized power management tailored to the specific needs of your application.
Enable MicroPython sleep mode
To enable MicroPython sleep mode on the XBee 3 BLU device, configure the SM
(Sleep Mode) setting with 6
.
Enter/Exit sleep mode
MicroPython sleep allows your script to directly control the sleep behavior of the XBee module.
Using the XBee().sleep_now()
and XBee().wake_reason()
functions, the MicroPython script dictates when the module sleeps, for how long, and under what conditions it wakes up.
For more information about managing sleep mode in MicroPython, refer to the Digi MicroPython Programming Guide. |
sleep_now()
The XBee().sleep_now()
function puts the XBee module to sleep for a specified duration, after which it automatically wakes up. You can also configure it to wake up early if a specific event (such as a pin change) occurs.
Function parameters:
-
timeout_ms
: Specifies the sleep duration in milliseconds.-
If set to
None
, the device will sleep indefinitely, waking only on an external event. -
If
timeout_ms
is specified, the function will return the actual sleep time in milliseconds once the device wakes.
-
-
pin_wake
: If set toTrue
, the device can wake up early if the pinDTR/SLEEP_RQ
, which acts as a sleep/wake trigger, detects a falling edge (high to low transition).
wake_reason()
After the XBee module wakes from sleep, the XBee().wake_reason()
function can be used to determine whether it woke due to a timeout or an external pin event.
It returns one of the following values:
-
xbee.RTC_WAKE
: Indicates the device woke after the fulltimeout_ms
elapsed. -
xbee.PIN_WAKE
: Indicates the device woke early due to a falling edge onD0
pin.
For more information about MicroPython sleep on the XBee 3 BLU device, refer to the Digi MicroPython Programming Guide. |
MicroPython sleep example
The following code demonstrates how to use MicroPython sleep on an XBee module.
It sleeps for 30 seconds every 10 seconds, with the option to wake early if the DTR/SLEEP_RQ
pin is toggled.
import time
import xbee
x = xbee.XBee()
while True:
# Sleep for 30 seconds, allow early wake with DTR/SLEEP_RQ toggle.
print("- Sleeping for 30 seconds...")
sleep_ms = x.sleep_now(30000, True)
# XBee has woken up.
print("- Slept for %u ms." % sleep_ms)
if x.wake_reason() is xbee.PIN_WAKE:
print(" - Woke early due to DTR toggle.")
# Wait 10 seconds.
print("- Waiting 10 seconds...")
time.sleep(10)
Prevent sleep from MicroPython
When an XBee device enters sleep mode, any MicroPython code that is currently executing will be suspended until the device wakes up. Once the device wakes, MicroPython resumes execution from where it was paused.
In certain scenarios, such as during critical operations (e.g., performing a long computation, processing important data, or transmitting essential information), you may want to prevent the device from entering sleep mode.
This is especially relevant when using one of the standard ATSM sleep modes (except for SM
= 6
, which is MicroPython-controlled sleep).
To prevent the device from sleeping during critical tasks, you can use the XBee().wake_lock
mechanism in MicroPython.
This ensures that the device stays awake as long as necessary, even when it would normally enter sleep mode.
For more information about preventing sleep in MicroPython, refer to the Digi MicroPython Programming Guide. |
MicroPython sleep prevent example
Here’s an example of how to use xbee().wake_lock
:
import xbee
import time
x = xbee.XBee()
with x.wake_lock:
# Perform critical operation, such as data processing or transmission.
# Simulate a task that takes 10 seconds.
print("- Device will stay awake while the wake lock is active.")
time.sleep(10)
# Back to things that are safe to interrupt
print("- Wake lock released. Device can enter sleep mode now.")