Skip to content

Commit 2932367

Browse files
committed
examples: add new samples to send and receive User Data Relay messages
- communication/relay/SendUserDataRelaySample - communication/relay/ReceiveUserDataRelaySample Signed-off-by: Diego Escalona <diego.escalona@digi.com>
1 parent 0b9017a commit 2932367

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2019, Digi International Inc.
2+
#
3+
# Permission to use, copy, modify, and/or distribute this software for any
4+
# purpose with or without fee is hereby granted, provided that the above
5+
# copyright notice and this permission notice appear in all copies.
6+
#
7+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
from digi.xbee.devices import XBeeDevice
16+
17+
# TODO: Replace with the serial port where your local module is connected to.
18+
PORT = "COM1"
19+
# TODO: Replace with the baud rate of your local module.
20+
BAUD_RATE = 9600
21+
22+
23+
def main():
24+
print(" +----------------------------------------------------+")
25+
print(" | XBee Python Library Receive User Data Relay Sample |")
26+
print(" +----------------------------------------------------+\n")
27+
28+
device = XBeeDevice(PORT, BAUD_RATE)
29+
30+
try:
31+
device.open()
32+
33+
def relay_data_callback(relay_message):
34+
print("Relay data received from %s >> '%s'" %
35+
(relay_message.relay_interface.name,
36+
relay_message.data.decode("utf-8")))
37+
38+
device.add_user_data_relay_received_callback(relay_data_callback)
39+
40+
print("Waiting for User Data Relay messages...\n")
41+
input()
42+
43+
finally:
44+
if device is not None and device.is_open():
45+
device.close()
46+
47+
48+
if __name__ == '__main__':
49+
main()
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Introduction
2+
------------
3+
This sample Python application shows how to receive and process data coming
4+
from other interfaces of the XBee device using a callback executed every time
5+
a new User Data Relay message is received.
6+
7+
The application prints the interface that sent the message and the data of
8+
the message.
9+
10+
NOTE: This example uses the generic XBee device (XBeeDevice) class, but it
11+
can be applied to any other local XBee device class.
12+
13+
14+
Requirements
15+
------------
16+
To run this example you will need:
17+
18+
* One XBee3 module in API mode and its corresponding carrier board (XBIB
19+
or equivalent).
20+
* The XCTU application (available at www.digi.com/xctu).
21+
* An Android or iOS device with the Digi XBee Mobile application installed
22+
(available at Google Play and App Store).
23+
24+
25+
Compatible protocols
26+
--------------------
27+
* 802.15.4
28+
* Cellular
29+
* DigiMesh
30+
* Zigbee
31+
32+
33+
Example setup
34+
-------------
35+
1) Plug the XBee radio into the XBee adapter and connect it to your
36+
computer's USB or serial port.
37+
38+
2) Ensure that the module is in API mode.
39+
For further information on how to perform this task, read the
40+
'Configuring Your XBee Modules' topic of the Getting Started guide.
41+
42+
3) Set the port and baud rate of the XBee radio in the sample file.
43+
If you configured the module in the previous step with XCTU, you will
44+
see the port number and baud rate in the 'Port' label of the device
45+
on the left view.
46+
47+
48+
Running the example
49+
-------------------
50+
First, build and launch the application. Then, you need to send a User Data
51+
Relay message from the XBee Mobile application to the serial interface.
52+
Follow the steps below to do so:
53+
54+
1) Launch the XBee Mobile application on your mobile device.
55+
56+
2) Wait until your XBee device is discovered and connect to it with the
57+
password you configured during the setup.
58+
59+
3) Open the Relay Console from the Options menu.
60+
61+
4) Tap on the Add (+) button to create a new frame. Select 'SERIAL' as
62+
Relay interface and enter 'Hello XBee!' as data. Then, tap on 'Add'.
63+
64+
5) Send this frame by selecting it and taping on 'Send selected frame'.
65+
66+
When the User Data Relay frame is sent, verify that a line with the source
67+
interface and data is printed out in the console of the launched application:
68+
69+
Relay data received from BLUETOOTH >> 'Hello XBee!'
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2019, Digi International Inc.
2+
#
3+
# Permission to use, copy, modify, and/or distribute this software for any
4+
# purpose with or without fee is hereby granted, provided that the above
5+
# copyright notice and this permission notice appear in all copies.
6+
#
7+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
from digi.xbee.devices import XBeeDevice
16+
from digi.xbee.models.options import UserDataRelayInterface
17+
import time
18+
19+
# TODO: Replace with the serial port where your local module is connected to.
20+
PORT = "COM1"
21+
# TODO: Replace with the baud rate of your local module.
22+
BAUD_RATE = 9600
23+
DEST_INTERFACE = UserDataRelayInterface.BLUETOOTH
24+
# TODO: Optionally, replace with the text of the message.
25+
DATA_TO_SEND = "Hello from the serial interface (#%s)"
26+
27+
28+
def main():
29+
print(" +-------------------------------------------------+")
30+
print(" | XBee Python Library Send User Data Relay Sample |")
31+
print(" +-------------------------------------------------+\n")
32+
33+
device = XBeeDevice(PORT, BAUD_RATE)
34+
35+
try:
36+
device.open()
37+
38+
for i in range(10):
39+
data = (DATA_TO_SEND % (i + 1))
40+
41+
print("Sending User Data Relay to %s >> '%s'... " %
42+
(DEST_INTERFACE.name, data), end="")
43+
44+
device.send_user_data_relay(DEST_INTERFACE, data.encode("utf-8"))
45+
46+
print("Success")
47+
48+
time.sleep(1)
49+
finally:
50+
if device is not None and device.is_open():
51+
device.close()
52+
53+
54+
if __name__ == '__main__':
55+
main()
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Introduction
2+
------------
3+
This sample Python application shows how to send data to other interfaces of
4+
the XBee device.
5+
6+
The application sends 10 User Data Relay messages to the Bluetooth Low Energy
7+
interface.
8+
9+
NOTE: This example uses the generic XBee device (XBeeDevice) class, but it
10+
can be applied to any other local XBee device class.
11+
12+
13+
Requirements
14+
------------
15+
To run this example you will need:
16+
17+
* One XBee3 module in API mode and its corresponding carrier board (XBIB
18+
or equivalent).
19+
* The XCTU application (available at www.digi.com/xctu).
20+
* An Android or iOS device with the Digi XBee Mobile application installed
21+
(available at Google Play and App Store).
22+
23+
24+
Compatible protocols
25+
--------------------
26+
* 802.15.4
27+
* Cellular
28+
* DigiMesh
29+
* Zigbee
30+
31+
32+
Example setup
33+
-------------
34+
1) Plug the XBee radio into the XBee adapter and connect it to your
35+
computer's USB or serial port.
36+
37+
2) Ensure that the module is in API mode.
38+
For further information on how to perform this task, read the
39+
'Configuring Your XBee Modules' topic of the Getting Started guide.
40+
41+
3) Enable the Bluetooth interface of the XBee device and configure the
42+
Bluetooth authentication using XCTU.
43+
For further information on how to perform this task, refer to the
44+
XCTU user manual.
45+
46+
4) Set the port and baud rate of the XBee radio in the sample file.
47+
If you configured the module in the previous step with XCTU, you
48+
will see the port number and baud rate in the 'Port' label of the
49+
device on the left view.
50+
51+
52+
Running the example
53+
-------------------
54+
First, build the application. Then, you need to set up the XBee Mobile app
55+
to see the data received. Follow these steps to do so:
56+
57+
1) Launch the XBee Mobile application on your mobile device.
58+
59+
2) Wait until your XBee device is discovered and connect to it with the
60+
password you configured during the setup.
61+
62+
3) Open the Relay Console from the Options menu.
63+
64+
Finally, launch the sample application. A total of 10 User Data Relay
65+
messages are sent to the Bluetooth interface. When that happens, a line with
66+
the result of each operation is printed to the standard output:
67+
68+
Sending User Data Relay to BLUETOOTH >> 'Hello from the serial interface (#1)'... Success
69+
Sending User Data Relay to BLUETOOTH >> 'Hello from the serial interface (#2)'... Success
70+
Sending User Data Relay to BLUETOOTH >> 'Hello from the serial interface (#3)'... Success
71+
...
72+
73+
Verify in the Relay Console of the XBee Mobile app that new frames have been
74+
received. Tap on one of them and then on the info button, the details will be
75+
similar to:
76+
77+
- Time: Received time.
78+
- Relay interface SERIAL.
79+
- Data: Hello from the serial interface (#1)

0 commit comments

Comments
 (0)