Skip to content

Commit 50dc641

Browse files
committed
examples: add new examples to send and receive data through the Bluetooth
and MicroPython interfaces Signed-off-by: Ruben Moral <ruben.moral@digi.com>
1 parent 9706b6a commit 50dc641

File tree

9 files changed

+550
-4
lines changed

9 files changed

+550
-4
lines changed

doc/examples.rst

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,34 @@ You can find the example at the following path:
276276
:ref:`communicateSendIPData`.
277277

278278

279+
Send Bluetooth Data
280+
```````````````````
281+
282+
This sample application shows how to send data to the XBee Bluetooth Low Energy
283+
interface.
284+
285+
You can find the example at the following path:
286+
**examples/communication/bluetooth/SendBluetoothDataSample**
287+
288+
.. note::
289+
For more information about sending Bluetooth data, see
290+
:ref:`communicateSendBluetoothData`.
291+
292+
293+
Send MicroPython Data
294+
`````````````````````
295+
296+
This sample application shows how to send data to the XBee MicroPython
297+
interface.
298+
299+
You can find the example at the following path:
300+
**examples/communication/micropython/SendMicroPythonDataSample**
301+
302+
.. note::
303+
For more information about sending MicroPython data, see
304+
:ref:`communicateSendMicroPythonData`.
305+
306+
279307
Send User Data Relay
280308
````````````````````
281309

@@ -286,7 +314,7 @@ You can find the example at the following path:
286314

287315
.. note::
288316
For more information about sending User Data Relay messages, see
289-
:ref:`communicateSendRelayData`.
317+
:ref:`communicateSendBluetoothData` or :ref:`communicateSendMicroPythonData`.
290318

291319

292320
Receive data
@@ -383,8 +411,36 @@ You can find the example at the following path:
383411
:ref:`communicateReceiveSMS`.
384412

385413

386-
Receive User Data Relay messages
387-
````````````````````````````````
414+
Receive Bluetooth data
415+
``````````````````````
416+
417+
This sample application shows how to receive data from the XBee Bluetooth Low
418+
Energy interface.
419+
420+
You can find the example at the following path:
421+
**examples/communication/bluetooth/ReceiveBluetoothDataSample**
422+
423+
.. note::
424+
For more information about receiving Bluetooth data, see
425+
:ref:`communicateReceiveBluetoothData`.
426+
427+
428+
Receive MicroPython data
429+
````````````````````````
430+
431+
This sample application shows how to receive data from the XBee MicroPython
432+
interface.
433+
434+
You can find the example at the following path:
435+
**examples/communication/micropython/ReceiveMicroPythonDataSample**
436+
437+
.. note::
438+
For more information about receiving MicroPython data, see
439+
:ref:`communicateReceiveMicroPythonData`.
440+
441+
442+
Receive User Data Relay
443+
```````````````````````
388444

389445
This sample application shows how to receive data from other XBee interface.
390446

@@ -393,7 +449,8 @@ You can find the example at the following path:
393449

394450
.. note::
395451
For more information about receiving User Data Relay messages, see
396-
:ref:`communicateReceiveRelayData`.
452+
:ref:`communicateReceiveBluetoothData` or
453+
:ref:`communicateReceiveMicroPythonData`.
397454

398455

399456
Receive modem status
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 Bluetooth Data Sample |")
26+
print(" +---------------------------------------------------+\n")
27+
28+
device = XBeeDevice(PORT, BAUD_RATE)
29+
30+
try:
31+
device.open()
32+
33+
def bluetooth_data_callback(data):
34+
print("Data received from Bluetooth >> '%s'" % data.decode("utf-8"))
35+
36+
device.add_bluetooth_data_received_callback(bluetooth_data_callback)
37+
38+
print("Waiting for data from the Bluetooth interface...\n")
39+
input()
40+
41+
finally:
42+
if device is not None and device.is_open():
43+
device.close()
44+
45+
46+
if __name__ == '__main__':
47+
main()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Introduction
2+
------------
3+
This sample Python application shows how to receive and process data coming
4+
from the Bluetooth interface of the XBee device.
5+
6+
The application registers a callback to be notified when new data coming from
7+
Bluetooth is received and prints it to the standard output.
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 will
48+
see the port number and baud rate in the 'Port' label of the device
49+
on the left view.
50+
51+
52+
Running the example
53+
-------------------
54+
First, build and launch the application. Then, you need to send a User Data
55+
Relay message from the XBee Mobile application to the serial interface.
56+
Follow the steps below to do so:
57+
58+
1) Launch the XBee Mobile application on your mobile device.
59+
60+
2) Wait until your XBee device is discovered and connect to it with the
61+
password you configured during the setup.
62+
63+
3) Open the Relay Console from the Options menu.
64+
65+
4) Tap on the Add (+) button to create a new frame. Select 'SERIAL' as
66+
Relay interface and enter 'Hello XBee!' as data. Then, tap on 'Add'.
67+
68+
5) Send this frame by selecting it and taping on 'Send selected frame'.
69+
70+
When the User Data Relay frame is sent, verify that a line with the data is
71+
printed out in the console of the launched application:
72+
73+
Data received from Bluetooth >> 'Hello XBee!'
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 XBeeLocalInterface
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+
# TODO: Optionally, replace with the text of the message.
24+
DATA_TO_SEND = "Hello from the serial interface (#%s)"
25+
26+
27+
def main():
28+
print(" +------------------------------------------------+")
29+
print(" | XBee Python Library Send Bluetooth Data Sample |")
30+
print(" +------------------------------------------------+\n")
31+
32+
device = XBeeDevice(PORT, BAUD_RATE)
33+
34+
try:
35+
device.open()
36+
37+
for i in range(10):
38+
data = (DATA_TO_SEND % (i + 1))
39+
40+
print("Sending data to Bluetooth interface >> '%s'... " % data, end="")
41+
42+
device.send_bluetooth_data(data.encode("utf-8"))
43+
44+
print("Success")
45+
46+
time.sleep(1)
47+
finally:
48+
if device is not None and device.is_open():
49+
device.close()
50+
51+
52+
if __name__ == '__main__':
53+
main()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Introduction
2+
------------
3+
This sample Python application shows how to send data to the Bluetooth
4+
interface of the XBee device.
5+
6+
The application sends 10 messages to the Bluetooth Low Energy interface.
7+
8+
NOTE: This example uses the generic XBee device (XBeeDevice) class, but it
9+
can be applied to any other local XBee device class.
10+
11+
12+
Requirements
13+
------------
14+
To run this example you will need:
15+
16+
* One XBee3 module in API mode and its corresponding carrier board (XBIB
17+
or equivalent).
18+
* The XCTU application (available at www.digi.com/xctu).
19+
* An Android or iOS device with the Digi XBee Mobile application installed
20+
(available at Google Play and App Store).
21+
22+
23+
Compatible protocols
24+
--------------------
25+
* 802.15.4
26+
* Cellular
27+
* DigiMesh
28+
* Zigbee
29+
30+
31+
Example setup
32+
-------------
33+
1) Plug the XBee radio into the XBee adapter and connect it to your
34+
computer's USB or serial port.
35+
36+
2) Ensure that the module is in API mode.
37+
For further information on how to perform this task, read the
38+
'Configuring Your XBee Modules' topic of the Getting Started guide.
39+
40+
3) Enable the Bluetooth interface of the XBee device and configure the
41+
Bluetooth authentication using XCTU.
42+
For further information on how to perform this task, refer to the
43+
XCTU user manual.
44+
45+
4) Set the port and baud rate of the XBee radio in the sample file.
46+
If you configured the module in the previous step with XCTU, you
47+
will see the port number and baud rate in the 'Port' label of the
48+
device on the left view.
49+
50+
51+
Running the example
52+
-------------------
53+
First, build the application. Then, you need to set up the XBee Mobile app
54+
to see the data received. Follow these steps to do so:
55+
56+
1) Launch the XBee Mobile application on your mobile device.
57+
58+
2) Wait until your XBee device is discovered and connect to it with the
59+
password you configured during the setup.
60+
61+
3) Open the Relay Console from the Options menu.
62+
63+
Finally, launch the sample application. A total of 10 data messages are sent
64+
to the Bluetooth interface. When that happens, a line with the result of each
65+
operation is printed to the standard output:
66+
67+
Sending data to Bluetooth interface >> 'Hello from the serial interface (#1)'... Success
68+
Sending data to Bluetooth interface >> 'Hello from the serial interface (#2)'... Success
69+
Sending data to Bluetooth interface >> 'Hello from the serial interface (#3)'... Success
70+
...
71+
72+
Verify in the Relay Console of the XBee Mobile app that new frames have been
73+
received. Tap on one of them and then on the info button, the details will be
74+
similar to:
75+
76+
- Time: Received time.
77+
- Relay interface SERIAL.
78+
- Data: Hello from the serial interface (#1)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 MicroPython Data Sample |")
26+
print(" +-----------------------------------------------------+\n")
27+
28+
device = XBeeDevice(PORT, BAUD_RATE)
29+
30+
try:
31+
device.open()
32+
33+
def micropython_data_callback(data):
34+
print("Data received from MicroPython >> '%s'" % data.decode("utf-8"))
35+
36+
device.add_micropython_data_received_callback(micropython_data_callback)
37+
38+
print("Waiting for data from the MicroPython interface...\n")
39+
input()
40+
41+
finally:
42+
if device is not None and device.is_open():
43+
device.close()
44+
45+
46+
if __name__ == '__main__':
47+
main()

0 commit comments

Comments
 (0)