From 9072b24e55d080aec17a503524085b0c7bda2c66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Balzarotti?= Date: Tue, 14 Mar 2023 22:43:16 +0100 Subject: [PATCH] add AHT40 sensor --- sensors.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/sensors.py b/sensors.py index 4c79509..87e3d05 100644 --- a/sensors.py +++ b/sensors.py @@ -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),