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