Repository navigation
Expand file tree
/
Copy pathPiWeather.py
More file actions
146 lines (112 loc) · 3.72 KB
/
Copy pathPiWeather.py
File metadata and controls
146 lines (112 loc) · 3.72 KB
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
140
141
142
143
144
145
146
import configparser
import logging
import pigpio
import requests
import sys
import time
#logging.basicConfig(level=logger.INFO)
logger = logging.getLogger(__name__)
pi = pigpio.pi()
def main():
#Load config
config = configparser.ConfigParser()
config.read('/boot/PiWeather.ini')
#Setup logging
ch = logging.StreamHandler()
logger.setLevel(logging.getLevelName(config['General'].get('LogLevel','INFO')))
ch.setLevel(logging.getLevelName(config['General'].get('LogLevel','INFO')))
logger.addHandler(ch)
#Parse config
(Gages, Current) = ParseConfig(config)
OverrideButton = int(config.get('General','OverrideButton'))
#Initialize
Display = AnalogDisplay(OverrideButton, Gages)
while 1:
try:
Current = GetWeather(Gages, Current)
Display.UpdateGages(Current)
time.sleep(60)
except KeyboardInterrupt:
Shutdown()
def ParseConfig(config):
Gages = []
Current = {}
for s in config.sections():
if s == 'General':
continue
G = {'Station': s, 'Fallback': config.get(s,'Fallback', fallback=None), 'GPIO': int(config.get(s,'GPIO')), 'Min': int(config.get(s,'Min')), 'Max': int(config.get(s,'Max'))}
Current[s] = 0
Gages.append(G)
logger.info(Gages)
return (Gages, Current)
def Shutdown():
logger.info('Shutting down....')
pi.stop()
sys.exit(0)
def GetWeather(Gages, Current):
Stations = ''
for G in Gages:
Stations += G['Station'] + ','
if G['Fallback']:
Stations += G['Fallback'] + ','
logger.debug('Using stations %s',Stations)
try:
r = requests.get('http://stationdata.wunderground.com/cgi-bin/stationlookup?station={0:s}&units=english&v=2.0&format=json&_={1:d}'.format(Stations,int(round(time.time()*1000,0))))
data = r.json()
logger.debug('Raw weather response: %s',str(data))
for G in Gages:
if G['Station'] in data['stations']:
Current[G['Station']] = float(data['stations'][G['Station']]['temperature'])
elif G['Fallback'] in data['stations']:
Current[G['Station']] = float(data['stations'][G['Fallback']]['temperature'])
logger.info('Unable to get weather from WUnderground for %s, using FALLBACK %s', G['Station'], G['Fallback'])
else:
logger.info('Unable to get weather from WUnderground for %s', G['Station'])
except Exception:
logger.exception('Failed to pull weather from WUnderground for %s', Stations)
return Current
class AnalogDisplay():
def __init__(self, OverrideButton, Gages):
self.OverrideButton = OverrideButton
self.Gages = Gages
self.DutyRange = 100
self.Override = False
self.OverrideTime = time.time()
#Initialize output pins
for G in self.Gages:
pi.set_PWM_range(G['GPIO'], self.DutyRange) #PWM from 0-100
#Initialize max button
pi.set_mode(self.OverrideButton, pigpio.INPUT)
pi.set_pull_up_down(self.OverrideButton, pigpio.PUD_DOWN)
pi.callback(self.OverrideButton, pigpio.FALLING_EDGE, self.DoOverride)
self.StartupAnimation()
def UpdateGages(self, Current):
if self.Override:
logger.debug('In override mode, not updating gages')
if time.time() - self.OverrideTime > 60:
self.Override = False
else:
return
s = ''
for G in self.Gages:
Reading = Current[G['Station']]
Range = G['Max'] - G['Min']
Output = (Reading - G['Min'])/Range
Output = max(0,min(1,Output))
Duty = Output*self.DutyRange
s += ' ' + G['Station'] + ' = ' + str(Reading)
pi.set_PWM_dutycycle(G['GPIO'], Duty)
logger.info(s)
def DoOverride(self, gpio, level, tick):
logger.info('Doing override')
self.Override = True
self.OverrideTime = time.time()
for G in self.Gages:
pi.set_PWM_dutycycle(G['GPIO'], self.DutyRange)
def StartupAnimation(self):
for G in self.Gages:
pi.set_PWM_dutycycle(G['GPIO'], self.DutyRange)
time.sleep(0.5)
pi.set_PWM_dutycycle(G['GPIO'], 0)
if __name__ == "__main__":
main()