Bladeren bron

Timing and Formatadaptions

master
Dirk Alders 3 jaren geleden
bovenliggende
commit
6ab2629fb1
2 gewijzigde bestanden met toevoegingen van 155 en 155 verwijderingen
  1. 150
    148
      bmp.py
  2. 5
    7
      dht.py

+ 150
- 148
bmp.py Bestand weergeven

@@ -1,148 +1,150 @@
1
-import logging
2
-import time
3
-
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
-
13
-try:
14
-    import smbus
15
-except ImportError:
16
-    logger.warning("Could not import smbus. DEBUG set to True.")
17
-    DEBUG = True
18
-else:
19
-    from . import DEBUG
20
-
21
-
22
-class bmp_180(background_task):
23
-	RUN_SLEEP_TIME = 0.5
24
-	SMBUS_DELAY = 0.5
25
-	MIN_REFRESH_RATE = RUN_SLEEP_TIME + 3 * SMBUS_DELAY
26
-	KEY_TEMPERATURE = 'temperature'
27
-	KEY_PRESSURE = 'pressure'
28
-	KEY_TIME = 'time'
29
-	
30
-	def __init__(self, data_callback=None):
31
-		self.__data_callback__ = data_callback
32
-		# Initialise background_task
33
-		background_task.__init__(self)
34
-	
35
-	def __run__(self):
36
-		data = self.__bmp_data_transmission__()
37
-		if data is not None:
38
-			logger.debug('BMP-Communication: Successfully: %s', repr(data))
39
-			if self.__data_callback__ is not None:
40
-				self.__data_callback__(**data)
41
-	
42
-	def __bmp_data_transmission__(self):
43
-		if not DEBUG:
44
-			rv = {}
45
-			# Get I2C bus
46
-			bus = smbus.SMBus(1)
47
-			
48
-			# BMP180 address, 0x77(119)
49
-			# Read data back from 0xAA(170), 22 bytes
50
-			data = bus.read_i2c_block_data(0x77, 0xAA, 22)
51
-			
52
-			# Convert the data
53
-			AC1 = data[0] * 256 + data[1]
54
-			if AC1 > 32767 :
55
-				AC1 -= 65535
56
-			AC2 = data[2] * 256 + data[3]
57
-			if AC2 > 32767 :
58
-				AC2 -= 65535
59
-			AC3 = data[4] * 256 + data[5]
60
-			if AC3 > 32767 :
61
-				AC3 -= 65535
62
-			AC4 = data[6] * 256 + data[7]
63
-			AC5 = data[8] * 256 + data[9]
64
-			AC6 = data[10] * 256 + data[11]
65
-			B1 = data[12] * 256 + data[13]
66
-			if B1 > 32767 :
67
-				B1 -= 65535
68
-			B2 = data[14] * 256 + data[15]
69
-			if B2 > 32767 :
70
-				B2 -= 65535
71
-			MB = data[16] * 256 + data[17]
72
-			if MB > 32767 :
73
-				MB -= 65535
74
-			MC = data[18] * 256 + data[19]
75
-			if MC > 32767 :
76
-				MC -= 65535
77
-			MD = data[20] * 256 + data[21]
78
-			if MD > 32767 :
79
-				MD -= 65535
80
-			
81
-			time.sleep(self.SMBUS_DELAY)
82
-			
83
-			# BMP180 address, 0x77(119)
84
-			# Select measurement control register, 0xF4(244)
85
-			#		0x2E(46)	Enable temperature measurement
86
-			bus.write_byte_data(0x77, 0xF4, 0x2E)
87
-			
88
-			time.sleep(self.SMBUS_DELAY)
89
-			
90
-			# BMP180 address, 0x77(119)
91
-			# Read data back from 0xF6(246), 2 bytes
92
-			# temp MSB, temp LSB
93
-			data = bus.read_i2c_block_data(0x77, 0xF6, 2)
94
-			
95
-			# Convert the data
96
-			temp = data[0] * 256 + data[1]
97
-			
98
-			# BMP180 address, 0x77(119)
99
-			# Select measurement control register, 0xF4(244)
100
-			#		0x74(116)	Enable pressure measurement, OSS = 1
101
-			bus.write_byte_data(0x77, 0xF4, 0x74)
102
-			
103
-			time.sleep(self.SMBUS_DELAY)
104
-			
105
-			# BMP180 address, 0x77(119)
106
-			# Read data back from 0xF6(246), 3 bytes
107
-			# pres MSB1, pres MSB, pres LSB
108
-			data = bus.read_i2c_block_data(0x77, 0xF6, 3)
109
-			
110
-			rv[self.KEY_TIME] = int(time.time())
111
-		
112
-			# Convert the data
113
-			pres = ((data[0] * 65536) + (data[1] * 256) + data[2]) / 128
114
-			# Callibration for Temperature
115
-			X1 = (temp - AC6) * AC5 / 32768.0
116
-			X2 = (MC * 2048.0) / (X1 + MD)
117
-			B5 = X1 + X2
118
-			rv[self.KEY_TEMPERATURE] = ((B5 + 8.0) / 16.0) / 10.0
119
-			
120
-			# Calibration for Pressure
121
-			B6 = B5 - 4000
122
-			X1 = (B2 * (B6 * B6 / 4096.0)) / 2048.0
123
-			X2 = AC2 * B6 / 2048.0
124
-			X3 = X1 + X2
125
-			B3 = (((AC1 * 4 + X3) * 2) + 2) / 4.0
126
-			X1 = AC3 * B6 / 8192.0
127
-			X2 = (B1 * (B6 * B6 / 2048.0)) / 65536.0
128
-			X3 = ((X1 + X2) + 2) / 4.0
129
-			B4 = AC4 * (X3 + 32768) / 32768.0
130
-			B7 = ((pres - B3) * (25000.0))
131
-			pressure = 0.0
132
-			if B7 < 2147483648:
133
-				pressure = (B7 * 2) / B4
134
-			else :
135
-				pressure = (B7 / B4) * 2
136
-			X1 = (pressure / 256.0) * (pressure / 256.0)
137
-			X1 = (X1 * 3038.0) / 65536.0
138
-			X2 = ((-7357) * pressure) / 65536.0
139
-			rv[self.KEY_PRESSURE] = (pressure + (X1 + X2 + 3791) / 16.0) / 100
140
-		
141
-			return rv
142
-		else:
143
-			time.sleep(3 * self.SMBUS_DELAY)
144
-			return {
145
-				self.KEY_PRESSURE: 1717.,
146
-				self.KEY_TEMPERATURE: 17.17,
147
-				self.KEY_TIME: int(time.time())
148
-			}
1
+import logging
2
+import time
3
+
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
+
13
+try:
14
+    import smbus
15
+except ImportError:
16
+    logger.warning("Could not import smbus. DEBUG set to True.")
17
+    DEBUG = True
18
+else:
19
+    from . import DEBUG
20
+
21
+
22
+class bmp_180(background_task):
23
+    RUN_SLEEP_TIME = 2.0
24
+    SMBUS_DELAY = 0.5
25
+    MIN_REFRESH_RATE = RUN_SLEEP_TIME + 3 * SMBUS_DELAY
26
+    KEY_TEMPERATURE = 'temperature'
27
+    KEY_PRESSURE = 'pressure'
28
+    KEY_TIME = 'time'
29
+
30
+    def __init__(self, data_callback=None):
31
+        self.__data_callback__ = data_callback
32
+        # Initialise background_task
33
+        background_task.__init__(self)
34
+
35
+    def __run__(self):
36
+        data = self.__bmp_data_transmission__()
37
+        if data is not None:
38
+            logger.info('BMP-Communication successful: %s', repr(data))
39
+            if self.__data_callback__ is not None:
40
+                self.__data_callback__(**data)
41
+        else:
42
+            logger.debug('BMP-Communication failed: No data received')
43
+
44
+    def __bmp_data_transmission__(self):
45
+        if not DEBUG:
46
+            rv = {}
47
+            # Get I2C bus
48
+            bus = smbus.SMBus(1)
49
+
50
+            # BMP180 address, 0x77(119)
51
+            # Read data back from 0xAA(170), 22 bytes
52
+            data = bus.read_i2c_block_data(0x77, 0xAA, 22)
53
+
54
+            # Convert the data
55
+            AC1 = data[0] * 256 + data[1]
56
+            if AC1 > 32767 :
57
+                AC1 -= 65535
58
+            AC2 = data[2] * 256 + data[3]
59
+            if AC2 > 32767 :
60
+                AC2 -= 65535
61
+            AC3 = data[4] * 256 + data[5]
62
+            if AC3 > 32767 :
63
+                AC3 -= 65535
64
+            AC4 = data[6] * 256 + data[7]
65
+            AC5 = data[8] * 256 + data[9]
66
+            AC6 = data[10] * 256 + data[11]
67
+            B1 = data[12] * 256 + data[13]
68
+            if B1 > 32767 :
69
+                B1 -= 65535
70
+            B2 = data[14] * 256 + data[15]
71
+            if B2 > 32767 :
72
+                B2 -= 65535
73
+            MB = data[16] * 256 + data[17]
74
+            if MB > 32767 :
75
+                MB -= 65535
76
+            MC = data[18] * 256 + data[19]
77
+            if MC > 32767 :
78
+                MC -= 65535
79
+            MD = data[20] * 256 + data[21]
80
+            if MD > 32767 :
81
+                MD -= 65535
82
+
83
+            time.sleep(self.SMBUS_DELAY)
84
+
85
+            # BMP180 address, 0x77(119)
86
+            # Select measurement control register, 0xF4(244)
87
+            #        0x2E(46)    Enable temperature measurement
88
+            bus.write_byte_data(0x77, 0xF4, 0x2E)
89
+
90
+            time.sleep(self.SMBUS_DELAY)
91
+
92
+            # BMP180 address, 0x77(119)
93
+            # Read data back from 0xF6(246), 2 bytes
94
+            # temp MSB, temp LSB
95
+            data = bus.read_i2c_block_data(0x77, 0xF6, 2)
96
+
97
+            # Convert the data
98
+            temp = data[0] * 256 + data[1]
99
+
100
+            # BMP180 address, 0x77(119)
101
+            # Select measurement control register, 0xF4(244)
102
+            #        0x74(116)    Enable pressure measurement, OSS = 1
103
+            bus.write_byte_data(0x77, 0xF4, 0x74)
104
+
105
+            time.sleep(self.SMBUS_DELAY)
106
+
107
+            # BMP180 address, 0x77(119)
108
+            # Read data back from 0xF6(246), 3 bytes
109
+            # pres MSB1, pres MSB, pres LSB
110
+            data = bus.read_i2c_block_data(0x77, 0xF6, 3)
111
+
112
+            rv[self.KEY_TIME] = int(time.time())
113
+
114
+            # Convert the data
115
+            pres = ((data[0] * 65536) + (data[1] * 256) + data[2]) / 128
116
+            # Callibration for Temperature
117
+            X1 = (temp - AC6) * AC5 / 32768.0
118
+            X2 = (MC * 2048.0) / (X1 + MD)
119
+            B5 = X1 + X2
120
+            rv[self.KEY_TEMPERATURE] = ((B5 + 8.0) / 16.0) / 10.0
121
+
122
+            # Calibration for Pressure
123
+            B6 = B5 - 4000
124
+            X1 = (B2 * (B6 * B6 / 4096.0)) / 2048.0
125
+            X2 = AC2 * B6 / 2048.0
126
+            X3 = X1 + X2
127
+            B3 = (((AC1 * 4 + X3) * 2) + 2) / 4.0
128
+            X1 = AC3 * B6 / 8192.0
129
+            X2 = (B1 * (B6 * B6 / 2048.0)) / 65536.0
130
+            X3 = ((X1 + X2) + 2) / 4.0
131
+            B4 = AC4 * (X3 + 32768) / 32768.0
132
+            B7 = ((pres - B3) * (25000.0))
133
+            pressure = 0.0
134
+            if B7 < 2147483648:
135
+                pressure = (B7 * 2) / B4
136
+            else :
137
+                pressure = (B7 / B4) * 2
138
+            X1 = (pressure / 256.0) * (pressure / 256.0)
139
+            X1 = (X1 * 3038.0) / 65536.0
140
+            X2 = ((-7357) * pressure) / 65536.0
141
+            rv[self.KEY_PRESSURE] = (pressure + (X1 + X2 + 3791) / 16.0) / 100
142
+
143
+            return rv
144
+        else:
145
+            time.sleep(3 * self.SMBUS_DELAY)
146
+            return {
147
+                self.KEY_PRESSURE: 1717.,
148
+                self.KEY_TEMPERATURE: 17.17,
149
+                self.KEY_TIME: int(time.time())
150
+                }

+ 5
- 7
dht.py Bestand weergeven

@@ -19,7 +19,7 @@ else:
19 19
     from . import DEBUG
20 20
 
21 21
 class dht_22(background_task):
22
-    RUN_SLEEP_TIME = 2.0
22
+    RUN_SLEEP_TIME = 4.0
23 23
     MIN_REFRESH_RATE = RUN_SLEEP_TIME
24 24
     KEY_TEMPERATURE = 'temperature'
25 25
     KEY_HUMIDITY = 'humidity'
@@ -43,15 +43,15 @@ class dht_22(background_task):
43 43
             # check data
44 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 45
                 #
46
-                logger.debug('DHT-Communication: Successfully: %s', repr(data))
46
+                logger.info('DHT-Communication successful: %s', repr(data))
47 47
                 self.__monitor__.process(data)
48 48
                 if self.__data_callback__ is not None:
49 49
                     self.__data_callback__(**data)
50 50
                 if self.__monitor__.status():
51
-                    logger.warning("DHT measurement stopped caused by too many identical values!")
51
+                    logger.warning("DHT-Communication failed: Measurement stopped caused by suspicious constant values!")
52 52
                     self.close()
53 53
             else:
54
-                logger.warning("DHT-Gradient to high: Ignoring data %s!", repr(data))
54
+                logger.debug("DHT-Communication failed: Gradient to high. Ignoring data %s!", repr(data))
55 55
                 
56 56
 
57 57
 
@@ -69,7 +69,7 @@ class dht_22(background_task):
69 69
                     time.sleep(self.RUN_SLEEP_TIME)
70 70
                 except RuntimeError as error:
71 71
                     # Errors happen fairly often, DHT's are hard to read, just keep going
72
-                    logger.debug('DHT-Communication: ' + error.args[0])
72
+                    logger.debug('DHT-Communication failed: ' + error.args[0])
73 73
                     time.sleep(2.0)
74 74
                 except Exception as error:
75 75
                     self.__dht_device__.exit()
@@ -127,8 +127,6 @@ class dht_22_monitor(object):
127 127
             else:
128 128
                 self.__init_statevars__()
129 129
         if self.__max_const_measurements__ > self.__max_const_treshold__:
130
-            if not self.__fail__:
131
-                logger.warning("DHT measurement values are suspicious constant!")
132 130
             self.__fail__ = True
133 131
 
134 132
     def status(self):

Laden…
Annuleren
Opslaan