loading

In this tutorial, I show how to implement the connection with Dragonboard410c and CSR1011 through Bluetooth low energy. We use a library called PyGatt to intermediate communication between the devices.

This is a good way to start with IoT applications because is simple to implement and can integrate BLE's devices.

Step 1: Dragonboard System Requirements

Due to the libraries that we will use, this project only works on Linux based systems. The Dragonboard 410c supports a Debian OS edited by Linaro. Follow this steps to install the OS.

The PyGatt library is based on PyBluez, open the LX terminal in the Dragonboard and enter this commands.

$ sudo apt-get update<br>$ sudo apt-get install python-pip python-dev ipython
$ sudo apt-get install bluetooth libbluetooth-dev
$ sudo pip install pybluez
$ sudo pip install pygatt
$ sudo apt-get install libdbus-1-dev libdbus-glib-1-dev libglib2.0-dev libical-dev libreadline-dev libudev-dev libusb-dev make<br>
$ mkdir -p work/bluepy<br>cd work/bluepy 
wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.32.tar.xz
tar xvf bluez-5.32.tar.xz

$ cd bluez-5.32
./configure --disable-systemd
make

$ sudo make install
$ sudo reboot

Step 2: Starting With PyGatt

To initiate coding, open your preferred editor.

To import the PyGatt module type:

import pygatt

The next step is to instance the adapter, it is responsible for creating the connection between the devices and returning the device.

adapter = pygatt.GATTToolBackend() 
adapter.start()

Now, we are able to connect to our device. In the function connect, the first param is the MAC address of the device to connect and the second is the timeout.

device = adapter.connect('11:22:33:44:55:66', 10)

Step 3: Discovering Characteristics

Characteristics are defined attributes that can contain values, it is possible to read the values or modify it and periodically notify the value.

A UUID is used to identify a characteristic, it is a 128bit number. In the Bluetooth Low Energy context, the UUID is standardized, only need to change the firsts 16bits.

To discover a characteristic, we call the function discover_characteristics().

for uuid in device.discover_characteristics().keys():
    print("Read UUID %s: %s" % (uuid, binascii.hexlify(device.char_read(uuid))))

Step 4: Subscribing to a Characteristic

Subscribe to a characteristic means you will receive data every time the characteristic publish an information.

The PyGatt provides a function to transform the 16bits UUID into 128bits.

from pygatt.util import uuid16_to_uuid
uuid_characteristic = uuid16_to_uuid(0x2A37)

The subscribe function needs two parameters, the first is the uuid_characteristic and the second is the function that will be called after receive data.

device.subscribe(uuid_characteristic, msgcallback)

Is expect receive two parameters in the msgcallback, the handle, and the message. A handle is assigned to each characteristic.

def msgcallback(handle, message):
    print "message received:"
    for i in range(len(message)):
        print str(message[i])

Step 5: Sending and Reading Data Content

To send data to another device, use the function char_write, is important convert the data to a byte array before sending the content.

def getByteArray(message):
i = [message] data = bytearray(i) return data device.char_write(uuid, getByteArray(message))

Read a data is simples, just need call the function char_read. The data received is always a byte array.

data = device.char_read(uuid)
for i in range(len(data)):
    print str(data[i])

Step 6: Introducing CSR 1011

CSR 1011 is a Bluetooth low energy board. We will use the example that comes with the board, the heart rate sensor. It measures the heart rate beat and sends to Dragonboard the value.

Open the project hr_sensor in the IDE and build it to the board.

Step 7: CSR Characteristics

The CSR characteristics are defined through JSON pattern, every characteristic belongs to a service and they have properties that define the type and use of each one.

For example, the characteristics declaration of the heart rate measurement project.

primary_service {
    uuid : UUID_HEART_RATE_SERVICE,
    name : "HEART_RATE_SERVICE",
    characteristic {
        uuid : UUID_HEART_RATE_MEASUREMENT,
        name : "HEART_RATE_MEASUREMENT",
        properties : notify,
        flags : FLAG_IRQ,
        size_value : 0x11,
        client_config {
            flags : FLAG_IRQ,
            name : "HEART_RATE_MEASUREMENT_C_CFG"
        }
    },
},

The properties define what function the characteristic will exercise, e.g: read will only allow the device read the value. The image ahead explains better the properties attributes.

Step 8: Connecting and Subscribing

Repeat the step 2 with the CSR1011 MAC Address.

Open the hr_sensor project and look for the heart_rate_service_db.db file, search the characteristic with name HEART_RATE_MEASUREMENT, observe the UUID property and open the heart_rate_service_uuids.h file and search for the UUID number correspondent to the UUID property from the heart rate measurement (0x2A37).

Repeat the step 4 with the found UUID number.

Now, every time the CSR 1011 make a measure of the heart rate and send the data, you will receive it.

Step 9: Code Example

This code demonstrate how to receive data from CSR1011.

<p>Very helpful!</p>

About This Instructable

123views

8favorites

More by Matheus Campos:Dragonboard and BLE Device CSR1011 CSR1011 - Water Level Sensor Dragonboard and AWS IoT - Python 
Add instructable to: