Pārlūkot izejas kodu

threaded cyclic data aquisition implemented

spike_filter
Dirk Alders 4 gadus atpakaļ
vecāks
revīzija
75d30731e4
3 mainītis faili ar 199 papildinājumiem un 42 dzēšanām
  1. 3
    42
      __init__.py
  2. 141
    0
      bmp.py
  3. 55
    0
      dht.py

+ 3
- 42
__init__.py Parādīt failu

@@ -1,44 +1,5 @@
1
-try:
2
-    from config import dht_gpio
3
-except ImportError:
4
-    dht_gpio = None
1
+import logging
5 2
 
6
-try:
7
-    import Adafruit_DHT as dht
8
-except ImportError:
9
-    dht = None
10 3
 
11
-try:
12
-    import Adafruit_BMP.BMP085 as bmp
13
-except ImportError:
14
-    bmp = None
15
-
16
-#TODO: background process for value sensing (every x seconds)
17
-
18
-KEY_TEMPERATURE = 't'
19
-KEY_HUMIDITY = 'h'
20
-KEY_PRESSURE = 'p'
21
-
22
-
23
-class EnvSens(object):
24
-    def __init__(self):
25
-        if bmp is not None:
26
-            self.bmp = bmp.BMP085()
27
-
28
-    def get_env_data(self):
29
-        #
30
-        # DHT22
31
-        #
32
-        if dht is None or dht_gpio is None:
33
-            h = -17.3
34
-            t = -125.7
35
-        else:
36
-            h, t = dht.read_retry(dht.DHT22, dht_gpio)
37
-        #
38
-        # BMP180
39
-        #
40
-        if bmp is None:
41
-            p = -1024.17
42
-        else:
43
-            p = self.bmp.read_pressure()/100.
44
-        return {KEY_TEMPERATURE: t, KEY_HUMIDITY: h, KEY_PRESSURE: p}
4
+logger_name = 'RPI_ENVSENS'
5
+logger = logging.getLogger(logger_name)

+ 141
- 0
bmp.py Parādīt failu

@@ -0,0 +1,141 @@
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
+import time
10
+
11
+from . import logger
12
+
13
+
14
+class bmp_180(object):
15
+	KEY_TEMPERATURE = 'temperature'
16
+	KEY_PRESSURE = 'pressure'
17
+	KEY_TIME = 'time'
18
+	
19
+	def __init__(self, data_callback=None):
20
+		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)
35
+	
36
+	def close(self):
37
+		self.__active__ = False
38
+		self.__thread__.join()
39
+	
40
+	def __del__(self):
41
+		self.close()
42
+	
43
+	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)
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(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

+ 55
- 0
dht.py Parādīt failu

@@ -0,0 +1,55 @@
1
+import adafruit_dht
2
+import threading
3
+import time
4
+
5
+from . import logger
6
+
7
+
8
+class dht_22(object):
9
+    KEY_TEMPERATURE = 'temperature'
10
+    KEY_HUMIDITY = 'humidity'
11
+    KEY_TIME = 'time'
12
+
13
+    def __init__(self, gpio, data_callback=None):
14
+        self.__data_callback__ = data_callback
15
+        # 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()
37
+
38
+    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

Notiek ielāde…
Atcelt
Saglabāt