Skip to content

Commit 815c3ce

Browse files
committed
samples: add new 'End to End' sample
Signed-off-by: Ruben Moral <ruben.moral@digi.com>
1 parent 89c0723 commit 815c3ce

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

samples/demos/end_to_end/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
End to End Sample Application
2+
=============================
3+
4+
This example demonstrates how to use the XBee Gateway to transmit data from
5+
XBee nodes of the network to Digi Remote Manager and vice versa.
6+
7+
The example reads every 10 seconds the temperature and humidity from the board's
8+
I2C sensor, sends the readings to the gateway and goes to sleep until the
9+
next cycle. It also processes incoming data messages from the gateway to
10+
change the sampling rate or disable the temperature/humidity service.
11+
12+
Requirements
13+
------------
14+
15+
To run this example you need:
16+
17+
* One XBee 3 radio module with MicroPython support.
18+
* One carrier board for the radio module (XBIB-C board).
19+
* An XBee Gateway device.
20+
* A Digi Remote Manager account with your XBee Gateway added to it.
21+
Go to https://myaccount.digi.com/ to create it if you do not have one.
22+
* Another instance of PyCharm with the 'End to End' sample for Python loaded
23+
(located in the *DEMOS* category).
24+
25+
Setup
26+
-----
27+
28+
Make sure the hardware is set up correctly:
29+
30+
1. Plug the XBee 3 radio module into the XBee adapter and connect it to your
31+
computer's USB port.
32+
33+
Run
34+
---
35+
36+
The example is already configured, so all you need to do is build and launch
37+
the project. Then, launch the **End to End** sample for Python in the XBee
38+
Gateway and follow the instructions explained in that sample's README file.
39+
40+
Required libraries
41+
--------------------
42+
43+
* hdc1080
44+
45+
Supported platforms
46+
-------------------
47+
48+
* Digi XBee3 Zigbee 3 - minimum firmware version: 1006
49+
* Digi XBee3 802.15.4 - minimum firmware version: 2003
50+
* Digi XBee3 DigiMesh 2.4 - minimum firmware version: 3002
51+
52+
License
53+
-------
54+
55+
Copyright (c) 2020, Digi International, Inc.
56+
57+
Permission is hereby granted, free of charge, to any person obtaining a copy
58+
of this software and associated documentation files (the "Software"), to deal
59+
in the Software without restriction, including without limitation the rights
60+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
61+
copies of the Software, and to permit persons to whom the Software is
62+
furnished to do so, subject to the following conditions:
63+
64+
The above copyright notice and this permission notice shall be included in all
65+
copies or substantial portions of the Software.
66+
67+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
68+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
69+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
70+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
71+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
72+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
73+
SOFTWARE.

samples/demos/end_to_end/main.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 hdc1080 import HDC1080
22+
from machine import I2C
23+
import time
24+
import xbee
25+
26+
# Constants
27+
SLEEP_TIME_MS = 8000 # 8 seconds.
28+
AWAKE_TIME_MS = 2000 # 2 seconds.
29+
30+
MSG_ON = "ON"
31+
MSG_OFF = "OFF"
32+
MSG_SEPARATOR = "@@@"
33+
MSG_AWAKE = "AWAKE"
34+
35+
print(" +------------------------------------+")
36+
print(" | XBee MicroPython End to End Sample |")
37+
print(" +------------------------------------+\n")
38+
39+
# Instantiate the HDC1080 peripheral.
40+
sensor = HDC1080(I2C(1))
41+
42+
# Instantiate the XBee device.
43+
xb = xbee.XBee()
44+
45+
# Configure sleep mode to be managed by MicroPython.
46+
xb.atcmd("SM", 0x06)
47+
48+
sleep_time = SLEEP_TIME_MS
49+
50+
# Start reading temperature and humidity measures.
51+
while True:
52+
# Notify the gateway that the XBee is awake.
53+
xbee.transmit(xbee.ADDR_COORDINATOR, MSG_AWAKE)
54+
# Wait during the configured time for incoming messages.
55+
start_time_ms = time.ticks_ms()
56+
while time.ticks_diff(time.ticks_ms(), start_time_ms) < AWAKE_TIME_MS:
57+
incoming = xbee.receive()
58+
if incoming is not None and incoming["sender_nwk"] == 0 and not incoming["broadcast"]:
59+
try:
60+
payload = int(incoming["payload"].decode())
61+
except ValueError:
62+
continue
63+
if payload < 0:
64+
continue
65+
# Update the sleep time.
66+
sleep_time = payload * 1000
67+
print("Temperature/humidity service stopped.\n" if sleep_time == 0
68+
else "Changed sleep time to %d s.\n" % payload)
69+
70+
if sleep_time > 0:
71+
# Read the temperature and humidity.
72+
temp_celsius = sensor.read_temperature(True)
73+
humidity_hr = sensor.read_humidity()
74+
75+
# Print values in the console.
76+
print("- Temperature: %s C" % round(temp_celsius, 1))
77+
print("- Humidity: %s %%\n" % round(humidity_hr, 1))
78+
79+
# Create the message to send with the temperature and humidity.
80+
tempHum = "{0}{1}{2}".format(
81+
round(temp_celsius, 1),
82+
MSG_SEPARATOR,
83+
round(humidity_hr, 1)
84+
)
85+
86+
# Send values to the gateway (coordinator).
87+
try:
88+
xbee.transmit(xbee.ADDR_COORDINATOR, tempHum)
89+
except OSError as e:
90+
print("Could not transmit data: " + str(e))
91+
92+
# Sleep for the configured timeout.
93+
xb.sleep_now(sleep_time)

0 commit comments

Comments
 (0)