forked from hermann-kurz/weatherstation-sensortag
/
sensortag-tempodb.py
250 lines (200 loc) · 7.76 KB
/
sensortag-tempodb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from btle import UUID, Peripheral
from tempodb import Client, DataPoint
import struct
import math
import datetime
import time
import random
import sys
# credentials for tempodb API
API_KEY = 'put API key here'
API_SECRET = 'put API secret here'
SERIES_KEY1 = 'luftdruck'
SERIES_KEY2 = 'temperatur'
HEIGHT=900 # we are 900m above sea level
tempodbclient = Client(API_KEY, API_SECRET)
dir(tempodbclient)
def _TI_UUID(val):
return UUID("%08X-0451-4000-b000-000000000000" % (0xF0000000+val))
class SensorBase:
# Derived classes should set: svcUUID, ctrlUUID, dataUUID
sensorOn = chr(0x01)
sensorOff = chr(0x00)
def __init__(self, periph):
self.periph = periph
self.service = self.periph.getServiceByUUID(self.svcUUID)
self.ctrl = None
self.data = None
def enable(self):
if self.ctrl == None:
self.ctrl = self.service.getCharacteristics(self.ctrlUUID) [0]
if self.data == None:
self.data = self.service.getCharacteristics(self.dataUUID) [0]
if self.sensorOn != None:
self.ctrl.write(self.sensorOn,withResponse=True)
def read(self):
return self.data.read()
def disable(self):
if self.ctrl != None:
self.ctrl.write(self.sensorOff)
# Derived class should implement _formatData()
def calcPoly(coeffs, x):
return coeffs[0] + (coeffs[1]*x) + (coeffs[2]*x*x)
class IRTemperatureSensor(SensorBase):
svcUUID = _TI_UUID(0xAA00)
dataUUID = _TI_UUID(0xAA01)
ctrlUUID = _TI_UUID(0xAA02)
zeroC = 273.15 # Kelvin
tRef = 298.15
Apoly = [1.0, 1.75e-3, -1.678e-5]
Bpoly = [-2.94e-5, -5.7e-7, 4.63e-9]
Cpoly = [0.0, 1.0, 13.4]
def __init__(self, periph):
SensorBase.__init__(self, periph)
self.S0 = 6.4e-14
def read(self):
'''Returns (ambient_temp, target_temp) in degC'''
# See http://processors.wiki.ti.com/index.php/SensorTag_User_Guide#IR_Temperature_Sensor
(rawVobj, rawTamb) = struct.unpack('<hh', self.data.read())
tAmb = rawTamb / 128.0
Vobj = 1.5625e-7 * rawVobj
tDie = tAmb + self.zeroC
S = self.S0 * calcPoly(self.Apoly, tDie-self.tRef)
Vos = calcPoly(self.Bpoly, tDie-self.tRef)
fObj = calcPoly(self.Cpoly, Vobj-Vos)
tObj = math.pow( math.pow(tDie,4.0) + (fObj/S), 0.25 )
return (tAmb, tObj - self.zeroC)
class AccelerometerSensor(SensorBase):
svcUUID = _TI_UUID(0xAA10)
dataUUID = _TI_UUID(0xAA11)
ctrlUUID = _TI_UUID(0xAA12)
def __init__(self, periph):
SensorBase.__init__(self, periph)
def read(self):
'''Returns (x_accel, y_accel, z_accel) in units of g'''
x_y_z = struct.unpack('bbb', self.data.read())
return tuple([ (val/64.0) for val in x_y_z ])
class HumiditySensor(SensorBase):
svcUUID = _TI_UUID(0xAA20)
dataUUID = _TI_UUID(0xAA21)
ctrlUUID = _TI_UUID(0xAA22)
def __init__(self, periph):
SensorBase.__init__(self, periph)
def read(self):
'''Returns (ambient_temp, rel_humidity)'''
(rawT, rawH) = struct.unpack('<HH', self.data.read())
temp = -46.85 + 175.72 * (rawT / 65536.0)
RH = -6.0 + 125.0 * ((rawH & 0xFFFC)/65536.0)
return (temp, RH)
class MagnetometerSensor(SensorBase):
svcUUID = _TI_UUID(0xAA30)
dataUUID = _TI_UUID(0xAA31)
ctrlUUID = _TI_UUID(0xAA32)
def __init__(self, periph):
SensorBase.__init__(self, periph)
def read(self):
'''Returns (x, y, z) in uT units'''
x_y_z = struct.unpack('<hhh', self.data.read())
return tuple([ 1000.0 * (v/32768.0) for v in x_y_z ])
# Revisit - some absolute calibration is needed
class BarometerSensor(SensorBase):
svcUUID = _TI_UUID(0xAA40)
dataUUID = _TI_UUID(0xAA41)
ctrlUUID = _TI_UUID(0xAA42)
calUUID = _TI_UUID(0xAA43)
sensorOn = None
def __init__(self, periph):
SensorBase.__init__(self, periph)
def enable(self):
SensorBase.enable(self)
self.calChr = self.service.getCharacteristics(self.calUUID) [0]
# Read calibration data
self.ctrl.write( chr(0x02), True )
(c1,c2,c3,c4,c5,c6,c7,c8) = struct.unpack("<HHHHhhhh", self.calChr.read())
self.c1_s = c1/float(1 << 24)
self.c2_s = c2/float(1 << 10)
self.sensPoly = [ c3/1.0, c4/float(1 << 17), c5/float(1<<34) ]
self.offsPoly = [ c6*float(1<<14), c7/8.0, c8/float(1<<19) ]
self.ctrl.write( chr(0x01), True )
def read(self):
'''Returns (ambient_temp, pressure_millibars)'''
(rawT, rawP) = struct.unpack('<hH', self.data.read())
temp = (self.c1_s * rawT) + self.c2_s
sens = calcPoly( self.sensPoly, float(rawT) )
offs = calcPoly( self.offsPoly, float(rawT) )
pres = (sens * rawP + offs) / (100.0 * float(1<<14))
return (temp,pres)
class GyroscopeSensor(SensorBase):
svcUUID = _TI_UUID(0xAA50)
dataUUID = _TI_UUID(0xAA51)
ctrlUUID = _TI_UUID(0xAA52)
sensorOn = chr(0x07)
def __init__(self, periph):
SensorBase.__init__(self, periph)
def read(self):
'''Returns (x,y,z) rate in deg/sec'''
x_y_z = struct.unpack('<hhh', self.data.read())
return tuple([ 250.0 * (v/32768.0) for v in x_y_z ])
#class KeypressSensor(SensorBase):
# TODO: only sends notifications, you can't poll it
# svcUUID = UUID(0xFFE0)
# write 0100 to 0x60
# get notifications on 5F
class SensorTag(Peripheral):
def __init__(self,addr):
Peripheral.__init__(self,addr)
self.discoverServices()
self.IRtemperature = IRTemperatureSensor(self)
self.accelerometer = AccelerometerSensor(self)
self.humidity = HumiditySensor(self)
self.magnetometer = MagnetometerSensor(self)
self.barometer = BarometerSensor(self)
self.gyroscope = GyroscopeSensor(self)
# self.keypress = KeypressSensor(self)
if __name__ == "__main__":
import time
def quickTest(sensor):
sensor.enable()
for i in range(10):
print "Result", sensor.read()
time.sleep(1.0)
sensor.disable()
tag = SensorTag("34:B1:F7:D5:01:3B")
sensors = [tag.IRtemperature, tag.humidity, tag.barometer, tag.magnetometer]
[ s.enable() for s in sensors ]
tempodbclient = Client(API_KEY, API_SECRET)
while True:
timestamp = time.time()
timestring = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
[ s.enable() for s in sensors ]
ir, hum, baro, mag = [ s.read() for s in sensors ]
time.sleep(1.1)
ir, hum, baro, mag = [ s.read() for s in sensors ]
[ s.disable() for s in sensors ]
print timestring, " IR", ir, "hum", hum, "baro", baro, "mag", mag
date = datetime.datetime.now()
# 1: Pressure
# correct pressure for height above sea level
# see http://de.wikipedia.org/wiki/Barometrische_H%C3%B6henformel#Reduktion_auf_Meeresh.C3.B6he
# baro[0] is the current temperature of the sensor
# baro[1] is the absolute atmospheric pressure
# p0 is the corrected atmospheric pressure adjusted to HEIGHT
temperature = baro[0]
temperature = 6
efactor=5.6402 * (-0.0916 + math.exp(0.06 * temperature))
xfactor = (9.80665 / (287.05 * ((273.15 + temperature) + .12 * efactor + 0.0065 * (HEIGHT/2)))) * HEIGHT
p0 = baro[1] * math.exp(xfactor)
# upload to tempoDB
data = []
data.append(DataPoint(date, p0))
tmp=tempodbclient.write_key(SERIES_KEY1, data)
# 2. Temperature
data = []
data.append(DataPoint(date, temperature))
tmp=tempodbclient.write_key(SERIES_KEY2, data)
print tmp
sys.stdout.flush()
# wait 5minutes
time.sleep(300.0)
tag.disconnect()
del tag