-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht11.py
49 lines (38 loc) · 885 Bytes
/
dht11.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
from time import sleep
from datetime import datetime
from common import getDB
import Adafruit_DHT
class DHT11():
def __init__(self, pin):
self.pin = pin
def readValue(self):
value = None
pin = self.pin
try:
value = Adafruit_DHT.read_retry(11,pin)
except:
pass
return value
def loop(self):
sql = ('INSERT INTO TempHumidity (datetime,Humidity,Temperature) VALUES (%(datetime)s,%(humidity)s,%(temperature)s)')
db = None
cursor = None
while True:
if not db:
db = getDB()
date_time = datetime.now()
try:
cursor = db.cursor()
humidity, temperature = self.readValue()
if humidity and temperature:
data = {
'datetime': date_time,
'humidity': humidity,
'temperature': temperature
}
cursor.execute(sql,data)
db.commit()
except:
pass
sleep(30)
db.close()