-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdev_i2c_Sensor_BME280.py
499 lines (395 loc) · 22.2 KB
/
gdev_i2c_Sensor_BME280.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#! /usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
I2C module BME280 - for Temperature, Pressure, Humidity
"""
###############################################################################
# This file is part of GeigerLog.
#
# GeigerLog is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GeigerLog is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GeigerLog. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
__author__ = "ullix"
__copyright__ = "Copyright 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024"
__credits__ = [""]
__license__ = "GPL3"
# Barometric pressure: (use for altitude correction)
# Sea level standard pressure: 1013.25 hPa
# Barometric formula: p = p0 * exp(- H / H0), with H0 =7990m
# Example: p @ Altitude 85m: p= 1013.25 * exp(-85/7990) = 1013.25 * 0,98942 = 1002,53
# NOTE: on Sensor Module Bluedot:
# https://www.bluedot.space/bme280-tsl2591/
# Vendor Reference: BME280 + TSL2591, ASIN: B0795WWXX8
# TSL2591 @ address 0x29
# "Please note that the BME280 is hardwired to use the I2C Address 0x77. The
# alternative Address (0x76) is not available! "
# =============================================================================
# comparison of results obtained with the different dongles
# all calibration points agree
# Calibration Data for the BlueDot module BME280 + TSL2591
# validation: data identical between the 2 dongles
#
# via ELV dongle
# ELV BME280 TX 11:04:16 [ ] [ 17] get cal1 == b'S EE 88 S EF 18 P'
# ELV RX 11:04:16 [ 24] [ 72] == b'BE 6E 9A 69 32 00 77 92 CE D6 D0 0B 00 22 A4 FF F9 FF AC 26 0A D8 BD 10 '
# ELV BME280 TX 11:04:16 [ ] [ 17] get cal2 == b'S EE A1 S EF 01 P'
# ELV RX 11:04:16 [ 1] [ 3] == b'4B '
# ELV BME280 TX 11:04:16 [ ] [ 17] get cal3 == b'S EE E1 S EF 07 P'
# ELV RX 11:04:16 [ 7] [ 21] == b'6B 01 00 13 2D 03 1E '
# via IOW dongle
# IOW BME280 TX 11:57:37 [ 2] [ 8] get cal1 == 02 C2 EE 88 00 00 00 00
# IOW BME280 RX 11:57:37 [ 24] [ 8] == 02 02 00 00 00 00 00 00 ACK
# IOW BME280 tx 11:57:37 [ 24] [ 8] == 03 18 EF 00 00 00 00 00
# IOW BME280 RX 11:57:37 [ 24] [ 8] == 03 06 BE 6E 9A 69 32 00 ok, Bytes received: 6
# IOW BME280 RX 11:57:37 [ 24] [ 8] == 03 06 77 92 CE D6 D0 0B ok, Bytes received: 12
# IOW BME280 RX 11:57:37 [ 24] [ 8] == 03 06 00 22 A4 FF F9 FF ok, Bytes received: 18
# IOW BME280 RX 11:57:37 [ 24] [ 8] == 03 06 AC 26 0A D8 BD 10 ok, Bytes received: 24
# IOW BME280 RX 11:57:37 [ 24] [ 24] Final == BE 6E 9A 69 32 00 77 92 CE D6 D0 0B 00 22 A4 FF F9 FF AC 26 0A D8 BD 10
# IOW BME280 TX 11:57:37 [ 2] [ 8] get cal2 == 02 C2 EE A1 00 00 00 00
# IOW BME280 RX 11:57:37 [ 1] [ 8] == 02 02 00 00 00 00 00 00 ACK
# IOW BME280 tx 11:57:37 [ 1] [ 8] == 03 01 EF 00 00 00 00 00
# IOW BME280 RX 11:57:37 [ 1] [ 8] == 03 01 4B 00 00 00 00 00 ok, Bytes received: 6
# IOW BME280 RX 11:57:37 [ 1] [ 1] Final == 4B
# IOW BME280 TX 11:57:37 [ 2] [ 8] get cal3 == 02 C2 EE E1 00 00 00 00
# IOW BME280 RX 11:57:37 [ 7] [ 8] == 02 02 00 00 00 00 00 00 ACK
# IOW BME280 tx 11:57:37 [ 7] [ 8] == 03 07 EF 00 00 00 00 00
# IOW BME280 RX 11:57:37 [ 7] [ 8] == 03 06 6B 01 00 13 2D 03 ok, Bytes received: 6
# IOW BME280 RX 11:57:37 [ 7] [ 8] == 03 01 1E 01 00 13 2D 03 ok, Bytes received: 12
# IOW BME280 RX 11:57:37 [ 7] [ 7] Final == 6B 01 00 13 2D 03 1E
# ISS BME280 i1 14:40:32 [ 24] [ 4] get cal1 == b'U\xef\x88\x18' == 55 EF 88 18
# ISS RX 14:40:32 [ 24] [ 24] == b'\xben\x9ai2\x00w\x92\xce\xd6\xd0\x0b\x00"\xa4\xff\xf9\xff\xac&\n\xd8\xbd\x10'
# == BE 6E 9A 69 32 00 77 92 CE D6 D0 0B 00 22 A4 FF F9 FF AC 26 0A D8 BD 10
# ISS BME280 i1 14:40:32 [ 1] [ 4] get cal2 == b'U\xef\xa1\x01' == 55 EF A1 01
# ISS RX 14:40:32 [ 1] [ 1] == b'K' == 4B
# ISS BME280 i1 14:40:32 [ 7] [ 4] get cal3 == b'U\xef\xe1\x07' == 55 EF E1 07
# ISS RX 14:40:32 [ 7] [ 7] == b'k\x01\x00\x13-\x03\x1e' == 6B 01 00 13 2D 03 1E
# =============================================================================
# Calling problems with ISS dongle due to non-standard way of reading from device
# preparations
#activating BME280 on ELVdongle +++++++++++++++++++++++++++++++++++++++++++
#ELV BME280 TX 14:12:42 [ 9] [ 9] get ID == b'S EE D0 P'
#ELV iR 14:12:42 [ 9] [ 9] == b'S EF 01 P'
#ELV BME280 RX 14:12:42 [ 1] [ 3] == b'60 '
# Found Sensor BME280
#activating BME280 on IOW24-DG +++++++++++++++++++++++++++++++++++++++++++
#IOW BME280 TX 14:04:43 [ 2] [ 8] get ID == 02 C2 EE D0 00 00 00 00
#IOW RX 14:04:43 [ 1] [ 8] == 02 02 00 00 00 00 00 00 ACK
#IOW iR 14:04:43 [ 1] [ 8] == 03 01 EF 00 00 00 00 00
#IOW RX 14:04:43 [ 1] [ 8] == 03 01 60 00 00 00 00 00 ok, Bytes received: 6
# Answer: == 60
# Found Sensor BME280
#activating BME280 on ISSdongle +++++++++++++++++++++++++++++++++++++++++++
# the I2C official way, NOT working !!!
#ISS BME280 TX 15:01:05 [ 3] [ 3] get ID == b'U\xee\xd0' == 55 EE D0 # send 2 bytes, addr ee + D0
#ISS iR 15:01:05 [ 1] [ 3] == b'U\xef\x01' == 55 EF 01 # prep for reading 1 byte from addr EF
#ISS RX 15:01:06 [ 1] [ 0] == b'' == # Failure, no bytes returned
#
# with ISS specific workaraound
#ISS BME280 TX 14:15:07 [ 3] [ 3] get ID == b'U\xee\xd0' == 55 EE D0 # send 2 bytes, addr ee + D0
#ISS BME280 i1 14:15:07 [ 1] [ 4] get ID == b'U\xef\xd0\x01' == 55 EF D0 01 # prep for reading 1 byte from addr EF, but RESEND the register D0
#ISS RX 14:15:07 [ 1] [ 1] == b'`' == 60 # success
# Found Sensor BME280
# when not one but two registered need to be addressed, the ELV and IOW work unmodifyied
# but the ISS requires that a different command be used (56 instead of 55)!
# the ISS has no option for 3 byte commands, while the ELV and IOW would work unmodified
# using ELV dongle
#ELV BME280 TX 10:24:11 [ 12] [ 12] ctrl_meas == b'S EE F4 D6 P' # send 3 bytes, addr ee + F4 D6
#ELV iR 10:24:11 [ 9] [ 9] == b'S EF 01 P' # prep for reading 1 byte from addr EF
#ELV RX 10:24:11 [ 1] [ 3] == b'D6 ' # success
# using ISS dongle
#ISS BME280 TX 10:31:53 [ 4] [ 4] ctrl_meas == b'U\xee\xf4\xd6' == 55 EE F4 D6 # send 3 bytes, addr ee + F4 D6
#ISS iR 10:31:53 [ 1] [ 3] == b'U\xef\x01' == 55 EF 01 # prep for reading 1 byte from addr EF
#ISS RX 10:31:53 [ 1] [ 0] == b'' == # Failure, no bytes returned
#ISS BME280 TX 10:27:16 [ 4] [ 4] ctrl_meas == b'U\xee\xf4\xd6' == 55 EE F4 D6 # send 3 bytes, addr ee + F4 D6
#ISS ii 10:27:16 [ 1] [ 5] == b'U\xef\xf4\xd6\x01' == 55 EF F4 D6 01 # prep for reading 1 byte from addr EF, resending F4 D6
#ISS RX 10:27:16 [ 1] [ 0] == b'' == # Failure, no bytes returned
#ISS BME280 TX 10:47:54 [ 4] [ 4] ctrl_meas == b'U\xee\xf4\xd6' == 55 EE F4 D6 # send 3 bytes, addr ee + F4 D6
#ISS ii 10:47:54 [ 1] [ 5] == b'V\xef\xf4\xd6\x01' == 56 EF F4 D6 01 # using CMD=56 (not 55): prep for reading 1 byte from addr EF, resending F4 D6
#ISS RX 10:47:54 [ 1] [ 1] == b'\xd6' == D6 # success
from gsup_utils import *
class SensorBME280:
"""Code for the BME280 sensors"""
addr = 0x76 # addr options: 0x76, 0x77
id = 0x60
name = "BME280"
handle = g.I2CDongle # default for use by 'I2C' device; RaspiI2C defines its own
def __init__(self, addr, I2Chandle=None):
"""Init SensorBME280 class"""
self.addr = addr
if I2Chandle is not None: self.handle = I2Chandle
def SensorInit(self):
"""Reset, check ID, set reg hum, get calibration, trigger measurement"""
defname = "SensorInit: " + self.name + ": "
# dprint(defname)
setIndent(1)
### check for presence of an I2C device at address
if self.handle.DongleIsSensorPresent(self.addr):
# device found
gdprint(defname, "Found an I2C device at address 0x{:02X}".format(self.addr))
else:
# no device found
setIndent(0)
return False, "Did not find any I2C device at address 0x{:02X}".format(self.addr)
# check ID
tmsg = "checkID"
register = 0xD0
readbytes = 1
data = []
answ = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
if len(answ) == readbytes and answ[0] == self.id:
dmsg = "sensor {:8s} at address 0x{:02X} with ID 0x{:02x}".format(self.name, self.addr, self.id)
# response = (True, dmsg)
gdprint(defname, "Sensor has expected ID: 0x{:02X}".format(self.id))
else:
setIndent(0)
return (False, "Failure - Did find sensor, but ID: '{}' is not as expected: 0x{:02X}".format(answ, self.id))
# soft reset
# does NOT give a return!
tmsg = "Soft Reset"
register = 0xE0
readbytes = 0
data = [0xB6]
wrt = self.handle.DongleWriteReg (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
# set ctrl-hum
tmsg = "Ctrl_hum"
register = 0xf2
readbytes = 1
# data = [0x05] # oversampling ×16
# data = [0x01] # oversampling ×1
# data = [0b00000010] # oversampling ×2
data = [0b00000011] # oversampling ×4
answ = self.handle.DongleWriteRead(self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
# get Calibration Data calib00...calib25 (0x88 ... 0x9F) 24 values
tmsg = "calib1 24"
register = 0x88
readbytes = 24
data = []
self.cal1 = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
# get Calibration Data calib26...calib41 (0xA1 ) 1 value
tmsg = "calib2 1"
register = 0xA1
readbytes = 1
data = []
self.cal2 = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
# get Calibration Data calib26...calib41 (0xe1 ... 0xe7) 7 values
tmsg = "calib3 7"
register = 0xe1
readbytes = 7
data = []
self.cal3 = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
# Full reset
gdprint(defname, self.SensorReset())
setIndent(0)
return (True, dmsg)
def getStatus(self):
"""get Status of BME280"""
tmsg = "getStatus"
register = 0xF3
readbytes = 1
data = []
answ = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
return answ
def SensorgetValues(self):
""" get one measurement of Temp, Press, Humid"""
# trigger measurement with: ctrl_meas
# makes one measurement, then waits for next trigger due to forced mode
# 0b 101 101 10 = D6 = P oversampling * 16, T oversampling * 16, forced mode
#
# max measurement time: BOSCH: t measure,max = 1.25 + [2.3 ⋅ 1] + [2.3 ⋅ 4 + 0.575] + [0] = 13.325 ms
# on dongle ISS: BME280: T:25.050, P:996.343, H:33.799 duration: 2.7 ... 4.1 ms (avg: 3.3) 1.0x
# on dongle ELV: BME280: T:25.790, P:996.186, H:32.296 duration: 18 ... 20 ms (avg:19.0) 5.8x
# on dongle IOW: BME280: T:24.250, P:983.489, H:35.830 duration: 37.4 ... 40.5 ms (avg:39.9) 12.1x
# on dongle FTD: BME280: T:24.250, P:983.489, H:35.830 duration: 64.8 ... 71.7 ms (avg:65.4) 19.8x
# on dongle ISS: 100 kHz BME280: duration: 2.7 ... 4.1 ms (avg: 3.3) 1.0x
# on dongle ISS: 400 kHz BME280: duration: 1.6 ... 3.6 ms (avg: 2.3) 0.7x (1.4x faster)
# on dongle ISS: 1000 kHz BME280:(BME280 as only activated module) duration: 1.6 ... 4.0 ms (avg: 2.4)
# on dongle ISS: 400 kHz BME280:(BME280 as only activated module) duration: 1.7 ... 4.8 ms (avg: 2.9)
# on dongle ISS: 100 kHz BME280:(BME280 as only activated module) duration: 2.9 ... 7.1 ms (avg: 3.8)
start = time.time()
defname = "SensorgetValues: {:10s}: ".format(self.name)
# cdprint(defname)
setIndent(1)
fail = False
nanresp = (g.NAN,) * 3
# trigger measurement
try:
tmsg = "ctrl_meas"
register = 0xf4
readbytes = 1
# data = [0xd6] # = b110 101 10 Oversampling: T=b110 -->16x, P=b101 -->16x, mode=b10 -->Forced (hum also was 16x)
# data = [0x26] # = b001 001 10 Oversampling: T=b001 --> 1x, P=b001 --> 1x, mode=b10 -->Forced Ready: +hum=1x:-->1x:24 ms
# data = [0x4A] # = b010 010 10 Oversampling: T=b010 --> 2x, P=b010 --> 2x, mode=b10 -->Forced Ready: +hum=b10-->2x:24 ms
data = [0x6E] # = b011 011 10 Oversampling: T=b011 --> 4x, P=b011 --> 4x, mode=b10 -->Forced Ready: +hum=b11-->4x:32 ms
answ = self.handle.DongleWriteReg(self.addr, register, readbytes, data, addrScheme=1, msg="")
loop = 0
while loop < 100:
status = self.getStatus()[0]
# rdprint(defname, "loop: {} status: {:08b} Bit3:{} Bit0:{}".format(loop, status, status & 0b1000, status & 0b0001 ))
if status & 0b1000 == 0: break # data are ready
time.sleep(0.005)
loop += 1
except Exception as e:
exceptPrint(e, tmsg)
return nanresp
# if fail or len(answ) != readbytes:
# rdprint(defname + tmsg + " failed, giving up")
# setIndent(0)
# return nanresp
# get raw data
try:
tmsg = "getRawData"
register = 0xf7
readbytes = 8
data = []
answ = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg)
except Exception as e:
exceptPrint(e, "get data F7..FE")
fail = True
# check for valid data
# answ == [128, 0, 0, 128, 0, 0, 128, 0]: does happen in first 1 or even first 2 calls
if fail or len(answ) != readbytes or answ == [128, 0, 0, 128, 0, 0, 128, 0]:
rdprint(defname + tmsg + " failed, giving up")
setIndent(0)
return nanresp
# get raw data and convert
try:
t_raw, p_raw, h_raw = self.BME280getRawData(answ)
t, p, h = readBME280All(self.cal1, self.cal2, self.cal3, p_raw, t_raw, h_raw)
except Exception as e:
exceptPrint(e, "converting raw data")
duration = (time.time() - start) * 1000
# gdprint(defname, "T:{:<6.3f}, P:{:<6.3f}, H:{:<6.3f} dur:{:<0.1f} ms".format(t, p, h, duration))
setIndent(0)
return (t, p, h)
def BME280getRawData(self, rec):
"""calcs raw press, temp, hum"""
msb, lsb, xlsb = rec[0], rec[1], rec[2]
press = (msb << 16 | lsb << 8 | xlsb) >> 4
msb, lsb, xlsb = rec[3], rec[4], rec[5]
temp = (msb << 16 | lsb << 8 | xlsb) >> 4
msb, lsb = rec[6], rec[7]
humid = (msb << 8 | lsb )
return temp, press, humid
def SensorReset(self):
"""Soft Reset the BME280 sensor + Ctrl-hum=5 setting"""
# duration: 6.8 ... 7.4 ms
start = time.time()
defname = "SensorReset: "
# dprint(defname)
# 5.4.2 Register 0xE0 “reset”
# If the value 0xB6 is written to the register, the device is reset using the complete
# power-on-reset procedure. Writing other values than 0xB6 has no effect. The readout
# value is always 0x00.
tmsg = "reset"
register = 0xe0
readbytes = 1
data = [0xb6]
answ = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg) # answ is []
# 5.4.3 Register 0xF2 “ctrl_hum”
# The “ctrl_hum” register sets the humidity data acquisition options of the device. Changes to this
# register only become effective after a write operation to “ctrl_meas”.
# Bit 2, 1, 0 Controls oversampling of humidity data.
# 001 (=0x01) : Humidity oversampling ×1
# 011 (=0x03) : Humidity oversampling ×4
# 101 (=0x05) : Humidity oversampling ×16
tmsg = "ctrl_hum"
register = 0xf2
readbytes = 1
data = [0x03]
answ = self.handle.DongleWriteRead (self.addr, register, readbytes, data, addrScheme=1, msg=tmsg) # answ is [5]
duration = 1000 * (time.time() - start)
return defname + "reset & ctrl_hum =>{}; dur:{:0.1f} ms ".format(data[0], duration)
def SensorGetInfo(self):
info = "{}\n" .format("Temperature, Pressure, Humidity")
info += "- Address: 0x{:02X}\n" .format(self.addr)
info += "- ID: 0x{:02X}\n" .format(self.id)
info += "- Variables: {}\n" .format(", ".join("{}".format(x) for x in g.Sensors["BME280"][5]))
return info.split("\n")
#--------------------------------------
# code adapted from file bme280.py
# Author : Matt Hawkins
# Date : 25/07/2016
# http://www.raspberrypi-spy.co.uk/
#--------------------------------------
from ctypes import c_short
from ctypes import c_byte
from ctypes import c_ubyte
def getShort(data, index):
# return two bytes from data as a signed 16-bit value
return c_short((data[index+1] << 8) + data[index]).value
def getUShort(data, index):
# return two bytes from data as an unsigned 16-bit value
return (data[index+1] << 8) + data[index]
def getChar(data,index):
# return one byte from data as a signed char
result = data[index]
if result > 127:
result -= 256
return result
def getUChar(data,index):
# return one byte from data as an unsigned char
result = data[index] & 0xFF
return result
def readBME280All(cal1, cal2, cal3, pres_raw, t_raw, h_raw):
# Convert byte data to word values
dig_T1 = getUShort(cal1, 0)
dig_T2 = getShort (cal1, 2)
dig_T3 = getShort (cal1, 4)
dig_P1 = getUShort(cal1, 6)
dig_P2 = getShort (cal1, 8)
dig_P3 = getShort (cal1, 10)
dig_P4 = getShort (cal1, 12)
dig_P5 = getShort (cal1, 14)
dig_P6 = getShort (cal1, 16)
dig_P7 = getShort (cal1, 18)
dig_P8 = getShort (cal1, 20)
dig_P9 = getShort (cal1, 22)
dig_H1 = getUChar (cal2, 0)
dig_H2 = getShort (cal3, 0)
dig_H3 = getUChar (cal3, 2)
dig_H4 = getChar (cal3, 3)
dig_H4 = (dig_H4 << 24) >> 20
dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F)
dig_H5 = getChar (cal3, 5)
dig_H5 = (dig_H5 << 24) >> 20
dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F)
dig_H6 = getChar (cal3, 6)
#Refine temperature
var1 = ((((t_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11
var2 = (((((t_raw>>4) - (dig_T1)) * ((t_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14
t_fine = var1+var2
temperature = float(((t_fine * 5) + 128) >> 8);
# Refine pressure and adjust for temperature
var1 = t_fine / 2 - 64000
var2 = var1 * var1 * dig_P6 / 32768
var2 = var2 + var1 * dig_P5 * 2
var2 = var2 / 4 + dig_P4 * 65536
var1 = (dig_P3 * var1 * var1 / 524288 + dig_P2 * var1) / 524288
var1 = (1 + var1 / 32768) * dig_P1
if var1 == 0:
pressure=0
else:
pressure = 1048576 - pres_raw
pressure = ((pressure - var2 / 4096.0) * 6250) / var1
var1 = dig_P9 * pressure * pressure / 2147483648
var2 = pressure * dig_P8 / 32768
pressure = pressure + (var1 + var2 + dig_P7) / 16
# Refine humidity
humidity = t_fine - 76800
humidity = (h_raw - (dig_H4 * 64.0 + dig_H5 / 16384.0 * humidity)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * humidity * (1.0 + dig_H3 / 67108864.0 * humidity)))
humidity = humidity * (1.0 - dig_H1 * humidity / 524288.0)
if humidity > 100:
humidity = 100
elif humidity < 0:
humidity = 0
return temperature/100, pressure/100, humidity