|
@@ -27,6 +27,9 @@ class dht_22(background_task):
|
27
|
27
|
|
28
|
28
|
def __init__(self, gpio, data_callback=None):
|
29
|
29
|
self.__data_callback__ = data_callback
|
|
30
|
+
|
|
31
|
+ self.__temp_monitor__ = gradient_monitor(.5, 'DHT Temperature')
|
|
32
|
+ self.__hum_monitor__ = gradient_monitor(5, 'DHT Humidity')
|
30
|
33
|
self.__monitor__ = dht_22_monitor(300)
|
31
|
34
|
# Initial the dht device, with data pin connected to:
|
32
|
35
|
if not DEBUG:
|
|
@@ -37,13 +40,19 @@ class dht_22(background_task):
|
37
|
40
|
def __run__(self):
|
38
|
41
|
data = self.__dht_data_transmission__()
|
39
|
42
|
if data is not None:
|
40
|
|
- logger.debug('DHT-Communication: Successfully: %s', repr(data))
|
41
|
|
- self.__monitor__.process(data)
|
42
|
|
- if self.__data_callback__ is not None:
|
43
|
|
- self.__data_callback__(**data)
|
44
|
|
- if self.__monitor__.status():
|
45
|
|
- logger.warning("DHT measurement stopped caused by too many identical values!")
|
46
|
|
- self.close()
|
|
43
|
+ # check data
|
|
44
|
+ 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]):
|
|
45
|
+ #
|
|
46
|
+ logger.debug('DHT-Communication: Successfully: %s', repr(data))
|
|
47
|
+ self.__monitor__.process(data)
|
|
48
|
+ if self.__data_callback__ is not None:
|
|
49
|
+ self.__data_callback__(**data)
|
|
50
|
+ if self.__monitor__.status():
|
|
51
|
+ logger.warning("DHT measurement stopped caused by too many identical values!")
|
|
52
|
+ self.close()
|
|
53
|
+ else:
|
|
54
|
+ logger.warning("DHT-Gradient to high: Ignoring data %s!", repr(data))
|
|
55
|
+
|
47
|
56
|
|
48
|
57
|
|
49
|
58
|
def __dht_data_transmission__(self):
|
|
@@ -60,7 +69,7 @@ class dht_22(background_task):
|
60
|
69
|
time.sleep(self.RUN_SLEEP_TIME)
|
61
|
70
|
except RuntimeError as error:
|
62
|
71
|
# Errors happen fairly often, DHT's are hard to read, just keep going
|
63
|
|
- logger.info('DHT-Communication: ' + error.args[0])
|
|
72
|
+ logger.debug('DHT-Communication: ' + error.args[0])
|
64
|
73
|
time.sleep(2.0)
|
65
|
74
|
except Exception as error:
|
66
|
75
|
self.__dht_device__.exit()
|
|
@@ -73,6 +82,24 @@ class dht_22(background_task):
|
73
|
82
|
}
|
74
|
83
|
|
75
|
84
|
|
|
85
|
+class gradient_monitor(object):
|
|
86
|
+ def __init__(self, max_gradient):
|
|
87
|
+ self.__max_gradient__ = max_gradient
|
|
88
|
+ #
|
|
89
|
+ self.__last_value__ = None
|
|
90
|
+ self.__last_time__ = None
|
|
91
|
+
|
|
92
|
+ def process(self, value, time):
|
|
93
|
+ rv = True
|
|
94
|
+ if self.__last_value__ is not None and self.__last_time__ is not None:
|
|
95
|
+ gradient = abs((value - self.__last_value__) / (time - self.__last_time__))
|
|
96
|
+ if gradient > self.__max_gradient__:
|
|
97
|
+ rv = False
|
|
98
|
+ self.__last_value__ = value
|
|
99
|
+ self.__last_time__ = time
|
|
100
|
+ return rv
|
|
101
|
+
|
|
102
|
+
|
76
|
103
|
class dht_22_monitor(object):
|
77
|
104
|
def __init__(self, max_const_treshold=250):
|
78
|
105
|
self.__max_const_treshold__ = max_const_treshold
|
|
@@ -97,8 +124,6 @@ class dht_22_monitor(object):
|
97
|
124
|
if self.__current_const_measurements__ > self.__max_const_measurements__:
|
98
|
125
|
self.__max_const_measurements__ = self.__current_const_measurements__
|
99
|
126
|
else:
|
100
|
|
- if self.__current_const_measurements__ >= self.__max_const_measurements__:
|
101
|
|
- logger.warning('%d identical data measurements identified!', self.__max_const_measurements__)
|
102
|
127
|
self.__init_statevars__()
|
103
|
128
|
if self.__max_const_measurements__ > self.__max_const_treshold__:
|
104
|
129
|
if not self.__fail__:
|