Skip to content

Commit 89c0723

Browse files
Zack Yanceymikewadsten
authored andcommitted
Add samples and type hints for the idle_radio() feature
1 parent 14bd8e1 commit 89c0723

File tree

5 files changed

+404
-0
lines changed

5 files changed

+404
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
Idle Radio Polling Sample Application
2+
======================================
3+
4+
The `idle_radio()` function allows the MicroPython application to put the radio
5+
into an idle state, reducing current draw but preventing the radio from
6+
receiving transmissions (see the *Idle Radio* sample application).
7+
8+
Zigbee networks allow a little bit more flexibility in this idle state. This
9+
state is actually the default state for end devices--whenever a message is sent
10+
to an end device it is held by a router until the end device enables the radio
11+
and polls for new messages. Normally this process happens automatically, but the
12+
`idle_radio()` function disables this polling, transferring the responsibility
13+
to do so to the application (using the `poll_now()` function).
14+
15+
In this example an end device sleeps, waking every 5 seconds to take a sample
16+
from a sensor. Every 4 times the device wakes, it sends a message to the
17+
coordinator with the last 4 samples. When the device sends the samples it also
18+
polls for messages, allowing it to receive any messages that were sent while it
19+
was sleeping/idling.
20+
21+
22+
Requirements
23+
------------
24+
25+
To run this example you need:
26+
27+
* Two XBee 3 Zigbee modules with MicroPython support.
28+
* Carrier boards for each radio module (XBIB-U-DEV or XBIB-C board).
29+
30+
31+
Setup
32+
-----
33+
34+
Set up a network with two modules:
35+
36+
* One module is the *coordinator*, which will receive the sensor readings.
37+
Configure the following AT commands on the coordinator:
38+
* Set **AP** to 1 to enable API mode, making it easier to see transmissions
39+
from the client using XCTU.
40+
* Set **CE** to 1 to make the device a coordinator.
41+
* Set **SP** to 0x7D0 (20 seconds). This indicates how long end devices will
42+
go between polls, and is used to determine the timeout for transmissions.
43+
44+
* The other module is the *end device* that will run the sample
45+
application. Configure the following AT commands on the end device:
46+
* Set **SM** to 6 on the end device to allow MicroPython to control sleep.
47+
48+
Allow the end device to join the network (wait until the associate LED on the
49+
end device is blinking and querying the **AI** command returns 0).
50+
51+
52+
Run
53+
---
54+
55+
Open the coordinator in XCTU's API terminal so you can observe the frames output
56+
by the module, then compile and launch the application on the sensor module.
57+
58+
Observe the output from MicroPython on the sensor module. The module will wake
59+
every 5 seconds, take a reading, and go back to sleep. Every 4th time the module
60+
wakes, it will send the last 4 samples to the coordinator. After 12 samples, the
61+
example will finish.
62+
63+
The coordinator (or any other device on the network) can send a transmission to
64+
the end device at any time, and it will be received the next time the end device
65+
polls for data. It is recommended to send transmissions to the end device with
66+
transmit option bit six (0x40) set, to enable the extended timeout.
67+
68+
Supported platforms
69+
-------------------
70+
71+
* Digi XBee3 Zigbee 3 - minimum firmware version: 100B
72+
73+
License
74+
-------
75+
76+
Copyright (c) 2020, Digi International, Inc.
77+
78+
Permission is hereby granted, free of charge, to any person obtaining a copy
79+
of this software and associated documentation files (the "Software"), to deal
80+
in the Software without restriction, including without limitation the rights
81+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
82+
copies of the Software, and to permit persons to whom the Software is
83+
furnished to do so, subject to the following conditions:
84+
85+
The above copyright notice and this permission notice shall be included in all
86+
copies or substantial portions of the Software.
87+
88+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
89+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
90+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
91+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
92+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
93+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
94+
SOFTWARE.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Copyright (c) 2020, Digi International, Inc.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
# SOFTWARE.
20+
import time
21+
import xbee
22+
23+
24+
def read_sensor():
25+
"""This example uses ATTP to read the internal temperature sensor."""
26+
return xbee.atcmd("TP")
27+
28+
29+
def handle_rx_packet(rx):
30+
"""Print out any received packets."""
31+
print("Received:", rx["payload"])
32+
33+
34+
def main():
35+
module = xbee.XBee()
36+
37+
# Wait until the module has joined a network before starting
38+
ai = xbee.atcmd("AI")
39+
while ai != 0:
40+
print("Waiting to join a network, AI=%x" % ai)
41+
time.sleep(1)
42+
ai = xbee.atcmd("AI")
43+
44+
# Put the radio to sleep. Note that by doing so the MicroPython
45+
# application assumes the responsibility of fequently calling poll_now()
46+
# or data sent to this device may be lost.
47+
xbee.idle_radio(True)
48+
49+
# register handle_rx_packet to be called whenever a packet is received.
50+
xbee.receive_callback(handle_rx_packet)
51+
52+
samples = []
53+
# Since this application spends most of its time asleep, stopping
54+
# the application through the UART can be difficult. To make the
55+
# example easier to work with, it will only run for about a minute
56+
# instead of indefinitely.
57+
for _ in range(12):
58+
# Sleep for 5 seconds
59+
module.sleep_now(5000)
60+
61+
# Upon waking, take a sample
62+
sample = read_sensor()
63+
samples.append(sample)
64+
print(" Sample: {}".format(sample))
65+
66+
if len(samples) == 4:
67+
# Once we have four samples, Send the samples to
68+
# the coordinator. Note that we don't have to call
69+
# xbee.idle_radio(True) to be able to do this--the radio is
70+
# enabled just long enough to send the transmission.
71+
print("Transmit samples: {}".format(samples))
72+
xbee.transmit(xbee.ADDR_COORDINATOR, str(samples))
73+
74+
# Clear the stored samples
75+
samples.clear()
76+
77+
# We need to call poll_now() periodically to check for incoming
78+
# messages, so do that now.
79+
xbee.poll_now()
80+
# handle_rx_packet() is registered as the receive callback, so
81+
# it will be called automatically if the poll comes back with
82+
# any data.
83+
84+
print("Example complete, re-enabling radio")
85+
xbee.idle_radio(False)
86+
87+
88+
main()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Idle Radio Sample Application
2+
======================================
3+
4+
By default, whenever an XBee module is awake, the radio is active and available
5+
to receive RF transmissions. If the application does not always need to receive
6+
transmissions while it is awake (for example, if a MicroPython application is
7+
waking only to sample some data and go back to sleep), the radio can be disabled
8+
to save power.
9+
10+
In this example an end device sleeps, waking every 5 seconds to take a sample
11+
from a sensor. Every 4 times the device wakes, it sends a message to an
12+
aggregator with the last 4 samples. When the device isn't actively transmitting
13+
or receiving, the radio is kept in the idle state to minimize current draw.
14+
15+
16+
**Note** if using XBee 3 Zigbee: This sample application will run, but on a
17+
Zigbee network additional functionality is available to further reduce power
18+
usage. See the *Idle Radio Polling* sample application for more information.
19+
20+
21+
Requirements
22+
------------
23+
24+
To run this example you need:
25+
26+
* Two XBee 3 modules with MicroPython support.
27+
* Carrier boards for each radio module (XBIB-U-DEV or XBIB-C board).
28+
29+
30+
Setup
31+
-----
32+
33+
Set up a network with two modules:
34+
35+
* One module is the *aggregator*, which will receive the sensor readings.
36+
37+
* The other module is the *sensor module* that will run the sample
38+
application. Configure the following AT commands:
39+
* **SM** 6
40+
* **DH**/**DL**: Set to the 64-bit address (**SH**/**SL**) of the aggregator.
41+
42+
43+
Run
44+
---
45+
46+
Open the aggregator in XCTU's API terminal so you can observe the frames output
47+
by the module, then compile and launch the application on the sensor module.
48+
49+
Observe the output from MicroPython on the sensor module. The module will wake
50+
every 5 seconds, take a reading, and go back to sleep. Every 4th time the module
51+
wakes, it will send the last 4 samples to the aggregator. After 12 samples, the
52+
example will finish.
53+
54+
To send a message to the sensor module, the aggregator will need to wait until
55+
it receives a packet from the sensor module, then send its message during the 1
56+
second that the sensor module application activates the radio after sending its
57+
samples.
58+
59+
Supported platforms
60+
-------------------
61+
62+
* Digi XBee3 Zigbee 3 - minimum firmware version: 100B
63+
* Digi XBee3 DigiMesh 2.4 - minimum firmware version: 300B
64+
65+
License
66+
-------
67+
68+
Copyright (c) 2020, Digi International, Inc.
69+
70+
Permission is hereby granted, free of charge, to any person obtaining a copy
71+
of this software and associated documentation files (the "Software"), to deal
72+
in the Software without restriction, including without limitation the rights
73+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
74+
copies of the Software, and to permit persons to whom the Software is
75+
furnished to do so, subject to the following conditions:
76+
77+
The above copyright notice and this permission notice shall be included in all
78+
copies or substantial portions of the Software.
79+
80+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
81+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
82+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
83+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
84+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
85+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
86+
SOFTWARE.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright (c) 2020, Digi International, Inc.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
# SOFTWARE.
20+
import time
21+
import xbee
22+
23+
24+
def read_sensor():
25+
"""This example uses ATTP to read the internal temperature sensor."""
26+
return xbee.atcmd("TP")
27+
28+
29+
def handle_rx_packet(rx):
30+
"""Print out any received packets."""
31+
print("Received:", rx["payload"])
32+
33+
34+
def main():
35+
module = xbee.XBee()
36+
37+
# Idle the radio. While the radio is idled, the radio cannot receive
38+
# transmissions and current draw is reduced.
39+
xbee.idle_radio(True)
40+
41+
# register handle_rx_packet to be called whenever a packet is received.
42+
xbee.receive_callback(handle_rx_packet)
43+
44+
# Send transmissions to the address configured by DH/DL
45+
dest_addr = xbee.atcmd("DH") + xbee.atcmd("DL")
46+
47+
samples = []
48+
# Since this application spends most of its time asleep, stopping
49+
# the application through the UART can be difficult. To make the
50+
# example easier to work with, it will only run for about a minute
51+
# instead of indefinitely.
52+
for _ in range(12):
53+
# Sleep for 5 seconds
54+
module.sleep_now(5000)
55+
56+
# Upon waking, take a sample
57+
sample = read_sensor()
58+
samples.append(sample)
59+
print(" Sample: {}".format(sample))
60+
61+
if len(samples) == 4:
62+
# Once we have four samples, Send the samples to
63+
# the coordinator. Note that we don't have to call
64+
# xbee.idle_radio(True) to be able to do this--the radio is
65+
# enabled just long enough to send the transmission.
66+
print("Transmit samples: {}".format(samples))
67+
xbee.transmit(dest_addr, str(samples))
68+
69+
# Clear the stored samples
70+
samples.clear()
71+
72+
# If the server is ever going to send transmissions to the
73+
# end device, the end device needs to enable the radio at
74+
# some point, and the server needs to know when that is. For
75+
# this example, that is achieved by leaving the radio on for
76+
# 1 second after transmitting a sample. If this end device
77+
# never needs to receive transmissions, this can be left
78+
# out.
79+
print("Enable the radio for a bit to receive transmissions...")
80+
xbee.idle_radio(False)
81+
# Wait 1 second with the radio enabled. This is the only
82+
# time the radio can receive transmissions.
83+
time.sleep(1)
84+
print("Disable the radio again")
85+
xbee.idle_radio(True)
86+
87+
print("Example complete, re-enabling radio")
88+
xbee.idle_radio(False)
89+
90+
91+
main()

0 commit comments

Comments
 (0)