-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi_dht.py
75 lines (60 loc) · 1.99 KB
/
rpi_dht.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
import time
import Adafruit_DHT
import RPi.GPIO as GPIO
from RPLCD import CharLCD
from datetime import datetime
import pytz
POLL_RATE = 30 * 60 # ROP in minutes
LCD_DATA_PINS = [13, 6, 5, 11]
S_PIN, SENSOR, B_PIN = 14, 22, 23 # Sensor signal, sensor data, button pin
print("Initializing...")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(B_PIN, GPIO.IN,
pull_up_down=GPIO.PUD_DOWN)
lcd = CharLCD(pin_rs=26, pin_e=19, pins_data=LCD_DATA_PINS,
numbering_mode=GPIO.BCM, dotsize=8,
auto_linebreaks=True,
cols=16, rows=2)
lcd.clear()
def main():
while True:
poll_sensor()
idle_sensor() # Hold for 30 min, listen for button down
def poll_sensor():
temp, humidity = read_sensor()
timeNow = datetime.now(pytz.timezone('US/Pacific')).strftime("%m/%d/%Y %H:%M")
print('Temp:{0}\u00b0F Hum:{1}%'.format(temp, humidity), timeNow)
with open(r'sensor_log.txt', 'a') as log:
output = str(temp) + ',' + str(humidity) + ',' + timeNow + '\n'
log.write(output)
log.close()
def idle_sensor():
counter = 0
while counter <= POLL_RATE:
if GPIO.input(B_PIN) == GPIO.HIGH:
trigger_sensor(counter)
counter += 1
time.sleep(1)
def read_sensor() -> (float, float):
humidity, temp = Adafruit_DHT.read_retry(SENSOR, S_PIN)
temp = temp * 9/5.0 + 32
if humidity is None and temp is None:
temp, humidity = -1, -1
lcd.write_string('Failed to read sensor')
lcd.clear()
return temp, humidity
return round(temp, 2), round(humidity, 2)
def trigger_sensor(counter: int):
lcd.clear()
print(">> Reading sensor..")
lcd.write_string('Reading sensor..')
temp, humidity = read_sensor()
lcd.clear()
lcd.write_string('{0}F, {1}% Hum, c{2}'.format(temp, humidity, counter))
time.sleep(3)
lcd.clear()
main()
GPIO.cleanup()
# Overlay graph with Temp vs Time and Hum vs Time
# And overlay with hourly local weather