Initiate sleep from MicroPython

Note This section only applies to devices that support the Power Management feature.

XBee Cellular Modem:

If you disable sleep modes by setting SM (Sleep Mode) to 0, you can use XBee().sleep_now() and XBee().wake_reason() to control when the module sleeps. When selecting sleep and wake times on the XBee Cellular Modem, take into consideration the time it takes to close network connections and shut down the cellular connection before sleeping, and then to restore the connection when waking back up.

XBee 3 Zigbee RF Module, XBee 3 802.15.4 RF Module, XBee 3 DigiMesh RF Module:

When setting SM (Sleep Mode) to 6, you can use XBee().sleep_now() and XBee().wake_reason() to control when the device sleeps. The device sleeps for the time period programmed with an optional early pin wake (DTR, commissioning button, or SPI_SSEL).

sleep_now(timeout_ms, pin_wake=False)

Sleeps for timeout_ms milliseconds and then wakes.

Note The sleep time reported includes code execution overhead—several milliseconds.

wake_reason()

Returns either xbee.RTC_WAKE if the full timeout_ms elapsed, or xbee.PIN_WAKE when enabled and DIO8 woke the device early.

The following example shows power management with MicroPython:

from machine import Pin
import time
import xbee

def read_switch(iopin = None):
  if iopin.value() == 0:
    print("SW2 has been pressed!")
    return True
  return False


# Configure DIO0(SW2) to put module to sleep
dio0 = Pin('D0', Pin.IN, Pin.PULL_UP)

x = xbee.XBee()

print("\n")
print("How to use this example:")
# pressing SW2 triggers sleep for 30 seconds
print("Option 1 press SW2 and let the program run until it wakes from 30 seconds sleep.")
print("Option 2 press SW2 to put the module under sleep for 30 seconds, "
      "then while its sleeping toggle DTR by Close/Open MicroPython Terminal Com port.")
print("Option 3 press SW2 then do ^C (cancel) to exit example program while its sleeping")

print("Waiting for SW2 to be pressed to Sleep. Please Press SW2")

while True:
  sw2 = read_switch(dio0)
  if sw2:
    # sleep for 30 seconds, wake early DTR toggled active.
    print("sleeping for 30 seconds")
    sleep_ms = x.sleep_now(30000, True)
    print("slept for %u ms" % sleep_ms)
    if x.wake_reason() is xbee.PIN_WAKE:
      print("woke early on DTR toggle")