-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_station.py
223 lines (194 loc) · 9.38 KB
/
weather_station.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from __future__ import print_function
from __future__ import division
from di_sensors.temp_hum_press import TempHumPress
import datetime
import os
import sys
import time
import grovepi
from urllib import urlencode
from grove_rgb_lcd import *
import urllib2
# timeinterval 1.0
# https://github.com/srevenant/timeinterval
import timeinterval
import SI1145 #grove_i2c_sunlight_sensor
from config import Config
# ============================================================================
# Constants
# ============================================================================
# specifies how often to measure values from the Sense HAT (in minutes)
MEASUREMENT_INTERVAL = 5 # minutes
# Set to False when testing the code and/or hardware
# Set to True to enable upload of weather data to Weather Underground
WEATHER_UPLOAD = True
# the weather underground URL used to upload weather data
WU_URL = "http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php"
SINGLE_HASH = "#"
HASHES = "########################################"
SLASH_N = "\n"
air_sensor = 0
temp_hum_pr = TempHumPress() # Temprasure Humidity Pressure Sensor API
grovepi.pinMode(air_sensor, 'INPUT')
ir_uv_sensor = SI1145.SI1145()
setRGB(0, 179, 255)
def main():
global last_temp
# initialize the lastMinute variable to the current time to start
last_minute = datetime.datetime.now().minute
# on startup, just use the previous minute as lastMinute
last_minute -= 1
if last_minute == 0:
last_minute = 59
# infinite loop to continuously check weather values
while 1:
# on frequent measurements, so we'll take measurements every 5 seconds
# but only upload on measurement_interval
current_second = datetime.datetime.now().second
# are we at the top of the minute or at a 5 second interval?
if (current_second == 0) or ((current_second % (5/2)) == 0):
# Read the temperature
temp_f = round(temp_hum_pr.get_temperature_fahrenheit())
# Read the relative humidity
humidity = round(temp_hum_pr.get_humidity())
# Read the pressure in pascals
# https://www.metric-conversions.org/pressure/pascals-to-inches-of-mercury.htm
pressure = round(temp_hum_pr.get_pressure() * 0.00029530)
# Get sensor value
air_sensor_value = grovepi.analogRead(air_sensor)
if air_sensor_value > 700:
print ('High pollution')
elif air_sensor_value > 300:
print ('Low pollution')
else:
print ('Air fresh')
readVisible = ir_uv_sensor.readVisible()
IR = ir_uv_sensor.readIR()
UV = ir_uv_sensor.readUV()
uvIndex = UV / 100.0
print(temp_f)
print(humidity)
print(pressure)
setText('Temp: {:5.3f} Hum: {:5.3f} Pres: {:5.3f}, Air: {:5.3f}, Visible: {:5.3f}, IR: {:5.3f}, UV: {:5.3f}, UV Index: {:5.3f}'.format(temp_f, humidity, pressure, air_sensor_value, readVisible, IR, UV, uvIndex))
# get the current minute
current_minute = datetime.datetime.now().minute
# is it the same minute as the last time we checked?
if current_minute != last_minute:
# reset last_minute to the current_minute
last_minute = current_minute
# is minute zero, or divisible by 10?
# we're only going to take measurements every MEASUREMENT_INTERVAL minutes
if (current_second == 0) or ((current_second % (5/2)) == 0):
# get the reading timestamp
now = datetime.datetime.now()
print("\n%d second mark (%d @ %s)" % ((5/2), current_minute, str(now)))
# did the temperature go up or down?
if last_temp != temp_f:
if last_temp > temp_f:
# display a blue, down arrow
setRGB(0, 154, 255)
else:
# display a red, up arrow
setRGB(255, 0, 0)
else:
# temperature stayed the same
# display red and blue bars
setRGB(145, 0, 255)
# set last_temp to the current temperature before we measure again
last_temp = temp_f
# ========================================================
# Upload the weather data to Weather Underground
# ========================================================
# is weather upload enabled (True)?
if WEATHER_UPLOAD:
# From http://wiki.wunderground.com/index.php/PWS_-_Upload_Protocol
print("Uploading data to Weather Underground")
# build a weather data object
google_spreadsheet_data = {
'id': wu_station_id,
'date': datetime.datetime.now(),
'temp': str(temp_f),
'humidity': str(humidity),
'baromin': str(pressure),
'air': str(air_sensor_value),
'visible': str(readVisible),
'ir': str(IR),
'uv': str(UV),
'uv_index': str(uvIndex),
'realtime': str(1),
'rtfreq': str(2.5),
}
weather_data = {
"softwaretype": "RaspberryPi",
"action": "updateraw",
"ID": wu_station_id,
"PASSWORD": wu_station_key,
"dateutc": datetime.datetime.now(),
"tempf": str(temp_f),
'solarradiation': str(readVisible),
"humidity": str(humidity),
"baromin": str(pressure),
"UV": str(uvIndex),
}
try:
# upload_url = WU_URL + "?" + urlencode(weather_data)
# response = urllib2.urlopen(upload_url)
# html = response.read()
# print("Server response:", html)
# do something
# response.close() # best practice to close the file
google_sheet_url = 'https://script.google.com/macros/s/AKfycbw7j2UB76OLKf6mvwnKJTK4JP1HZCKxGnhxb_4d1ezK-mnQS_yw/exec' + '?' + urlencode(google_spreadsheet_data)
res = urllib2.urlopen(google_sheet_url)
res_html = res.read()
print("Server response:", res_html)
res.close()
except:
print('Exception:', sys.exc_info()[0], SLASH_N)
else:
print('Skipping Weather Underground upload')
# wait a second then check again
# You can always increase the sleep value below to check less often
time.sleep(1) # this should never happen since the above is an infinite loop
print('Leaving main()')
# ============================================================================
# here's where we start doing stuff
# ============================================================================
print(SLASH_N + HASHES)
print(SINGLE_HASH, 'Pi Weather Station ', SINGLE_HASH)
print(SINGLE_HASH, 'By Fernando Garcia ', SINGLE_HASH)
print(HASHES)
# make sure we don't have a MEASUREMENT_INTERVAL > 60
if (MEASUREMENT_INTERVAL is None) or (MEASUREMENT_INTERVAL > 60):
print('The application\'s \'MEASUREMENT_INTERVAL\' cannot be empty or greater than 60')
sys.exit(1)
# ============================================================================
# Read Weather Underground Configuration Parameters
# ============================================================================
print('\nInitializing Weather Underground configuration')
wu_station_id = Config.STATION_ID
wu_station_key = Config.STATION_KEY
if (wu_station_id is None) or (wu_station_key is None):
print('Missing values from the Weather Underground configuration file\n')
sys.exit(1)
# we made it this far, so it must have worked...
print('Successfully read Weather Underground configuration values')
print('Station ID:', wu_station_id)
# print("Station key:", wu_station_key)
# ============================================================================
# initialize the Sense HAT object
# ============================================================================
try:
setText('Initializing Weather App')
last_temp = temp_hum_pr.get_temperature_fahrenheit()
setText('Current temperature reading: ' + str(last_temp))
except:
print('Unable to initialize Weather App', sys.exc_info()[0])
sys.exit(1)
setText('Initialization complete!')
# Now see what we're supposed to do next
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
setText('\nExiting application\n')
sys.exit(0)