Ver código fonte

Restructuring

spike_filter
Dirk Alders 4 anos atrás
pai
commit
859b5dec67
6 arquivos alterados com 189 adições e 168 exclusões
  1. 28
    0
      __init__.py
  2. BIN
      _datasheets_/datasheet.pdf
  3. BIN
      _datasheets_/schematic_ardu.pdf
  4. BIN
      _datasheets_/schematic_rpi.pdf
  5. 119
    126
      bmp.py
  6. 42
    42
      dht.py

+ 28
- 0
__init__.py Ver arquivo

@@ -1,5 +1,33 @@
1 1
 import logging
2
+import threading
3
+import time
2 4
 
5
+DEBUG = False
3 6
 
4 7
 logger_name = 'RPI_ENVSENS'
5 8
 logger = logging.getLogger(logger_name)
9
+
10
+
11
+class background_task(object):
12
+    RUN_SLEEP_TIME = 2.0
13
+
14
+    def __init__(self):
15
+        self.__active__ = True
16
+        self.__thread__ = threading.Thread(target=self.run, args=())
17
+        self.__thread__.daemon = True                            # Daemonize thread
18
+        self.__thread__.start()                                  # Start the execution
19
+
20
+    def __run__(self):
21
+        pass
22
+
23
+    def run(self):
24
+        while self.__active__:
25
+            self.__run__()
26
+            time.sleep(self.RUN_SLEEP_TIME)
27
+
28
+    def close(self):
29
+        self.__active__ = False
30
+        self.__thread__.join()
31
+
32
+    def __del__(self):
33
+        self.close()

BIN
_datasheets_/datasheet.pdf Ver arquivo


BIN
_datasheets_/schematic_ardu.pdf Ver arquivo


BIN
_datasheets_/schematic_rpi.pdf Ver arquivo


+ 119
- 126
bmp.py Ver arquivo

@@ -1,141 +1,134 @@
1
-# Distributed with a free-will license.
2
-# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
3
-# BMP180
4
-# This code is designed to work with the BMP180_I2CS I2C Mini Module available from ControlEverything.com.
5
-# https://www.controleverything.com/content/Pressure?sku=BMP180_I2CS#tabs-0-product_tabset-2
6
-
7
-import smbus
8
-import threading
9 1
 import time
10 2
 
11
-from . import logger
3
+from . import logger, background_task, DEBUG
4
+if not DEBUG:
5
+	import smbus
12 6
 
13 7
 
14
-class bmp_180(object):
8
+class bmp_180(background_task):
9
+	RUN_SLEEP_TIME = 0.5
10
+	SMBUS_DELAY = 0.5
11
+	MIN_REFRESH_RATE = RUN_SLEEP_TIME + 3 * SMBUS_DELAY
15 12
 	KEY_TEMPERATURE = 'temperature'
16 13
 	KEY_PRESSURE = 'pressure'
17 14
 	KEY_TIME = 'time'
18 15
 	
19 16
 	def __init__(self, data_callback=None):
20 17
 		self.__data_callback__ = data_callback
21
-		# Initial the dht device, with data pin connected to:
22
-		self.__active__ = True
23
-		self.__thread__ = threading.Thread(target=self.run, args=())
24
-		self.__thread__.daemon = True                            # Daemonize thread
25
-		self.__thread__.start()                                  # Start the execution
26
-	
27
-	def run(self):
28
-		while self.__active__:
29
-			data = self.__bmp_data_transmission__()
30
-			if data is not None:
31
-				logger.debug('BMP-Communication: Successfully: %s', repr(data))
32
-				if self.__data_callback__ is not None:
33
-					self.__data_callback__(**data)
34
-			time.sleep(0.5)
18
+		# Initialise background_task
19
+		background_task.__init__(self)
35 20
 	
36
-	def close(self):
37
-		self.__active__ = False
38
-		self.__thread__.join()
39
-	
40
-	def __del__(self):
41
-		self.close()
21
+	def __run__(self):
22
+		data = self.__bmp_data_transmission__()
23
+		if data is not None:
24
+			logger.debug('BMP-Communication: Successfully: %s', repr(data))
25
+			if self.__data_callback__ is not None:
26
+				self.__data_callback__(**data)
42 27
 	
43 28
 	def __bmp_data_transmission__(self):
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(0.5)
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(0.5)
29
+		if not DEBUG:
30
+			rv = {}
31
+			# Get I2C bus
32
+			bus = smbus.SMBus(1)
33
+			
34
+			# BMP180 address, 0x77(119)
35
+			# Read data back from 0xAA(170), 22 bytes
36
+			data = bus.read_i2c_block_data(0x77, 0xAA, 22)
37
+			
38
+			# Convert the data
39
+			AC1 = data[0] * 256 + data[1]
40
+			if AC1 > 32767 :
41
+				AC1 -= 65535
42
+			AC2 = data[2] * 256 + data[3]
43
+			if AC2 > 32767 :
44
+				AC2 -= 65535
45
+			AC3 = data[4] * 256 + data[5]
46
+			if AC3 > 32767 :
47
+				AC3 -= 65535
48
+			AC4 = data[6] * 256 + data[7]
49
+			AC5 = data[8] * 256 + data[9]
50
+			AC6 = data[10] * 256 + data[11]
51
+			B1 = data[12] * 256 + data[13]
52
+			if B1 > 32767 :
53
+				B1 -= 65535
54
+			B2 = data[14] * 256 + data[15]
55
+			if B2 > 32767 :
56
+				B2 -= 65535
57
+			MB = data[16] * 256 + data[17]
58
+			if MB > 32767 :
59
+				MB -= 65535
60
+			MC = data[18] * 256 + data[19]
61
+			if MC > 32767 :
62
+				MC -= 65535
63
+			MD = data[20] * 256 + data[21]
64
+			if MD > 32767 :
65
+				MD -= 65535
66
+			
67
+			time.sleep(self.SMBUS_DELAY)
68
+			
69
+			# BMP180 address, 0x77(119)
70
+			# Select measurement control register, 0xF4(244)
71
+			#		0x2E(46)	Enable temperature measurement
72
+			bus.write_byte_data(0x77, 0xF4, 0x2E)
73
+			
74
+			time.sleep(self.SMBUS_DELAY)
75
+			
76
+			# BMP180 address, 0x77(119)
77
+			# Read data back from 0xF6(246), 2 bytes
78
+			# temp MSB, temp LSB
79
+			data = bus.read_i2c_block_data(0x77, 0xF6, 2)
80
+			
81
+			# Convert the data
82
+			temp = data[0] * 256 + data[1]
83
+			
84
+			# BMP180 address, 0x77(119)
85
+			# Select measurement control register, 0xF4(244)
86
+			#		0x74(116)	Enable pressure measurement, OSS = 1
87
+			bus.write_byte_data(0x77, 0xF4, 0x74)
88
+			
89
+			time.sleep(self.SMBUS_DELAY)
90
+			
91
+			# BMP180 address, 0x77(119)
92
+			# Read data back from 0xF6(246), 3 bytes
93
+			# pres MSB1, pres MSB, pres LSB
94
+			data = bus.read_i2c_block_data(0x77, 0xF6, 3)
95
+			
96
+			rv[self.KEY_TIME] = int(time.time())
89 97
 		
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)
98
+			# Convert the data
99
+			pres = ((data[0] * 65536) + (data[1] * 256) + data[2]) / 128
100
+			# Callibration for Temperature
101
+			X1 = (temp - AC6) * AC5 / 32768.0
102
+			X2 = (MC * 2048.0) / (X1 + MD)
103
+			B5 = X1 + X2
104
+			rv[self.KEY_TEMPERATURE] = ((B5 + 8.0) / 16.0) / 10.0
105
+			
106
+			# Calibration for Pressure
107
+			B6 = B5 - 4000
108
+			X1 = (B2 * (B6 * B6 / 4096.0)) / 2048.0
109
+			X2 = AC2 * B6 / 2048.0
110
+			X3 = X1 + X2
111
+			B3 = (((AC1 * 4 + X3) * 2) + 2) / 4.0
112
+			X1 = AC3 * B6 / 8192.0
113
+			X2 = (B1 * (B6 * B6 / 2048.0)) / 65536.0
114
+			X3 = ((X1 + X2) + 2) / 4.0
115
+			B4 = AC4 * (X3 + 32768) / 32768.0
116
+			B7 = ((pres - B3) * (25000.0))
117
+			pressure = 0.0
118
+			if B7 < 2147483648:
119
+				pressure = (B7 * 2) / B4
120
+			else :
121
+				pressure = (B7 / B4) * 2
122
+			X1 = (pressure / 256.0) * (pressure / 256.0)
123
+			X1 = (X1 * 3038.0) / 65536.0
124
+			X2 = ((-7357) * pressure) / 65536.0
125
+			rv[self.KEY_PRESSURE] = (pressure + (X1 + X2 + 3791) / 16.0) / 100
94 126
 		
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(0.5)
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] = 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
127
+			return rv
128
+		else:
129
+			time.sleep(3 * self.SMBUS_DELAY)
130
+			return {
131
+				self.KEY_PRESSURE: 1717.,
132
+				self.KEY_TEMPERATURE: 17.17,
133
+				self.KEY_TIME: int(time.time())
134
+			}

+ 42
- 42
dht.py Ver arquivo

@@ -1,11 +1,14 @@
1
-import adafruit_dht
2
-import threading
3 1
 import time
4 2
 
5
-from . import logger
3
+from . import logger, background_task, DEBUG
6 4
 
5
+if not DEBUG:
6
+    import adafruit_dht
7 7
 
8
-class dht_22(object):
8
+
9
+class dht_22(background_task):
10
+    RUN_SLEEP_TIME = 2.0
11
+    MIN_REFRESH_RATE = RUN_SLEEP_TIME
9 12
     KEY_TEMPERATURE = 'temperature'
10 13
     KEY_HUMIDITY = 'humidity'
11 14
     KEY_TIME = 'time'
@@ -13,43 +16,40 @@ class dht_22(object):
13 16
     def __init__(self, gpio, data_callback=None):
14 17
         self.__data_callback__ = data_callback
15 18
         # Initial the dht device, with data pin connected to:
16
-        self.__dht_device__ = adafruit_dht.DHT22(gpio)
17
-        self.__active__ = True
18
-        self.__thread__ = threading.Thread(target=self.run, args=())
19
-        self.__thread__.daemon = True                            # Daemonize thread
20
-        self.__thread__.start()                                  # Start the execution
21
-
22
-    def run(self):
23
-        while self.__active__:
24
-            data = self.__dht_data_transmission__()
25
-            if data is not None:
26
-                logger.debug('DHT-Communication: Successfully: %s', repr(data))
27
-                if self.__data_callback__ is not None:
28
-                    self.__data_callback__(**data)
29
-            time.sleep(2.0)
30
-
31
-    def close(self):
32
-        self.__active__ = False
33
-        self.__thread__.join()
34
-
35
-    def __del__(self):
36
-        self.close()
19
+        if not DEBUG:
20
+            self.__dht_device__ = adafruit_dht.DHT22(gpio)
21
+        # Initialise background_task
22
+        background_task.__init__(self)
23
+
24
+    def __run__(self):
25
+        data = self.__dht_data_transmission__()
26
+        if data is not None:
27
+            logger.debug('DHT-Communication: Successfully: %s', repr(data))
28
+            if self.__data_callback__ is not None:
29
+                self.__data_callback__(**data)
37 30
 
38 31
     def __dht_data_transmission__(self):
39
-        while self.__active__:
40
-            try:
41
-                # Store the values
42
-                data = {}
43
-                data[self.KEY_TEMPERATURE] = self.__dht_device__.temperature
44
-                data[self.KEY_HUMIDITY] = self.__dht_device__.humidity
45
-                data[self.KEY_TIME] = time.time()
46
-                if data[self.KEY_TEMPERATURE] is not None and data[self.KEY_HUMIDITY] is not None:
47
-                    return data
48
-                time.sleep(2.0)
49
-            except RuntimeError as error:
50
-                # Errors happen fairly often, DHT's are hard to read, just keep going
51
-                logger.debug('DHT-Communication: ' + error.args[0])
52
-                time.sleep(0.1)
53
-            except Exception as error:
54
-                self.__dht_device__.exit()
55
-                raise error
32
+        if not DEBUG:
33
+            while self.__active__:
34
+                try:
35
+                    # Store the values
36
+                    data = {}
37
+                    data[self.KEY_TEMPERATURE] = self.__dht_device__.temperature
38
+                    data[self.KEY_HUMIDITY] = self.__dht_device__.humidity
39
+                    data[self.KEY_TIME] = int(time.time())
40
+                    if data[self.KEY_TEMPERATURE] is not None and data[self.KEY_HUMIDITY] is not None:
41
+                        return data
42
+                    time.sleep(2.0)
43
+                except RuntimeError as error:
44
+                    # Errors happen fairly often, DHT's are hard to read, just keep going
45
+                    logger.info('DHT-Communication: ' + error.args[0])
46
+                    time.sleep(0.1)
47
+                except Exception as error:
48
+                    self.__dht_device__.exit()
49
+                    raise error
50
+        else:
51
+            return {
52
+                self.KEY_TEMPERATURE: -17.17,
53
+                self.KEY_HUMIDITY: 1.7,
54
+                self.KEY_TIME: int(time.time()),
55
+            }

Carregando…
Cancelar
Salvar