-
Notifications
You must be signed in to change notification settings - Fork 2
/
sensors.py
65 lines (47 loc) · 1.55 KB
/
sensors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import time
import smbus
import Adafruit_DHT
# Change to 0 if using pi with 256MB
i2c_bus = smbus.SMBus(1)
class AbstractSensor():
def __init__(self):
self.last_failure = 0
self.last_success = 0
def read(self):
raise('Abstract')
class DHTSensor(AbstractSensor):
def __init__(self, bcm_pin, location):
super(DHTSensor, self).__init__()
self.bcm_pin = bcm_pin
self.location = location
def read(self):
rel_humidity, temp = Adafruit_DHT.read_retry(
self.dht_type, self.bcm_pin)
if not temp or not rel_humidity:
self.last_failure = time.time()
return
self.last_success = time.time()
return {
'temperature': temp,
'rel_humidity': rel_humidity
}
class ADCSensor(AbstractSensor):
def __init__(self, i2c_addr, location):
super(ADCSensor, self).__init__()
self.i2c_addr = i2c_addr
self.location = location
def read(self):
for x in range(0, 4):
i2c_bus.write_byte_data(0x48, 0x40 | ((x + 1) & 0x03), 0)
v = i2c_bus.read_byte(0x48)
print(v,)
time.sleep(0.1)
print()
class DHT22Sensor(DHTSensor):
def __init__(self, bcm_pin, location):
self.dht_type = Adafruit_DHT.DHT22
super(DHT22Sensor, self).__init__(bcm_pin, location)
class DHT11Sensor(DHTSensor):
def __init__(self, bcm_pin, location):
self.dht_type = Adafruit_DHT.DHT11
super(DHT11Sensor, self).__init__(bcm_pin, location)