-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDHTSensor.py
executable file
·111 lines (89 loc) · 2.78 KB
/
DHTSensor.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
106
107
108
109
110
111
#!/usr/bin/python
###############################################################################
#
#
# Methods to help retrieve information about temperature and humidity.
#
#
###############################################################################
import subprocess
import re
import datetime
from time import *
import sys
from FileStorage import writeRow
from modules import lcddriver
sensorLib = "resources/Adafruit_DHT"
dataPath = "data/temperature-humidity.csv"
###############################################################################
#
#
# Read date from sensor,
# We can't read directly from sensor python is to slow for that.
# Instead of that we are using program written in C++ and we will parse response,
# to get information about temperature and humidity
#
#
###############################################################################
def readSensorOneTime(GPIO):
output = subprocess.check_output([sensorLib, "2302", GPIO])
matches1 = re.search("Temp =\s+([0-9.]+)", output)
matches2 = re.search("Hum =\s+([0-9.]+)", output)
if not matches1 and not matches2:
raise ValueError("Unable to read data")
temperature = float(matches1.group(1))
humidity = float(matches2.group(1))
return temperature, humidity
###############################################################################
#
#
# Smart way to get single result from sensor
#
#
###############################################################################
def readSensor(GPIO):
while True:
try:
return readSensorOneTime(GPIO)
except ValueError as e:
print e
sleep(3)
###############################################################################
#
#
# Main loop reading constantly sensor
#
#
###############################################################################
print "start"
lcd = lcddriver.lcd(0x27, 0)
waitXTimes = 5
counter = 0
while True:
dateAndTime = datetime.datetime.now()
row = []
row.append("17")
sensor1 = readSensor("17")
row.append(sensor1[0])
row.append(sensor1[1])
# sleep(1)
# row.append("18")
# sensor2 = readSensor("18")
# row.append(sensor2[0])
# row.append(sensor2[1])
try:
#write data to file only once per 5 mins
if counter == 0:
writeRow(dataPath, dateAndTime, row)
lcd.lcd_display_string(str(dateAndTime), 1)
lcd.lcd_display_string("T=" + str(sensor1[0]) + " H=" + str(sensor1[1]), 2)
counter = waitXTimes
#lcd.lcd_display_string("T=" + str(sensor2[0]) + " H=" + str(sensor2[1]), 2)
except IOError as er:
print "Unable to append data " + er
#wait 5s and try again
sleep(5)
#sys.exit()
# Wait 60 seconds before continuing
sleep(60)
counter -= 1