Skip to content

Commit 17e6b47

Browse files
Zack Yanceymikewadsten
authored andcommitted
Add a demo using modem status callbacks in a sync sleep network
1 parent eebf8f6 commit 17e6b47

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
Synchronous Sleep Sample Application
2+
======================================
3+
4+
In a synchronously sleeping DigiMesh network, the MicroPython
5+
application doesn't control when the module sleeps. If an application
6+
needs to be aware of when the network sleeps and wakes, it can do so by
7+
registering a modem status callback and watching for the relevant modem
8+
statuses.
9+
10+
This sample application demonstrates a module on a synchronously
11+
sleeping network configured to read a sample from an external sensor and
12+
send it to an aggregator each time the network wakes.
13+
14+
Requirements
15+
------------
16+
17+
To run this example you need:
18+
19+
* Two XBee 3 DigiMesh modules with MicroPython support.
20+
* Carrier boards for each radio module (XBIB-U-DEV or XBIB-C board).
21+
* One standalone HDC1080 humidity and temperature sensor (not necessary
22+
if you are using an XBIB-C carrier board).
23+
24+
Setup
25+
-----
26+
27+
Set up a synchronously sleeping network with two DigiMesh modules:
28+
29+
* One module is the *aggregator*, and should be configured with the
30+
following AT commands:
31+
* **AP** 1
32+
* **SM** 7
33+
* **SO** 1
34+
35+
* The other module is the *sensor module* that will run the sample
36+
application. Make sure the sensor module is on the same network as the
37+
aggregator (ie ID and CH match on both radios if they have been
38+
changed from the default), but do not configure sleep settings as that
39+
will be done by the application.
40+
41+
42+
Make sure the hardware is set up correctly on the sensor module:
43+
44+
1. Plug the XBee3 radio module into the XBee adapter and connect it to your
45+
computer's USB port.
46+
2. Connect the HDC1080 device to the I2C interface of the XBee module. The way
47+
to connect the sensor changes depending on the carrier board you have:
48+
49+
* XBIB-U-DEV board:
50+
51+
* Isolate the pins configured as SDA and SCL so they do not use the
52+
functionality provided by the board.
53+
* Connect the HDC1080 device to VCC, to the pins configured as SDA and SCL
54+
and to GND. See the following table for more information about the pins
55+
layout:
56+
57+
+--------+------------+----------+-----------+-----------+
58+
| Signal | Pin ID | Pin # TH | Pin # SMT | Pin # MMT |
59+
+--------+------------+----------+-----------+-----------+
60+
| SDA | PWM1/DIO11 | 7 | 8 | 8 |
61+
+--------+------------+----------+-----------+-----------+
62+
| SCL | AD1/DIO1 | 19 | 32 | 30 |
63+
+--------+------------+----------+-----------+-----------+
64+
65+
* XBIB-C board:
66+
67+
* XBIB-C boards already come with an HDC1080 I2C sensor connected to the
68+
I2C interface of the XBee module, so you don't need to connect anything.
69+
70+
**NOTE**: It is recommended to verify the capabilities of the pins used in
71+
the example as well as the electrical characteristics in the product manual
72+
of your XBee Device to ensure that everything is configured correctly.
73+
74+
Run
75+
---
76+
77+
Open the aggregator in XCTU's API terminal so you can observe the frames
78+
output by the module, then compile and launch the application on the
79+
sensor module.
80+
81+
The sensor module will begin to cyclically sleep: two seconds on, two
82+
seconds off. Each time the sensor module wakes, it will send a packet to
83+
the aggregator with the temperature measurement. Observe the
84+
transmissions received in XCTU.
85+
86+
Required libraries
87+
--------------------
88+
89+
* hdc1080
90+
91+
Supported platforms
92+
-------------------
93+
94+
* Digi XBee3 DigiMesh 2.4 - minimum firmware version: 300B
95+
96+
License
97+
-------
98+
99+
Copyright (c) 2020, Digi International, Inc.
100+
101+
Permission is hereby granted, free of charge, to any person obtaining a copy
102+
of this software and associated documentation files (the "Software"), to deal
103+
in the Software without restriction, including without limitation the rights
104+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
105+
copies of the Software, and to permit persons to whom the Software is
106+
furnished to do so, subject to the following conditions:
107+
108+
The above copyright notice and this permission notice shall be included in all
109+
copies or substantial portions of the Software.
110+
111+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
112+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
113+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
114+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
115+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
116+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
117+
SOFTWARE.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
21+
import time
22+
import xbee
23+
from machine import I2C
24+
from hdc1080 import HDC1080
25+
26+
MODEM_STATUS_NETWORK_WOKE = 0x0B
27+
28+
# Initialize the HDC1080 sensor.
29+
sensor = HDC1080(I2C(1))
30+
31+
32+
def handle_modem_status(status):
33+
if status == MODEM_STATUS_NETWORK_WOKE:
34+
temp_celsius = sensor.read_temperature(True)
35+
aggregator_addr = xbee.atcmd("DH") + xbee.atcmd("DL")
36+
xbee.transmit(aggregator_addr, "{}".format(temp_celsius))
37+
38+
39+
# Register the above function as the modem status callback so that it
40+
# will be called whenever a modem status is generated.
41+
xbee.modem_status.callback(handle_modem_status)
42+
43+
# Enable sync sleep modem status messages
44+
xbee.atcmd("SO", 4)
45+
46+
# Enable sync sleep
47+
xbee.atcmd("SM", 8)
48+
49+
while True:
50+
# The callback will fire even if the app is in another blocking call,
51+
# such as a long time.sleep call.
52+
time.sleep(120)
53+
54+
# Note that even after this code completes the modem status callback
55+
# will still be registered and will continue to execute until
56+
# MicroPython is rebooted or xbee.modem_status.callback(None) is called.

typehints/common/xbee/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from typing import Any, ContextManager, Iterator, Optional
2222

23+
from . import modem_status
2324

2425
ADDR_BROADCAST: bytes = ...
2526
ADDR_COORDINATOR: bytes = ...
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
21+
from typing import Any, Optional, Callable
22+
23+
def callback(callback: Optional[Callable[[int], None]], /) -> None:
24+
"""
25+
Register a callback method that is called whenever a modem status is
26+
generated by the XBee.
27+
28+
This callback takes one parameter, the integer value of the modem status.
29+
30+
**Note: This is only available on XBee 3 RF products with version
31+
ending in 0B or newer.**
32+
33+
:param callback: A function that is called whenever a modem status is
34+
received.
35+
If ``callback`` is None, the registered callback will be
36+
unregistered, allowing for polling of modem_status.receive() again.
37+
"""
38+
...
39+
40+
def receive() -> Optional[int]:
41+
"""
42+
Returns a modem status if one is available. This function can only
43+
be called if no callback is registered, otherwise it will raise an
44+
exception.
45+
46+
:return: The integer value of a modem status, if available.
47+
"""
48+
...

0 commit comments

Comments
 (0)