Initial spike filter implemented
This commit is contained in:
parent
f8cb99aa08
commit
95f340418f
45
dht.py
45
dht.py
@ -27,6 +27,9 @@ class dht_22(background_task):
|
|||||||
|
|
||||||
def __init__(self, gpio, data_callback=None):
|
def __init__(self, gpio, data_callback=None):
|
||||||
self.__data_callback__ = data_callback
|
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)
|
self.__monitor__ = dht_22_monitor(300)
|
||||||
# Initial the dht device, with data pin connected to:
|
# Initial the dht device, with data pin connected to:
|
||||||
if not DEBUG:
|
if not DEBUG:
|
||||||
@ -37,13 +40,19 @@ class dht_22(background_task):
|
|||||||
def __run__(self):
|
def __run__(self):
|
||||||
data = self.__dht_data_transmission__()
|
data = self.__dht_data_transmission__()
|
||||||
if data is not None:
|
if data is not None:
|
||||||
logger.debug('DHT-Communication: Successfully: %s', repr(data))
|
# check data
|
||||||
self.__monitor__.process(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]):
|
||||||
if self.__data_callback__ is not None:
|
#
|
||||||
self.__data_callback__(**data)
|
logger.debug('DHT-Communication: Successfully: %s', repr(data))
|
||||||
if self.__monitor__.status():
|
self.__monitor__.process(data)
|
||||||
logger.warning("DHT measurement stopped caused by too many identical values!")
|
if self.__data_callback__ is not None:
|
||||||
self.close()
|
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):
|
def __dht_data_transmission__(self):
|
||||||
@ -60,7 +69,7 @@ class dht_22(background_task):
|
|||||||
time.sleep(self.RUN_SLEEP_TIME)
|
time.sleep(self.RUN_SLEEP_TIME)
|
||||||
except RuntimeError as error:
|
except RuntimeError as error:
|
||||||
# Errors happen fairly often, DHT's are hard to read, just keep going
|
# 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)
|
time.sleep(2.0)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
self.__dht_device__.exit()
|
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):
|
class dht_22_monitor(object):
|
||||||
def __init__(self, max_const_treshold=250):
|
def __init__(self, max_const_treshold=250):
|
||||||
self.__max_const_treshold__ = max_const_treshold
|
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__:
|
if self.__current_const_measurements__ > self.__max_const_measurements__:
|
||||||
self.__max_const_measurements__ = self.__current_const_measurements__
|
self.__max_const_measurements__ = self.__current_const_measurements__
|
||||||
else:
|
else:
|
||||||
if self.__current_const_measurements__ >= self.__max_const_measurements__:
|
|
||||||
logger.warning('%d identical data measurements identified!', self.__max_const_measurements__)
|
|
||||||
self.__init_statevars__()
|
self.__init_statevars__()
|
||||||
if self.__max_const_measurements__ > self.__max_const_treshold__:
|
if self.__max_const_measurements__ > self.__max_const_treshold__:
|
||||||
if not self.__fail__:
|
if not self.__fail__:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user