Gerard at 43oh has posted an excellent guide for setting up the development environment for the CC2650STK. I highly recommend that you check it out before you attempt to install Code Composer Studio.
Wednesday, July 22, 2015
Sunday, July 12, 2015
TI SimpleLink SensorTag 2015 - Python
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.
getDeviceName.py
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[0], " |
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.
getHumidity.py
# # 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[0], " |
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.
Friday, July 3, 2015
TI SimpleLink SensorTag 2015 - Reading Temperature and Humidity
Next up we are going to read the temperature and humidity from the HC1000 Humidity Sensor. Each of the sensors that are accessible on the sensor tag has a GATT Service defined for it. Below is the Humidity Service.
Handle | Type | Type (text) | GATT Server | Description/Value (text) |
(hex) | (hex) | Permissions | ||
0x27 | 0x2800 | GATT Primary Service Declaration | R | Humidity Service |
0x28 | 0x2803 | GATT Characteristic Declaration | R | Humidity Data |
0x29 | 0xAA21 | Humidity Data | RN | TempLSB:TempMSB:HumidityLSB:HumidityMSB |
0x2A | 0x2902 | Client Characteristic Configuration | RW | Write "01:00" to enable notifications, "00:00" to disable |
0x2B | 0x2803 | GATT Characteristic Declaration | R | Humidity Config |
0x2C | 0xAA22 | Humidity Config | RW | Write "01" to start measurements, "00" to stop |
0x2D | 0x2803 | GATT Characteristic Declaration | R | Humidity Period |
0x2E | 0xAA23 | Humidity Period | RW | Period = [Input*10] ms, (lower limit 100 ms), default 1000 ms |
Before you can read a sensor value from the service you must turn it on. For the Humidity Service this is done by writing a 01 to the handle at 0x2C labeled in the table above as Humidity Config. Once you have turned on the Humidity Service you will read values from the handle at 0x29 labeled Humidity Data.
Referring to the table above we see that four values will be returned from left to right:
- Temp(LSB) - The least significant byte of the temperature data in hex.
- Temp(MSB) - The most significant byte of the temperature data in hex.
- Humidity(LSB) - The least significant byte of the humidity data in hex.
- Humidity(MSB) - The most significant byte of the humidity data in hex.
On page 14 of data sheet for the HC1000 Humidity Sensor are the formulas for converting this data into a temperature reading and a humidity reading.
- Temperature in Celsius = (Temp(MSB) * 0x100 + Temp(LSB)) / 65536 * 165 – 40
- Humidity = (Humidity(MSB) * 0x100 + Humidity(LSB)) / 65536 * 100
To get the values for the temperature and humidity readings follow the steps below. In this example the values returned are 64 66 0c a2.
pi@raspberrypi ~ $ gatttool -b B0:B4:48:B9:2C:82 -I |
The resulting temperature is:
(0x66 * 0x100 + 0x64) / 65536 * 165 – 40 = 26.19 C
The resulting humidity is:
(0xa2 * 0x100 + 0x0C) / 65536 * 100 = 63.30%
Below is a bash script that will perform the commands above and calculate the temperature and humidity.
getTempHumidity.sh
#!/bin/bash |
TI SimpleLink SensorTag 2015 - Reading the Device Name
In the last post we reviewed setting up a Raspberry Pi with Bluetooth to access the sensor tag. We finished up by successfully performing a Bluetooth Low Energy scan which saw the sensor tag and returned the address.
In this post we will walk through reading and writing to the sensor tag using gatttool which is provided as part of the bluez Bluetooth stack.
Reading the Sensor Tag’s Device Name
As discussed in the previous post you will need to make sure your sensor tag is advertising. When advertising the green LED visible on the back of the sensor tag will be blinking. If it isn’t blinking try removing and reinstalling the battery. If that doesn’t work replace the battery.
In the example below the hcitool lescan function is used to find the sensor tag. It reports back the sensor tag’s address shown in green below. Once you have the sensor tag’s address you should run gatttool providing –b and the address and a –I. The –b tells gatttool that the next set of numbers is the address of the device you want to use. The –I is for interactive mode. gatttool has both an interactive mode and a command line mode.
As you can see the steps start with connecting to the device. If the connect fails it could be because you waited too long and the sensor tag exited advertise mode or it could be that you have a version of gatttool that doesn’t work. Put the sensor tag back into advertise mode, make sure the green LED is blinking and try to connect again. If it fails this time it is most likely problem with gatttool.
Once you successfully connect to the sensor tag the green LED will stop flashing. Enter the char-read-hnd command which stands for read characteristic by handle for handle 0x3 which is the device name. You should get back the string of numbers below which if you convert from ascii values to characters spells SensorTag 2.0.
Disconnect from the sensor tag and the green LED should start blinking again and exit gatttool.
pi@raspberrypi ~ $ sudo hcitool lescan |
Below is a bash script that will perform the commands above and translate the numeric values to their ascii equivalents.
getDeviceName.sh
#!/bin/bash |
TI SimpleLink SensorTag 2015 - First Steps
After running through the Android (or IOS) App the next step was to manually read and write data from and to the sensor tag.
For this I used a Raspberry PI B running the most current version of Raspbian dated 2015-05-05 downloaded from the Raspberry PI Downloads page and an IOGEAR Bluetooth 4.0 USB Micro Adapter.
Raspbian doesn’t have Bluetooth support loaded. Unfortunately, the version that is currently in the repository doesn’t have a working copy of gatttool. As a result you will need both install Bluetooth using apt-get and build bluez from source.
pi@raspberrypi ~ $ sudo apt-get install --no-install-recommends bluetooth <messages removed to save space> pi@raspberrypi ~ $ sudo service bluetooth status |
Bluez source can be downloaded from https://www.kernel.org/pub/linux/bluetooth/. At the time I am writing this July 3, 2015 the current version is 5.31 which built find on the most recent release of Raspbian.
Follow the commands below to build bluez. While you need to build bluez you don’t need to install what you build. You just need to copy the new version of gatttool to /usr/bin and you should be set to go.
pi@raspberrypi ~ $ sudo apt-get install --no-install-recommends bluetooth <messages removed to save space> Setting up libcap-ng0 (0.6.6-2) ... 100%[======================================>] 1,631,664 756K/s in 2.1s 2015-07-03 20:24:08 (756 KB/s) - `bluez-5.31.tar.xz' saved [1631664/1631664] <messages removed to save space> config.status: creating lib/bluez.pc <messages removed to save space> CC tools/obex-server-tool.o |
There are two tools that you will be using with your write data to the sensor tag.
- hcitool is the primary command line tool for configuring Bluetooth connections. It is used to scan for your sensor tag.
- gatttool is a tool that provides command line access to GATT Services on a Bluetooth Low Energy device.
To scan the sensor tag you will need to make sure that it is advertising. When the sensor tag is advertising there is a green led that flashes. The led can be seen if you turn the sensor tag over. It will is in the right window in the green circle. If it is not flashing then you will need to press the round button on the right side of the sensor tag shown in the yellow circle. If it doesn’t try removing and reinstalling the battery. If that doesn’t work replace the battery.
Left | Front | Right | Back |
Once you have the sensor tag broadcasting go to your terminal window and type the hcitool command below.
pi@raspberrypi ~ $ sudo hcitool lescan |
After a second you should see your sensor tag print out on your terminal. Once it has you can enter a ^C to stop the scan. For the next step you will need the address of you sensor tag which is highlighted in green above.
Congratulations you have now successfully scanned your sensor tag. Next up reading data from the sensor tag.