import time import dht from machine import Pin SENSOR_READ_PERIOD = const(2) class Sensor: last_read_time: int last_result: tuple[int, int] sensor: dht.DHT11 def __init__(self, sensor_data_pin: Pin) -> None: self.sensor = dht.DHT11(sensor_data_pin) self.last_read_time = time.time() self.last_result = (0, 0) def __should_read_data(self) -> bool: return (time.time() - self.last_read_time) > SENSOR_READ_PERIOD def read(self) -> tuple[int, int]: if not self.__should_read_data(): print("should not read data") return self.last_result sensor = self.sensor try: sensor.measure() result = (sensor.temperature(), sensor.humidity()) self.last_result = result except OSError as e: print("err reading sensor", e) return self.last_result return result