TI Sensortag with Intel Edison and gatttool
Outlined below is the method I used to connect and gather sensor data from the TI Sensortag using the Intel Edison.
TI Sensortag
Install gatttool
First of all I will be using gatttool to connect to the server on the Sensortag in order to read sensor values. Gatttool does not come preinstalled on the Yocto image so it will have to be complied. I downloaded the bluez 5.24 source and compiled it with a few configure options:
cd ~
mkdir bluez_source
cd bluez_sourcec
wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.24.tar.xz --no-check-certificate
tar -xf bluez-5.24.tar.xz
cd bluez-5.24
./configure --disable-systemd --disable-udev
make
make install
After completing the compile you can now launch gatttool from bluez-5.24/attrib/gatttool. However lets link it to make working with gatttool easier.
export PATH=$PATH:~/bluez-5.24/attrib/
Now you can launch gatttool from anywhere
Scanning and Connecting Sensortag with bluetoothctl
First enable bluetooth
rfkill unblock bluetooth
Launch bluetooth ctl
bluetoothctl
Register an agent and set it to default, note: the following commands are launched from the [bluetooth]# shell (i.e. dont type in [bluetooth]#)
[bluetooth]# agent KeyboardDisplay [bluetooth]# default-agent
Perform a scan, pair with the Sensortag and connect to the Sensortag
[bluetooth]# scan on
If the SensorTag does not show up, make sure to hit the pair button on the side of the SensorTag. Make a note of the MAC address of the SensorTag when it appears in the scan list, you will use this address to pair the device. Note: You can tab complete the MAC address.
[bluetooth]# pair 34:B1:F7:D5:15:38
the [agent] will ask for a passkey, enter 0
[bluetooth]# connect 34:B1:F7:D5:15:38 [bluetooth]# scan off
What your terminal output should look like after complete the steps above
You can check the details of the connected device to see the name, alias, and UUIDs of device specific elements.
[bluetooth]# info 34:B1:F7:D5:15:38
You can now exit the bluetooth utility
[bluetooth]# exit
Using gatttool to read sensor values
gatttool can now be used to read sensor data from the SensorTag
Connect to the sensor tag with gatttool in interactive mode:
gatttool -b 34:B1:F7:D5:15:38 -I
Connect to the device, turn on the temperature sensor by writing 01 to the configure handle 0x29 , and read out the value from the temperature handle 0x25.
[34:B1:F7:D5:15:38][LE]> connect
[34:B1:F7:D5:15:38][LE]> char-write-cmd 0x29 01
[34:B1:F7:D5:15:38][LE]> char-read-hnd 0x25
The handle values corresponding to the temperature sensor were pulled from the Sensor Tag attribute table http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf
Gatttool output
The output is two 16 bit unsigned values, in order to convert these values to a temperature reading they must be fed into a script that uses the conversion algorithm outlined in the SensorTag wiki: http://processors.wiki.ti.com/index.php/SensorTag_User_Guide#Sensors_2
In order to do this I used python and the pexpect module which can create a child process that can be written to and read from. I used the python script from https://github.com/msaunby/ble-sensor-pi/blob/master/sensortag/sensortag_test.py,
however I had to change line 62 from:
tool.expect('\[CON\].*>') to tool.expect('Connection successful')
Installing pip and required python modules
In order to run the python script using pexpect, pexpect must be installed which is easiest done with pip. Pip however is not installed on Edison by default and is not present in the official opkg repo. Pip is however present in the unofficial Intel Edison repo compiled by Michael Hirsch. The following is an exerpt from his guide on using the unofficial repo from http://blogs.bu.edu/mhirsch/2014/11/getting-started-with-intel-edison/.
vi /etc/opkg/base-feeds.conf and paste in src/gz all http://repo.opkg.net/edison/repo/all src/gz edison http://repo.opkg.net/edison/repo/edison src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32
Then hit your Escape key and type
:wq opkg update opkg install python-pip
At this point when I tried to run pip I encountered the error:
ImportError: No module named pkg_resources
To fix this I ran the setup script for setuptools by:
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
At this point I was able to run sensortag_test.py and receive temperature data out:
./sensortag_test.py34:B1:F7:D5:15:38