Compare commits
No commits in common. "afedf43bdf2e3359290f69a36242ac7ec09c2c88" and "f8cb99aa08dd26583588de206c2774a1a0b8e2bf" have entirely different histories.
afedf43bdf
...
f8cb99aa08
46
dht.py
46
dht.py
@ -27,9 +27,6 @@ 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)
|
|
||||||
self.__hum_monitor__ = gradient_monitor(5)
|
|
||||||
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:
|
||||||
@ -40,19 +37,13 @@ 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:
|
||||||
# check data
|
logger.debug('DHT-Communication: Successfully: %s', repr(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]):
|
self.__monitor__.process(data)
|
||||||
#
|
if self.__data_callback__ is not None:
|
||||||
logger.debug('DHT-Communication: Successfully: %s', repr(data))
|
self.__data_callback__(**data)
|
||||||
self.__monitor__.process(data)
|
if self.__monitor__.status():
|
||||||
if self.__data_callback__ is not None:
|
logger.warning("DHT measurement stopped caused by too many identical values!")
|
||||||
self.__data_callback__(**data)
|
self.close()
|
||||||
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):
|
||||||
@ -69,7 +60,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.debug('DHT-Communication: ' + error.args[0])
|
logger.info('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()
|
||||||
@ -82,25 +73,6 @@ 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):
|
|
||||||
if self.__last_value__ is not None and self.__last_time__ is not None:
|
|
||||||
# Valid last value exists
|
|
||||||
gradient = abs((value - self.__last_value__) / (time - self.__last_time__))
|
|
||||||
if gradient > self.__max_gradient__:
|
|
||||||
return False
|
|
||||||
|
|
||||||
self.__last_value__ = value
|
|
||||||
self.__last_time__ = time
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
@ -125,6 +97,8 @@ 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