SensorTag Temperature Readings in Python
I wanted to wrap up my previous post TI SensorTag Temperature Readings ) with a little python example, thus this write is going to be short and sweet.
Using the bluepy python library (written by Ian Harvey) I’ve been able to to capture temperature readings, then covert them to Fahrenheit.
To demonstrate I captured a couple temperature samples, a few while sitting on my desk, and a few held up to my air condition vent:
root@raspberrypi:~# python temp.py
79.31 degrees fahrenheit
79.31 degrees fahrenheit
79.31 degrees fahrenheit
78.46 degrees fahrenheit
78.01 degrees fahrenheit
76.1 degrees fahrenheit
76.49 degrees fahrenheit
76.27 degrees fahrenheit
75.99 degrees fahrenheit
76.16 degrees fahrenheit
The Python is pretty straight forward:
#!/usr/bin/env python
from bluepy import btle
from time import sleep
sensor = btle.Peripheral('A0:xx:xx:xx:xx:xx')
sensor.writeCharacteristic(0x24, '\x01', withResponse=True)
sleep(1) # warm up
for i in range(10):
t_data = sensor.readCharacteristic(0x21)
msb = ord(t_data[3])
lsb = ord(t_data[2])
c = ((msb * 256 + lsb) / 4) * 0.03125
f = c * 9/5.0 + 32
print '%s degrees fahrenheit' % round(f, 2)
sleep(2)
sensor.writeCharacteristic(0x24, '\x00', withResponse=True)
Happy hacking!