add AHT40 sensor

This commit is contained in:
Nicolò Balzarotti 2023-03-14 22:43:16 +01:00
parent 2c5dd60aa4
commit 9072b24e55
1 changed files with 37 additions and 0 deletions

View File

@ -22,6 +22,13 @@ except:
print("Adafruit SHT4X not available!")
enable_sht = False
try:
import AHT20
enable_aht = True
except:
print("Adafruit SHT4X not available!")
enable_aht = False
# TODO client: add a way to disable live plotting? server: stop
# sending data periodically to everybody!
@ -114,6 +121,9 @@ if enable_sht:
SHT40_DEFAULT = adafruit_sht4x.Mode.NOHEAT_HIGHPRECISION
sht.mode = SHT40_DEFAULT
if enable_aht:
aht = AHT20.AHT20()
class SHT40(Sensor):
def __init__(self, what, every=600):
super().__init__()
@ -160,6 +170,31 @@ class SHT40(Sensor):
return self.store(
time, temperature if self.measure == 'Temperature' else relative_humidity)
class AHT40(Sensor):
def __init__(self, what):
super().__init__()
if what not in ("Temperature", "Humidity"):
print("ERROR: invalid sensor value: ", what)
return
self.measure = what
if not enable_aht:
return
def is_temperature(self):
return self.measure == "Temperature"
def is_humidity(self):
return self.measure == "Humidity"
def read(self):
time = perf_counter()
if not enable_aht:
return (time, None)
return self.store(
time, aht20.get_temperature_crc8() if (
self.measure == 'Temperature'
) else aht20.get_humidity_crc8())
class Sensors():
def __init__(self, history=2621440):
self.starttime = (datetime.now(), perf_counter())
@ -181,6 +216,8 @@ class Sensors():
'T_food': Temperature1W('28-06214252b671'),
'T_ext': SHT40('Temperature'),
'H_ext': SHT40('Humidity'),
'T_ext2': AHT40('Temperature'),
'H_ext2': AHT40('Humidity'),
'heater': GPIOState(22),
'humidifier/heater2': GPIOState(17),
'freezer': GPIOState(14),