diff --git a/dht.py b/dht.py index e95bce2..537f2c3 100644 --- a/dht.py +++ b/dht.py @@ -27,6 +27,9 @@ class dht_22(background_task): def __init__(self, gpio, data_callback=None): self.__data_callback__ = data_callback + + self.__temp_monitor__ = gradient_monitor(.5, 'DHT Temperature') + self.__hum_monitor__ = gradient_monitor(5, 'DHT Humidity') self.__monitor__ = dht_22_monitor(300) # Initial the dht device, with data pin connected to: if not DEBUG: @@ -37,13 +40,19 @@ class dht_22(background_task): def __run__(self): data = self.__dht_data_transmission__() if data is not None: - logger.debug('DHT-Communication: Successfully: %s', repr(data)) - self.__monitor__.process(data) - if self.__data_callback__ is not None: - self.__data_callback__(**data) - if self.__monitor__.status(): - logger.warning("DHT measurement stopped caused by too many identical values!") - self.close() + # check data + if self.__temp_monitor__.process(data[self.KEY_TEMPERATURE], data[self.KEY_TIME]) and self.__hum_monitor__.process(data[self.KEY_HUMIDITY], data[self.KEY_TIME]): + # + logger.debug('DHT-Communication: Successfully: %s', repr(data)) + self.__monitor__.process(data) + if self.__data_callback__ is not None: + self.__data_callback__(**data) + if self.__monitor__.status(): + logger.warning("DHT measurement stopped caused by too many identical values!") + self.close() + else: + logger.warning("DHT-Gradient to high: Ignoring data %s!", repr(data)) + def __dht_data_transmission__(self): @@ -60,7 +69,7 @@ class dht_22(background_task): time.sleep(self.RUN_SLEEP_TIME) except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going - logger.info('DHT-Communication: ' + error.args[0]) + logger.debug('DHT-Communication: ' + error.args[0]) time.sleep(2.0) except Exception as error: self.__dht_device__.exit() @@ -73,6 +82,24 @@ class dht_22(background_task): } +class gradient_monitor(object): + def __init__(self, max_gradient): + self.__max_gradient__ = max_gradient + # + self.__last_value__ = None + self.__last_time__ = None + + def process(self, value, time): + rv = True + if self.__last_value__ is not None and self.__last_time__ is not None: + gradient = abs((value - self.__last_value__) / (time - self.__last_time__)) + if gradient > self.__max_gradient__: + rv = False + self.__last_value__ = value + self.__last_time__ = time + return rv + + class dht_22_monitor(object): def __init__(self, max_const_treshold=250): self.__max_const_treshold__ = max_const_treshold @@ -97,8 +124,6 @@ class dht_22_monitor(object): if self.__current_const_measurements__ > self.__max_const_measurements__: self.__max_const_measurements__ = self.__current_const_measurements__ else: - if self.__current_const_measurements__ >= self.__max_const_measurements__: - logger.warning('%d identical data measurements identified!', self.__max_const_measurements__) self.__init_statevars__() if self.__max_const_measurements__ > self.__max_const_treshold__: if not self.__fail__: