-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensors.py
105 lines (91 loc) · 3.02 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Declaration of all the sensors that will be interacting with the micro-controller
in this project.
"""
from utime import time
from machine import Pin, ADC
from micropython import const
from dht import DHT11, DHT22
class Sensor:
id = "1234" # sensorId
rawValue = 0.0
calcValue = 0.0
def __init__(self, id):
self.id = id
def read(self):
"""default behaviour: calcValue is returned"""
return self.calcValue
def calculate(self):
"""default formula is simply using the rawValue for the calculated one"""
self.calcValue = self.rawValue
def __str__(self):
"""representation"""
return f"Sensor({self.id})"
#
# ---- Sensor for reading the level of moisture in the soil
# ---- MAker Soil Moisture Sensor
#
WET_READ = const(27803.7)
DRY_READ = const(45675.9)
class MakerSoilMoisture(Sensor):
"""
LED Turn On Output Voltage @VCC = 3.3V
Min Max
Blue (WET) 1.4 1.8V Reading: 27803
Green (MOIST) 1.8 2.1V
Red (DRY) 2.1 2.3V Reading: 45676
https://sg.cytron.io/p-maker-soil-moisture-sensor?r=1
"""
def __init__(self, id, pin):
Sensor.__init__(self, id)
self._adc = ADC(Pin(pin))
def calculate(self):
"""transform the reading in Volt into the moisture %"""
calcValue = (DRY_READ - self.rawValue) * 100.0 / (DRY_READ - WET_READ)
return min(100.0, max(0.0, calcValue))
def read(self):
self.rawValue = self._adc.read_u16()
self.calcValue = self.calculate()
return self.calcValue
class DHT:
"""
Supports both DHT11 and DHT 22
Since the sensor captures both air temperature and humidity, one reading is performed
"""
lastRead = time() - 4
def __init__(self, id, serie, pin):
"""
serie: either 11 or 22 to choose between DHT11 and DHT22
"""
self.dht = DHT11(Pin(pin)) if serie == 11 else DHT22(Pin(pin))
self.DHTT = Sensor(f"{id}_T")
self.DHTH = Sensor(f"{id}_H")
def read(self):
"""
Only read if the last read was done 3 seconds ago
"""
if time() - self.lastRead > 3:
try:
self.dht.measure()
self.DHTT.rawValue = self.DHTT.calcValue = self.dht.temperature()
self.DHTH.rawValue = self.DHTH.calcValue = self.dht.humidity()
except OSError as err:
pass
else:
self.lastRead = time()
@property
def temperature(self):
return self.DHTT.calcValue
@property
def humidity(self):
return self.DHTH.calcValue
if __name__ == "__main__":
acds = (MakerSoilMoisture("ACD0", 26), MakerSoilMoisture("ACD1", 27), MakerSoilMoisture("ACD2", 28))
for s in acds:
moisture = s.read()
print(s.id, s.rawValue, f"{moisture}%")
dht = DHT("DHT", 11, 15)
dht.read()
print("temperature:", dht.temperature)
dht.read()
print("humidity:", dht.humidity)