Browse Source

Initial spike filter implemented

spike_filter
Dirk Alders 4 years ago
parent
commit
95f340418f
1 changed files with 35 additions and 10 deletions
  1. 35
    10
      dht.py

+ 35
- 10
dht.py View File

27
 
27
 
28
     def __init__(self, gpio, data_callback=None):
28
     def __init__(self, gpio, data_callback=None):
29
         self.__data_callback__ = data_callback
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
         self.__monitor__ = dht_22_monitor(300)
33
         self.__monitor__ = dht_22_monitor(300)
31
         # Initial the dht device, with data pin connected to:
34
         # Initial the dht device, with data pin connected to:
32
         if not DEBUG:
35
         if not DEBUG:
37
     def __run__(self):
40
     def __run__(self):
38
         data = self.__dht_data_transmission__()
41
         data = self.__dht_data_transmission__()
39
         if data is not None:
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
     def __dht_data_transmission__(self):
58
     def __dht_data_transmission__(self):
60
                     time.sleep(self.RUN_SLEEP_TIME)
69
                     time.sleep(self.RUN_SLEEP_TIME)
61
                 except RuntimeError as error:
70
                 except RuntimeError as error:
62
                     # Errors happen fairly often, DHT's are hard to read, just keep going
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
                     time.sleep(2.0)
73
                     time.sleep(2.0)
65
                 except Exception as error:
74
                 except Exception as error:
66
                     self.__dht_device__.exit()
75
                     self.__dht_device__.exit()
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
 class dht_22_monitor(object):
103
 class dht_22_monitor(object):
77
     def __init__(self, max_const_treshold=250):
104
     def __init__(self, max_const_treshold=250):
78
         self.__max_const_treshold__ = max_const_treshold
105
         self.__max_const_treshold__ = max_const_treshold
97
                 if self.__current_const_measurements__ > self.__max_const_measurements__:
124
                 if self.__current_const_measurements__ > self.__max_const_measurements__:
98
                     self.__max_const_measurements__ = self.__current_const_measurements__
125
                     self.__max_const_measurements__ = self.__current_const_measurements__
99
             else:
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
                 self.__init_statevars__()
127
                 self.__init_statevars__()
103
         if self.__max_const_measurements__ > self.__max_const_treshold__:
128
         if self.__max_const_measurements__ > self.__max_const_treshold__:
104
             if not self.__fail__:
129
             if not self.__fail__:

Loading…
Cancel
Save