-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsense_temp_humidity_v0.3.py
executable file
·104 lines (92 loc) · 2.95 KB
/
sense_temp_humidity_v0.3.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
#!/usr/bin/python
from gpiozero import LED
from gpiozero import Button
from signal import pause
from subprocess import *
import sys
import os
import time
import Adafruit_DHT
from time import sleep
redled = LED(17)
amberled = LED(27)
greenled = LED(22)
button = Button(2)
def WriteData():
return
def ReadValues():
# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None and temperature is not None:
print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
print('The current Temperature is {0:0.1f} Degrees Celcius while the current Humidity is {1:0.1f}%'.format(temperature, humidity))
print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
time.sleep(60)
else:
print('Failed to get a reading. Will try again!')
time.sleep(60)
return
def SeeAndListen():
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
#print('The current Temperature is {0:0.1f} Degrees Celcius while the current Humidity is {1:0.1f}%'.format(temperature, humidity))
if temperature >= 0 and temperature <= 10:
greenled.on()
sleep(0.5)
greenled.off()
sleep(0.5)
greenled.on()
sleep(0.5)
greenled.off()
cmd="espeak 'The current temperature is " + str(temperature) + "and the humidity is" + str(humidity) + "Percent'"
os.system(cmd)
return
elif temperature > 10 and temperature <= 20:
amberled.on()
sleep(0.5)
amberled.off()
sleep(0.5)
amberled.on()
sleep(0.5)
amberled.off()
cmd="espeak 'The current temperature is " + str(temperature) + "and the humidity is" + str(humidity) + "Percent'"
os.system(cmd)
return
elif temperature > 20:
redled.on()
sleep(0.5)
redled.off()
sleep(0.5)
redled.on()
sleep(0.5)
redled.off()
cmd="espeak 'The current temperature is " + str(temperature) + "and the humidity is" + str(humidity) + "Percent'"
os.system(cmd)
return
else:
print('Failed to get a reading. I will not blink this time around. Will try again soon!!!')
time.sleep(5)
return
# Parse command line parameters.
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
sensor = sensor_args[sys.argv[1]]
pin = sys.argv[2]
else:
print('usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#')
print('example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4')
sys.exit(1)
while True:
ReadValues()
SeeAndListen()
WriteData()
pause()