-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_wq.py
executable file
·139 lines (90 loc) · 3.18 KB
/
monitor_wq.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#! /usr/bin/env python
from time import sleep
import RPi.GPIO as GPIO
import lcd_functions as lf
import psycopg2
import ds18b20
from datetime import datetime
def get_cursor():
"""get database cursor"""
conn = psycopg2.connect(database="postgres",
user="postgres",
password="apassword",
host="192.168.0.104")
conn.autocommit = True
cur = conn.cursor()
return cur
def get_weather(cur):
"""returns most recent temperature"""
cur.execute("""SELECT condition -> 'current_observation' -> 'temp_f'
FROM arlington_weather_condition
WHERE id = (SELECT MAX(id)
FROM arlington_weather_condition
WHERE condition -> 'current_observation' ->> 'temp_f' != '')""")
temp = [i[0] for i in cur][0]
return temp
def get_forecast(cur):
"""returns most recent forecast"""
cur.execute("""SELECT forecast -> 'forecast' -> 'simpleforecast' -> 'forecastday'
FROM arlington_weather_forecast
WHERE id = (SELECT MAX(id)
FROM arlington_weather_forecast);""")
data = [i[0] for i in cur][0][0]
low = str(data['low']['fahrenheit'])
high = str(data['high']['fahrenheit'])
return low, high
def get_inside(cur):
"""returns most recent inside temperature"""
cur.execute("""SELECT temp
FROM inside_temp
WHERE id = (SELECT MAX(id)
FROM inside_temp);""")
temp = str([i for i in cur][0][0])
return temp
def pick_color(temp):
"""returns color name based on temp"""
if temp >= 90.0:
color = 'red'
elif temp < 90.0 and temp >= 80.0:
color = 'yellow'
elif temp < 80.0 and temp >= 70.0:
color = 'green'
elif temp < 70.0 and temp >= 60.0:
color = 'cyan'
elif temp < 60.0 and temp >= 50.0:
color = 'blue'
else:
color = 'white'
return color
def monitor(wait=60):
"""continuous print latest temp to lcd"""
try:
# get lcd object
lcd = lf.lcd
# continuously read sensors
while True:
# get database cursor
cur = get_cursor()
# get inside temp
inside = get_inside(cur)
# get the latest temp
temp = get_weather(cur)
# get low/high
low, high = get_forecast(cur)
# pick background color based on temp
temp_color = pick_color(temp)
# create string to print to screen
temp_str = "In:{} Out:{}\n High:{} Low:{} ".format(inside, temp, high, low)
# display temp on LCD and set RGB color based on temp
lf.display_text(temp_str, color=temp_color)
# print temp to screen
print(temp_str)
# wait n seconds
sleep(wait)
except Exception, e:
print(str(e))
finally:
lcd.clear()
GPIO.cleanup()
if __name__ == '__main__':
monitor()