This post is part of my RoadTest review for the TI CC2650STK SimpleLink IoT SensorTag. As I plan to use the BeagleBone Black for data collection, I thought I’d blog about how to get BLE to work on it first.
Here we go …
Preparing the BBB
Before we jump to setting up BLE on the BBB, we require an operational BBB with internet connection. The full setup of the BBB using Debian can be found in one of my earlier posts: [AirCare] InTheAir – Week 2: Preparing the Beaglebone Black
It covers the following topics:
- preparing the SD card with Debian
- expanding the filesystem, making use of the full SD card’s available space
- setting up network connectivity using wifi
- connect a touchscreen display
Setting up Bluetooth
Update
I hadn’t use my BBB for a while before setting up the bluetooth, so the first thing I did was to update the software.
debian@beaglebone:~$ sudo apt-get update debian@beaglebone:~$ sudo apt-get upgrade
Bluetooth
Once the BBB’s software was up-to-date, I installed the bluetooth software.
debian@beaglebone:~$ sudo apt-get install bluetooth
This resulted in a lot of packages being installed, for a total of 144MB of additional disk space.
The following extra packages will be installed: bc bluez bluez-alsa bluez-cups bluez-gstreamer cups cups-client cups-common cups-filters cups-ppdc dc fonts-droid fonts-freefont-ttf foomatic-db-compressed-ppds foomatic-db-engine foomatic-filters ghostscript ghostscript-cups hpijs hplip hplip-data libart-2.0-2 libcupscgi1 libcupsdriver1 libcupsfilters1 libcupsimage2 libcupsmime1 libcupsppdc1 libescpr1 libgs9 libgs9-common libgutenprint2 libhpmud0 libijs-0.35 libjbig2dec0 liblcms1 libpaper-utils libpaper1 libpoppler19 libsane-hpaio libsensors4 libslp1 libsnmp-base libsnmp15 mscompress poppler-data poppler-utils printer-driver-all printer-driver-c2050 printer-driver-c2esp printer-driver-cjet printer-driver-escpr printer-driver-foo2zjs printer-driver-gutenprint printer-driver-hpcups printer-driver-hpijs printer-driver-m2300w printer-driver-min12xxw printer-driver-pnm2ppa printer-driver-postscript-hp printer-driver-ptouch printer-driver-pxljr printer-driver-sag-gdi printer-driver-splix python-imaging python-pexpect python-renderpm python-reportlab python-reportlab-accel samba-common samba-common-bin smbclient ttf-freefont Suggested packages: cups-bsd cups-pdf xpp hplip-cups openprinting-ppds cjet foomatic-db-gutenprint ghostscript-x hplip-gui hplip-doc system-config-printer gutenprint-locales liblcms-utils lm-sensors slpd openslp-doc snmp-mibs-downloader fonts-japanese-mincho fonts-ipafont-mincho fonts-japanese-gothic fonts-ipafont-gothic fonts-arphic-ukai fonts-arphic-uming fonts-unfonts-core psutils hannah-foo2zjs tk8.4 tix gutenprint-doc hpijs-ppds magicfilter apsfilter python-imaging-doc python-imaging-dbg python-renderpm-dbg pdf-viewer python-egenix-mxtexttools python-reportlab-doc cifs-utils Recommended packages: foomatic-db The following NEW packages will be installed: bc bluetooth bluez bluez-alsa bluez-cups bluez-gstreamer cups cups-client cups-common cups-filters cups-ppdc dc fonts-droid fonts-freefont-ttf foomatic-db-compressed-ppds foomatic-db-engine foomatic-filters ghostscript ghostscript-cups hpijs hplip hplip-data libart-2.0-2 libcupscgi1 libcupsdriver1 libcupsfilters1 libcupsimage2 libcupsmime1 libcupsppdc1 libescpr1 libgs9 libgs9-common libgutenprint2 libhpmud0 libijs-0.35 libjbig2dec0 liblcms1 libpaper-utils libpaper1 libpoppler19 libsane-hpaio libsensors4 libslp1 libsnmp-base libsnmp15 mscompress poppler-data poppler-utils printer-driver-all printer-driver-c2050 printer-driver-c2esp printer-driver-cjet printer-driver-escpr printer-driver-foo2zjs printer-driver-gutenprint printer-driver-hpcups printer-driver-hpijs printer-driver-m2300w printer-driver-min12xxw printer-driver-pnm2ppa printer-driver-postscript-hp printer-driver-ptouch printer-driver-pxljr printer-driver-sag-gdi printer-driver-splix python-imaging python-pexpect python-renderpm python-reportlab python-reportlab-accel samba-common samba-common-bin smbclient ttf-freefont 0 upgraded, 74 newly installed, 0 to remove and 0 not upgraded. Need to get 47.8 MB of archives. After this operation, 144 MB of additional disk space will be used. Do you want to continue [Y/n]? y
Dongle
I got myself a cheap BT dongle from Trust, paying special attention to support for Bluetooth 4.0 (BLE).
Plugged in the dongle in the BBB’s USB port and check if the device was detected properly. If you have more than one USB device to connect, you may need a powered USB HUB to connect them all simultaneously.
debian@beaglebone:~$ lsusb Bus 001 Device 003: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode) Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Tools
With the software installed and the dongle plugged it, it’s time to look at the software tools used to get some data from the SensorTag.
hcitool
hcitool is used to configure Bluetooth connections and send some special command to Bluetooth devices.
First, check hcitool detects your bluetooth dongle.
debian@beaglebone:~$ sudo hcitool dev Devices: hci0 00:1A:7D:DA:71:0F
Perform a scan to find out the SensorTag’s address.
debian@beaglebone:~$ sudo hcitool lescan LE Scan ... B0:B4:48:B8:23:01 (unknown) B0:B4:48:B8:23:01 CC2650 SensorTag ...
Create connection to SensorTag, using previously scanned address.
debian@beaglebone:~$ sudo hcitool lecc B0:B4:48:B8:23:01 Connection handle 69
gatttool
gatttool is a tool used to interact with BLE devices.
Use gatttool in interactive mode to send commands to the previously scanned address.
debian@beaglebone:~$ sudo gatttool -b B0:B4:48:B8:23:01 -I [ ][B0:B4:48:B8:23:01][LE]>
Connect to the SensorTag, notice how the prefix changes from “[ ]” to “[CON]”.
[ ][B0:B4:48:B8:23:01][LE]> connect [CON][B0:B4:48:B8:23:01][LE]>
Request the characteristics of the device.
[CON][B0:B4:48:B8:23:01][LE]> characteristics [CON][B0:B4:48:B8:23:01][LE]> handle: 0x0002, char properties: 0x02, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb handle: 0x0006, char properties: 0x02, char value handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb handle: 0x0009, char properties: 0x20, char value handle: 0x000a, uuid: 00002a05-0000-1000-8000-00805f9b34fb handle: 0x000d, char properties: 0x02, char value handle: 0x000e, uuid: 00002a23-0000-1000-8000-00805f9b34fb handle: 0x000f, char properties: 0x02, char value handle: 0x0010, uuid: 00002a24-0000-1000-8000-00805f9b34fb handle: 0x0011, char properties: 0x02, char value handle: 0x0012, uuid: 00002a25-0000-1000-8000-00805f9b34fb handle: 0x0013, char properties: 0x02, char value handle: 0x0014, uuid: 00002a26-0000-1000-8000-00805f9b34fb handle: 0x0015, char properties: 0x02, char value handle: 0x0016, uuid: 00002a27-0000-1000-8000-00805f9b34fb handle: 0x0017, char properties: 0x02, char value handle: 0x0018, uuid: 00002a28-0000-1000-8000-00805f9b34fb handle: 0x0019, char properties: 0x02, char value handle: 0x001a, uuid: 00002a29-0000-1000-8000-00805f9b34fb handle: 0x001b, char properties: 0x02, char value handle: 0x001c, uuid: 00002a2a-0000-1000-8000-00805f9b34fb handle: 0x001d, char properties: 0x02, char value handle: 0x001e, uuid: 00002a50-0000-1000-8000-00805f9b34fb handle: 0x0020, char properties: 0x12, char value handle: 0x0021, uuid: f000aa01-0451-4000-b000-000000000000 handle: 0x0023, char properties: 0x0a, char value handle: 0x0024, uuid: f000aa02-0451-4000-b000-000000000000 handle: 0x0025, char properties: 0x0a, char value handle: 0x0026, uuid: f000aa03-0451-4000-b000-000000000000 handle: 0x0028, char properties: 0x12, char value handle: 0x0029, uuid: f000aa21-0451-4000-b000-000000000000 handle: 0x002b, char properties: 0x0a, char value handle: 0x002c, uuid: f000aa22-0451-4000-b000-000000000000 handle: 0x002d, char properties: 0x0a, char value handle: 0x002e, uuid: f000aa23-0451-4000-b000-000000000000 handle: 0x0030, char properties: 0x12, char value handle: 0x0031, uuid: f000aa41-0451-4000-b000-000000000000 handle: 0x0033, char properties: 0x0a, char value handle: 0x0034, uuid: f000aa42-0451-4000-b000-000000000000 handle: 0x0035, char properties: 0x0a, char value handle: 0x0036, uuid: f000aa44-0451-4000-b000-000000000000 handle: 0x0038, char properties: 0x12, char value handle: 0x0039, uuid: f000aa81-0451-4000-b000-000000000000 handle: 0x003b, char properties: 0x0a, char value handle: 0x003c, uuid: f000aa82-0451-4000-b000-000000000000 handle: 0x003d, char properties: 0x0a, char value handle: 0x003e, uuid: f000aa83-0451-4000-b000-000000000000 handle: 0x0040, char properties: 0x12, char value handle: 0x0041, uuid: f000aa71-0451-4000-b000-000000000000 handle: 0x0043, char properties: 0x0a, char value handle: 0x0044, uuid: f000aa72-0451-4000-b000-000000000000 handle: 0x0045, char properties: 0x0a, char value handle: 0x0046, uuid: f000aa73-0451-4000-b000-000000000000 handle: 0x0048, char properties: 0x10, char value handle: 0x0049, uuid: 0000ffe1-0000-1000-8000-00805f9b34fb handle: 0x004d, char properties: 0x0a, char value handle: 0x004e, uuid: f000aa65-0451-4000-b000-000000000000 handle: 0x004f, char properties: 0x0a, char value handle: 0x0050, uuid: f000aa66-0451-4000-b000-000000000000 handle: 0x0052, char properties: 0x1a, char value handle: 0x0053, uuid: f000ac01-0451-4000-b000-000000000000 handle: 0x0055, char properties: 0x0a, char value handle: 0x0056, uuid: f000ac02-0451-4000-b000-000000000000 handle: 0x0057, char properties: 0x0a, char value handle: 0x0058, uuid: f000ac03-0451-4000-b000-000000000000 handle: 0x005a, char properties: 0x12, char value handle: 0x005b, uuid: f000ccc1-0451-4000-b000-000000000000 handle: 0x005d, char properties: 0x08, char value handle: 0x005e, uuid: f000ccc2-0451-4000-b000-000000000000 handle: 0x005f, char properties: 0x08, char value handle: 0x0060, uuid: f000ccc3-0451-4000-b000-000000000000 handle: 0x0062, char properties: 0x1c, char value handle: 0x0063, uuid: f000ffc1-0451-4000-b000-000000000000 handle: 0x0066, char properties: 0x1c, char value handle: 0x0067, uuid: f000ffc2-0451-4000-b000-000000000000
Request the device name using the proper characteristic, by handle.
[CON][B0:B4:48:B8:23:01][LE]> char-read-hnd 0x3 [CON][B0:B4:48:B8:23:01][LE]> Characteristic value/descriptor: 53 65 6e 73 6f 72 54 61 67 20 32 2e 30
The response is hexadecimal. To make it human readable, it needs to be converted to ASCII. This can be done using some command line magic.
debian@beaglebone:~$ echo -e $(echo -e " 53 65 6e 73 6f 72 54 61 67 20 32 2e 30" | sed 's/ /\\x/g') | awk '{printf "%s\n", $_}' SensorTag 2.0
(The above command is probably far too complicated for what I’m trying to achieve, so if any command line wizard is reading this, please help improve my magic …)
Or using Python for example.
debian@beaglebone:~$ python Python 2.7.3 (default, Mar 14 2014, 17:55:54) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> "53656e736f7254616720322e30".decode("hex") 'SensorTag 2.0'
Conclusion
There you have it, BLE on the BBB. I was able to connect to the SensorTag and retrieve it’s name. Once I’ve identified the other characteristics, I’ll be able to take in the data I want and keep a history of it.