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:

  1. Temp(LSB) - The least significant byte of the temperature data in hex.
  2. Temp(MSB) - The most significant byte of the temperature data in hex.
  3. Humidity(LSB) - The least significant byte of the humidity data in hex.
  4. 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
[B0:B4:48:B9:2C:82][LE]> connect
Attempting to connect to B0:B4:48:B9:2C:82
Connection successful
[B0:B4:48:B9:2C:82][LE]> char-write-req 0x2C 01
Characteristic value was written successfully
[B0:B4:48:B9:2C:82][LE]> char-read-hnd 0x29
Characteristic value/descriptor: 64 66 0c a2
[B0:B4:48:B9:2C:82][LE]> char-write-req 0x2C 00
Characteristic value was written successfully
[B0:B4:48:B9:2C:82][LE]> disconnect
[B0:B4:48:B9:2C:82][LE]> exit

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
if [ "$#" != 1 ]; then
  echo "Need to pass BLE Address $0 <BLE Address>"
else
  echo "Turning on Temperature and Humidity Sensor"
  gatttool -b $1 --char-write-req --handle=0x2C --value=01
  echo "Getting Temperature and Humidity"
  for i in `seq 1 10`;
  do
    sleep 1
    str=`gatttool -b $1 --char-read --handle=0x29`
    # Discard everything up to and including the ": " in the

    # gatttool response.  Put the four bytes returned into an
    # array. Then swap the bytes and put the results in a variable. 
    #
Use bc to compute the temp and humdity.
    IFS=' ' read -a array <<< ${str#*: }
    temp="0x${array[1]}${array[0]}"
    temp=$((temp))
    humidity="0x${array[3]}${array[2]}"
    humidity=$((humidity))
    printf "temp: %s, humidity: %s\n" \
      `echo "scale=4; ($temp/65536*165-40)*1.8+32" | bc -l` \
      `echo "scale=4; ($humidity*1.0/65536.0)*100.0" | bc -l`
  done
  echo "Turning off Temperature and Humidity Sensor"
  gatttool -b $1 --char-write-req --handle=0x2C --value=00
fi

No comments: