Having successfully accessed data on the sensor tag I decided to try my hand at programmatically accessing data from the sensor tag. I started out with the same two examples I had developed in bash: 1) to get the device name; and 2) get the humidty reading from the humidity sensor.
Surprisingly there are not too many options when it comes to Bluetooth Low Engergy APOs for Windows 7 or Linux. One of the few that exists is Ian Harvey’s bluepy for python on Linux.
Python isn’t something I use very much. I do most of my work in Perl and C with a bit of Java thrown in for Android. Fortunately there are a set of pretty good set of instructions for setting up bluepy on the Raspberry Pi that can be found at: http://www.elinux.org/RPi_Bluetooth_LE. The output from these instructions is below:
pi@raspberrypi ~ $ git clone https://github.com/IanHarvey/bluepy.git
Running the script generates the a dump of the services and characteristics.
Connecting to: B0:B4:48:B9:2C:82, address type: public
At this point I created a directory SensorTag2015 off my home directory to work in. I copied the bluepy python files into a subdirectory bluepy. This gave me a setup that looked like this.
In the SensorTag2015 directory I created and ran my python examples. I started with a very simple script that expects an address for the sensor tag on the command line.
pi@raspberrypi ~/SensorTag2015 $ python getDeviceName.py 00:00:00:00:00:00
The script imports two methods from the bluepy.btle fileand then it defines a UUID for the device name. It checks to make sure that at least one argument is passed and then establishes a connection to the sensor tag. If the script fails to connect to the sensor tag check to make sure that the sensor tag is advertising. If it isn't push the left button and check that the green LED is flashing. Once connected the script checks to make sure that the device name characteristic is readable, reads it and prints it out.
import sys from bluepy.btle import UUID, Peripheral temp_uuid = UUID(0x2A00) if len(sys.argv) != 2: print "Fatal, must pass device address:", sys.argv, "
Next I developed a slightly more complicated script for reading the Humidity sensor. The template I developed with this script is what I used for most of the examples.
The script expects the address of the sensor tag to be passed on the command line as with the script above. Then it defines TI unique UUIDs to configure the sensor and to get data from the sensor. Next it sets up the values that get written to the sensor's configure UUID to turn the sensor on or off. Note you must turn a sensor on before you are able to read it and get values other than zero back.
After these variables are setup the script connects to the sensor tag. Once connected the script writes the value to turn on the sensor to the configuration UUID. Then it reads the raw data from the sensor tag, converts it to values and calculates the sensor value. The formulas for calculating the sensor values were pulled from the Android App source code. After the values are printed out the script turns of the sensor and disconnects from the device.
# # TI SimpleLink SensorTag 2015 # Date: 2015 07 06 # # Sensor: Humidity Temperature # Values: Temperature and Humidity # import struct, sys, traceback from bluepy.btle import UUID, Peripheral, BTLEException def TI_UUID(val): return UUID("%08X-0451-4000-b000-000000000000" % (0xF0000000+val)) config_uuid = TI_UUID(0xAA22) data_uuid = TI_UUID(0xAA21) sensorOn = struct.pack("B", 0x01) sensorOff = struct.pack("B", 0x00) if len(sys.argv) != 2: print "Fatal, must pass device address:", sys.argv, "
On GitHub in the dhSensorTag2015 repository you will find these examples as well as additional examples to read the other sensors.
A couple of things to bear in mind with these examples:
- These examples were written using python 2.7.10 and were developed using V1.12 (Jun 23 2015) of TI’s firmware.
- The barometer example calculation in the Andriod App doesn’t appear to be correct and therefore the example uses an updated calculation.
- Data from all of the sensor examples, except the movement sensor, has been validated.