Browse Source

Adaption to new logging

spike_filter
Dirk Alders 3 years ago
parent
commit
f8cb99aa08
3 changed files with 75 additions and 7 deletions
  1. 6
    2
      __init__.py
  2. 10
    1
      bmp.py
  3. 59
    4
      dht.py

+ 6
- 2
__init__.py View File

4
 
4
 
5
 DEBUG = False
5
 DEBUG = False
6
 
6
 
7
-logger_name = 'RPI_ENVSENS'
8
-logger = logging.getLogger(logger_name)
7
+try:
8
+    from config import APP_NAME as ROOT_LOGGER_NAME
9
+except ImportError:
10
+    ROOT_LOGGER_NAME = 'root'
11
+
12
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
9
 
13
 
10
 
14
 
11
 class background_task(object):
15
 class background_task(object):

+ 10
- 1
bmp.py View File

1
+import logging
1
 import time
2
 import time
2
 
3
 
3
-from . import logger, background_task
4
+try:
5
+    from config import APP_NAME as ROOT_LOGGER_NAME
6
+except ImportError:
7
+    ROOT_LOGGER_NAME = 'root'
8
+
9
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
10
+
11
+from . import background_task
12
+
4
 try:
13
 try:
5
     import smbus
14
     import smbus
6
 except ImportError:
15
 except ImportError:

+ 59
- 4
dht.py View File

1
+import logging
1
 import time
2
 import time
2
 
3
 
3
-from . import logger, background_task
4
+try:
5
+    from config import APP_NAME as ROOT_LOGGER_NAME
6
+except ImportError:
7
+    ROOT_LOGGER_NAME = 'root'
8
+
9
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
10
+
11
+from . import background_task
12
+
4
 try:
13
 try:
5
     import adafruit_dht
14
     import adafruit_dht
6
 except ImportError:
15
 except ImportError:
18
 
27
 
19
     def __init__(self, gpio, data_callback=None):
28
     def __init__(self, gpio, data_callback=None):
20
         self.__data_callback__ = data_callback
29
         self.__data_callback__ = data_callback
30
+        self.__monitor__ = dht_22_monitor(300)
21
         # Initial the dht device, with data pin connected to:
31
         # Initial the dht device, with data pin connected to:
22
         if not DEBUG:
32
         if not DEBUG:
23
-            self.__dht_device__ = adafruit_dht.DHT22(gpio)
33
+            self.__dht_device__ = adafruit_dht.DHT22(gpio, use_pulseio=False)
24
         # Initialise background_task
34
         # Initialise background_task
25
         background_task.__init__(self)
35
         background_task.__init__(self)
26
 
36
 
28
         data = self.__dht_data_transmission__()
38
         data = self.__dht_data_transmission__()
29
         if data is not None:
39
         if data is not None:
30
             logger.debug('DHT-Communication: Successfully: %s', repr(data))
40
             logger.debug('DHT-Communication: Successfully: %s', repr(data))
41
+            self.__monitor__.process(data)
31
             if self.__data_callback__ is not None:
42
             if self.__data_callback__ is not None:
32
                 self.__data_callback__(**data)
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()
47
+
33
 
48
 
34
     def __dht_data_transmission__(self):
49
     def __dht_data_transmission__(self):
35
         if not DEBUG:
50
         if not DEBUG:
42
                     data[self.KEY_TIME] = int(time.time())
57
                     data[self.KEY_TIME] = int(time.time())
43
                     if data[self.KEY_TEMPERATURE] is not None and data[self.KEY_HUMIDITY] is not None:
58
                     if data[self.KEY_TEMPERATURE] is not None and data[self.KEY_HUMIDITY] is not None:
44
                         return data
59
                         return data
45
-                    time.sleep(2.0)
60
+                    time.sleep(self.RUN_SLEEP_TIME)
46
                 except RuntimeError as error:
61
                 except RuntimeError as error:
47
                     # Errors happen fairly often, DHT's are hard to read, just keep going
62
                     # Errors happen fairly often, DHT's are hard to read, just keep going
48
                     logger.info('DHT-Communication: ' + error.args[0])
63
                     logger.info('DHT-Communication: ' + error.args[0])
49
-                    time.sleep(0.1)
64
+                    time.sleep(2.0)
50
                 except Exception as error:
65
                 except Exception as error:
51
                     self.__dht_device__.exit()
66
                     self.__dht_device__.exit()
52
                     raise error
67
                     raise error
56
                 self.KEY_HUMIDITY: 1.7,
71
                 self.KEY_HUMIDITY: 1.7,
57
                 self.KEY_TIME: int(time.time()),
72
                 self.KEY_TIME: int(time.time()),
58
             }
73
             }
74
+
75
+
76
+class dht_22_monitor(object):
77
+    def __init__(self, max_const_treshold=250):
78
+        self.__max_const_treshold__ = max_const_treshold
79
+        self.__fail__ = False
80
+        self.__max_const_measurements__ = 0
81
+        self.__init_statevars__()
82
+
83
+    def __init_statevars__(self):
84
+        self.__current_const_measurements__ = 0
85
+        self.__last_measurements__ = None
86
+
87
+    def process(self, data):
88
+        if self.__last_measurements__ is None:
89
+            self.__last_measurements__ = data
90
+        else:
91
+            data_is_equal = True
92
+            for key in [dht_22.KEY_HUMIDITY, dht_22.KEY_TEMPERATURE]:
93
+                if data[key] != self.__last_measurements__[key]:
94
+                    data_is_equal = False
95
+            if data_is_equal:
96
+                self.__current_const_measurements__ += 1
97
+                if self.__current_const_measurements__ > self.__max_const_measurements__:
98
+                    self.__max_const_measurements__ = self.__current_const_measurements__
99
+            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__()
103
+        if self.__max_const_measurements__ > self.__max_const_treshold__:
104
+            if not self.__fail__:
105
+                logger.warning("DHT measurement values are suspicious constant!")
106
+            self.__fail__ = True
107
+
108
+    def status(self):
109
+        if self.__fail__:
110
+            self.__fail__ = False
111
+            self.__max_const_measurements__ = 0
112
+            return True
113
+        return False

Loading…
Cancel
Save