Skip to content

Commit c609bf5

Browse files
committed
examples: add new example to receive a file from the Bluetooth interface
This example is used in conjunction with the 'XBee BLE Microcontroller' example of the Mobile SDK. Signed-off-by: Ruben Moral <ruben.moral@digi.com>
1 parent 42d012f commit c609bf5

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 datetime import datetime
17+
18+
# TODO: Replace with the serial port where your local module is connected to.
19+
PORT = "COM1"
20+
# TODO: Replace with the baud rate of your local module.
21+
BAUD_RATE = 9600
22+
23+
SEPARATOR = "@@@"
24+
25+
MSG_START = "START" + SEPARATOR
26+
MSG_END = "END"
27+
MSG_ACK = "OK"
28+
29+
30+
def main():
31+
print(" +---------------------------------------------------+")
32+
print(" | XBee Python Library Receive Bluetooth File Sample |")
33+
print(" +---------------------------------------------------+\n")
34+
35+
file = None
36+
37+
device = XBeeDevice(PORT, BAUD_RATE)
38+
39+
try:
40+
device.open()
41+
42+
def bluetooth_data_callback(data):
43+
global file
44+
45+
# Check if the data is 'START' or 'END'.
46+
if data.startswith(bytearray(MSG_START, 'utf-8')):
47+
# Get the file name.
48+
file_name = data.decode('utf-8').split(SEPARATOR)[1]
49+
# Open the file for writing.
50+
file = open(file_name, "w+b")
51+
print(">> START message received, saving data to file...")
52+
send_ack(device)
53+
elif data == bytearray(MSG_END, 'utf-8'):
54+
file.close()
55+
print(">> END message received, file '%s'\n" % file.name)
56+
send_ack(device)
57+
elif file is not None:
58+
payload = data[:-1]
59+
checksum = data[-1]
60+
# Validate the checksum.
61+
if 0xFF - (sum(payload) & 0xFF) == checksum:
62+
# Write block to file.
63+
file.write(payload)
64+
send_ack(device)
65+
66+
device.add_bluetooth_data_received_callback(bluetooth_data_callback)
67+
68+
print("Waiting for data from the Bluetooth interface...\n")
69+
input()
70+
71+
finally:
72+
if file is not None and file.closed:
73+
file.close()
74+
if device is not None and device.is_open():
75+
device.close()
76+
77+
78+
def send_ack(device):
79+
"""
80+
Sends the ACK message to the given XBee device.
81+
82+
Args:
83+
device: XBee device to send the message to.
84+
"""
85+
device.send_bluetooth_data(MSG_ACK.encode("utf-8"))
86+
87+
88+
if __name__ == '__main__':
89+
main()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 and stores it in a file.
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 'XBee BLE Microcontroller' sample
21+
installed.
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, launch the 'XBee BLE
55+
Microcontroller' sample of the Digi Mobile SDK and follow the instructions
56+
explained in that sample's README file.
57+
58+
When you load and send a file in your mobile device, the sample prints out
59+
the following messages in the console at the beginning and at the end of the
60+
process:
61+
62+
>> START message received, saving data to file...
63+
>> END message received, file 'received_file_20190627120254.txt'
64+
65+
Verify that the received file is created successfully and is the same as the
66+
one you sent with the mobile application.

0 commit comments

Comments
 (0)