-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaht10.py
58 lines (50 loc) · 2.24 KB
/
aht10.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
import time, math
AHT10_ADDRESS = 0x38
AHT10_READ_DELAY_MS = 75
AHT_TEMPERATURE_CONST = 200
AHT_TEMPERATURE_OFFSET = 50
KILOBYTE_CONST = 1048576
CMD_INITIALIZE = bytearray([0xE1, 0x08, 0x00])
CMD_MEASURE = bytearray([0xAC, 0x33, 0x00])
FARENHEIT_MULTIPLIER = 9/5
FARENHEIT_OFFSET = 32
class AHT10:
def __init__(self, i2c, mode=0, address=AHT10_ADDRESS):
if i2c is None:
raise ValueError('I2C object required.')
if not mode in [0, 1]:
raise ValueError('Mode must be either 0 for Celsius or 1 Farenheit')
self.i2c = i2c
self.address = address
self.i2c.writeto(address, CMD_INITIALIZE)
self.readings_raw = bytearray(8)
self.results_parsed = [0, 0]
self.mode = mode # 0 for Celsius, 1 for Farenheit
def read_raw(self):
self.i2c.writeto(self.address, CMD_MEASURE)
time.sleep_ms(AHT10_READ_DELAY_MS) # type: ignore
self.readings_raw = self.i2c.readfrom(AHT10_ADDRESS, 6)
self.results_parsed[0] = self.readings_raw[1] << 12 | self.readings_raw[2] << 4 | self.readings_raw[3] >> 4
self.results_parsed[1] = (self.readings_raw[3] & 0x0F) << 16 | self.readings_raw[4] << 8 | self.readings_raw[5]
def humidity(self):
self.read_raw()
return (self.results_parsed[0] / KILOBYTE_CONST) * 100
def temperature(self):
self.read_raw()
if self.mode is 0:
return (self.results_parsed[1] / KILOBYTE_CONST) * AHT_TEMPERATURE_CONST - AHT_TEMPERATURE_OFFSET
else:
return ((self.results_parsed[1] / KILOBYTE_CONST) * AHT_TEMPERATURE_CONST - AHT_TEMPERATURE_OFFSET) * FARENHEIT_MULTIPLIER + FARENHEIT_OFFSET
def dew_point(self):
t = (self.results_parsed[1] / KILOBYTE_CONST) * AHT_TEMPERATURE_CONST - AHT_TEMPERATURE_OFFSET
h = self.humidity()
A = 17.27
B = 237.7
alpha = ((A * t) / (B + t)) + math.log(h / 100.0)
return (B * alpha) / (A - alpha)
def set_mode(self, mode):
if not mode in [0, 1]:
raise ValueError('Mode must be either 0 for Celsius or 1 Farenheit')
self.mode = mode
def print(self):
print("Temperature: " + str(self.temperature()) + ("C","F")[self.mode] + ", Humidity: " + str(self.humidity()))