Get started with MicroPython
MicroPython examples
Digi XBee® 3 802.15.4 RF Module User Guide
40
5. If the code is correct, press
Ctrl
+
D
to run the code; “Hello World” should print.
Note
If you want to exit paste mode without running the code, or if the code did not copy
correctly, press
Ctrl
+
C
to cancel and return to the normal MicroPython
>>>
prompt).
Example: use the time module
The time module is used for time-sensitive operations such as introducing a delay in your routine or a
timer.
The following time functions are supported by the XBee 3 802.15.4 RF Module:
n
ticks_ms()
returns the current millisecond counter value. This counter rolls over at
0x40000000.
n
ticks_diff()
compares the difference between two timestamps in milliseconds.
n
sleep()
delays operation for a set number of seconds.
n
sleep_ms()
delays operation for a set number of milliseconds.
n
sleep_us()
delays operation for a set number of microseconds.
Note
The standard
time.time()
function cannot be used, because this function produces the number
of seconds since the epoch. The XBee3 module lacks a realtime clock and cannot provide any date or
time data.
The following example exercises the various sleep functions and uses
ticks_diff()
to measure
duration:
import time
start = time.ticks_ms()
# Get the value from the millisecond counter
time.sleep(1)
# sleep for 1 second
time.sleep_ms(500)
# sleep for 500 milliseconds
time.sleep_us(1000)
# sleep for 1000 microseconds
delta = time.ticks_diff(time.ticks_ms(), start)
print("Operation took {} ms to execute".format(delta))
Example: AT commands using MicroPython
AT commands control the XBee 3 802.15.4 RF Module. The "AT" is an abbreviation for "attention", and
the prefix "AT" notifies the module about the start of a command line. For a list of AT commands that
can be used on the XBee 3 802.15.4 RF Module, see
.
MicroPython provides an
atcmd()
method to process AT commands, similar to how you can use
or API frames.
The
atcmd()
method accepts two parameters:
1. The two character AT command, entered as a string.
2. An optional second parameter used to set the AT command value. If this parameter is not
provided, the AT command is queried instead of being set. This value is an integer, bytes object,
or string, depending on the AT command.