-
Notifications
You must be signed in to change notification settings - Fork 69
/
HTU21D.py
executable file
·89 lines (72 loc) · 2.65 KB
/
HTU21D.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
#!/usr/bin/python3
import struct
import array
import time
import i2c_base
HTU21D_ADDR = 0x40
CMD_READ_TEMP_HOLD = b"\xE3"
CMD_READ_HUM_HOLD = b"\xE5"
CMD_READ_TEMP_NOHOLD = b"\xF3"
CMD_READ_HUM_NOHOLD = b"\xF5"
CMD_WRITE_USER_REG = b"\xE6"
CMD_READ_USER_REG = b"\xE7"
CMD_SOFT_RESET = b"\xFE"
class HTU21D(object):
def __init__(self):
self.dev = i2c_base.i2c(HTU21D_ADDR, 1) # HTU21D 0x40, bus 1
self.dev.write(CMD_SOFT_RESET) # Soft reset
time.sleep(.1)
def ctemp(self, sensor_temp):
t_sensor_temp = sensor_temp / 65536.0
return -46.85 + (175.72 * t_sensor_temp)
def chumid(self, sensor_humid):
t_sensor_humid = sensor_humid / 65536.0
return -6.0 + (125.0 * t_sensor_humid)
def temp_coefficient(self, rh_actual, temp_actual, coefficient=-0.15):
return rh_actual + (25 - temp_actual) * coefficient
def crc8check(self, value):
# Ported from Sparkfun Arduino HTU21D Library:
# https://github.com/sparkfun/HTU21D_Breakout
remainder = ((value[0] << 8) + value[1]) << 8
remainder |= value[2]
# POLYNOMIAL = 0x0131 = x^8 + x^5 + x^4 + 1 divisor =
# 0x988000 is the 0x0131 polynomial shifted to farthest
# left of three bytes
divisor = 0x988000
for i in range(0, 16):
if(remainder & 1 << (23 - i)):
remainder ^= divisor
divisor = divisor >> 1
if remainder == 0:
return True
else:
return False
def read_temperature(self):
self.dev.write(CMD_READ_TEMP_NOHOLD) # Measure temp
time.sleep(.1)
data = self.dev.read(3)
buf = array.array('B', data)
if self.crc8check(buf):
temp = (buf[0] << 8 | buf[1]) & 0xFFFC
return self.ctemp(temp)
else:
return -255
def read_humidity(self):
temp_actual = self.read_temperature() # For temperature coefficient compensation
self.dev.write(CMD_READ_HUM_NOHOLD) # Measure humidity
time.sleep(.1)
data = self.dev.read(3)
buf = array.array('B', data)
if self.crc8check(buf):
humid = (buf[0] << 8 | buf[1]) & 0xFFFC
rh_actual = self.chumid(humid)
rh_final = self.temp_coefficient(rh_actual, temp_actual)
rh_final = 100.0 if rh_final > 100 else rh_final # Clamp > 100
rh_final = 0.0 if rh_final < 0 else rh_final # Clamp < 0
return rh_final
else:
return -255
if __name__ == "__main__":
obj = HTU21D()
print("Temp: %s C" % obj.read_temperature())
print("Humid: %s %% rH" % obj.read_humidity())