-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht22.py
48 lines (35 loc) · 1.05 KB
/
dht22.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
from threading import Thread
import time
import Adafruit_DHT
class DHT22:
def __init__(self, pin=4):
self.dht22 = Adafruit_DHT.DHT22
self.pin = pin
self.temperature = None
self.humidity = None
self.start()
time.sleep(2)
def start(self):
t = Thread(target=self.update, args=())
t.daemon = True
t.start()
def update(self):
while True:
hum, temp = Adafruit_DHT.read_retry(self.dht22, self.pin)
if hum is None or temp is None:
continue
self.humidity = hum
self.temperature = temp
def get_temperature(self):
return self.temperature
def get_humidity(self):
return self.humidity
if __name__ == '__main__':
sense = DHT22()
for i in range(5):
temp = sense.get_temperature()
hum = sense.get_humidity()
print(f'====={time.ctime()}=====')
print(f'Temperature: {temp}')
print(f'Himidity: {hum}')
time.sleep(2)