Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
|
I am working on increasing my use of Python and starting into PHP, so I took what I have found out about getting information from JSON data and am now building a web application that pulls the weather every 10 minutes and writes a webpage to display that information.
I am using the Onion Omega 2+, Python3 and PHP7 along with a shell script and crontab entry to create the webpage. I am using my own Weather Underground API key (free from Weather Underground by visiting https://www.wunderground.com/weather/api ).
The crontab runs the shell script every 10 minutes to delete the previous webpage and run the Python script that pulls the data from the Weather Underground site and create the php file that will display the information.
The web page created is very simple, but has all of the pertinent information and should (I believe) follow the WUnderground policies. Working on that part. I am sure there is a simpler way to accomplish this task, but again, it is my own stepping stone into more coding.
OPKG Installs
Plain texthttps://wiki.onion.io/Tutorials/Contents#using-software-on-the-omega_php
opkg update
opkg install python3 php7 python-light python-urllib3
Do not forget to chmod +x on the shell script.
phpweather.py
Python#!/usr/bin/env python
import urllib2
import json
weatherdata = urllib2.urlopen("http://api.wunderground.com/api/<YOUR API KEY>/conditions/q/pws:KKYLOUIS68.json")
astrodata = urllib2.urlopen("http://api.wunderground.com/api/<YOUR API KEY>/astronomy/q/pws:KKYLOUIS68.json")
alertdata = urllib2.urlopen("http://api.wunderground.com/api/<YOUR API KEY>/alerts/q/pws:KKYLOUIS68.json")
weatherinfo = json.loads(weatherdata.read())
astroinfo = json.loads(astrodata.read())
alertinfo = json.loads(alertdata.read())
loc = weatherinfo['current_observation']['observation_location']['full']
updt = weatherinfo['current_observation']['observation_time']
cond = weatherinfo['current_observation']['weather']
temps = weatherinfo['current_observation']['temperature_string']
feels = weatherinfo['current_observation']['feelslike_string']
windchill = weatherinfo['current_observation']['windchill_string']
heat = weatherinfo['current_observation']['heat_index_string']
dew = weatherinfo['current_observation']['dewpoint_string']
humidity = weatherinfo['current_observation']['relative_humidity']
pressure = weatherinfo['current_observation']['pressure_in']
if weatherinfo['current_observation']['pressure_trend'] == '+':
trendinfo = 'upwards'
elif weatherinfo['current_observation']['pressure_trend'] == '-':
trendinfo = 'downwards'
elif weatherinfo['current_observation']['pressure_trend'] == '0':
trendinfo = 'constant'
elif weatherinfo['current_observation']['pressure_trend'] == '':
trendinfo = 'N/C'
rain = weatherinfo['current_observation']['precip_today_string']
wind = weatherinfo['current_observation']['wind_string']
vis = weatherinfo['current_observation']['visibility_mi'] + " miles."
sunrise = astroinfo['sun_phase']['sunrise']['hour'] + ":" + astroinfo['sun_phase']['sunrise']['minute']
sunset = astroinfo['sun_phase']['sunset']['hour'] + ":" + astroinfo['sun_phase']['sunset']['minute']
moonrise = astroinfo['moon_phase']['moonrise']['hour'] + ":" + astroinfo['moon_phase']['moonrise']['minute']
moonset = astroinfo['moon_phase']['moonset']['hour'] + ":" + astroinfo['moon_phase']['moonset']['minute']
phase = astroinfo['moon_phase']['phaseofMoon']
illum = astroinfo['moon_phase']['percentIlluminated'] + " %"
if 'type' in alertinfo:
alert = alertinfo['alerts']['description']
expir = alertinfo['alerts']['expires']
else:
alert = "No weather alerts or special notices for this region at this time."
expir = ""
weaticon = weatherinfo['current_observation']['icon_url']
wstation = weatherinfo['current_observation']['history_url']
wunderground = weatherinfo['current_observation']['image']['url']
f = open('/www/weather/index.php', 'w')
f.write('<?php\n')
f.write('\n')
f.write('?>\n')
f.write('<html>\n')
f.write('<head>\n')
f.write('<title>Current Weather</title>\n')
f.write('</head>\n')
f.write('<body bgcolor=efefef alink=yellow vlink=yellow link=yellow>\n')
f.write('<center>\n')
f.write('<table border=1>\n')
f.write('<tr bgcolor=363663><td colspan=2 align=center><font color=e6e6ff><b>Current Weather at my house: <a href='+wstation+'>'+loc+'</a></b></font></td></tr>\n')
f.write('<tr bgcolor=363663><td colspan=2 align=center><font color=e6e6ff><b>'+updt+'</b></font></td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Conditions</b></td><td align=center><img src='+weaticon+'><br>'+cond+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Temperature</b></td><td> '+temps+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Feels Like</b></td><td> '+feels+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Windchill</b></td><td> '+windchill+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Heat Index</b></td><td> '+heat+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Dewpoint</b></td><td> '+dew+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Relative Humidity</b></td><td> '+humidity+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Pressure</b></td><td> '+pressure+' and trending '+trendinfo+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Rainfall</b></td><td> '+rain+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Wind</b></td><td> '+wind+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Visibility</b></td><td> '+vis+'</td></tr>\n')
f.write('<tr bgcolor=363663><td colspan=2 align=center><font color=e6e6ff><b>Sun/Moon Information</b></font></td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Sun Rise</b></td><td> '+sunrise+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Sun Set</b></td><td> '+sunset+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Moon Rise</b></td><td>' +moonrise+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Moon Set</b></td><td> '+moonset+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Moon Phase</b></td><td> '+phase+'</td></tr>\n')
f.write('<tr bgcolor=efefef><td width=30%><b>Illuminiation</b></td><td> '+illum+'</td></tr>\n')
f.write('<tr bgcolor=363663><td colspan=2 align=center><font color=e6e6ff><b>Warnings?</b></font></td></tr>\n')
f.write('<tr bgcolor=efefef><td colspan=2>'+alert+'<p>'+expir+'</td></tr>\n')
f.write('</table>\n')
f.write('<p>\n')
f.write('Weather information brought to you by:<p>\n')
f.write('<a href=https://www.wunderground.com/?apiref=195759c3816c5574><img src='+wunderground+'></a>\n')
f.write('</center>\n')
f.write('</body>\n')
f.write('</html>\n')
f.close()
exit()
Index.php - Example Output
PHP<?php
?>
<html>
<head>
<title>Current Weather</title>
</head>
<body bgcolor=efefef alink=yellow vlink=yellow link=yellow>
<center>
<table border=1>
<tr bgcolor=363663><td colspan=2 align=center><font color=e6e6ff><b>Current Weather at my house: <a href=http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KKYLOUIS68>Indian Springs, Louisville, Kentucky</a></b></font></td></tr>
<tr bgcolor=363663><td colspan=2 align=center><font color=e6e6ff><b>Last Updated on February 14, 10:41 AM EST</b></font></td></tr>
<tr bgcolor=efefef><td width=30%><b>Conditions</b></td><td align=center><img src=http://icons.wxug.com/i/c/k/cloudy.gif><br>Overcast</td></tr>
<tr bgcolor=efefef><td width=30%><b>Temperature</b></td><td> 45.0 F (7.2 C)</td></tr>
<tr bgcolor=efefef><td width=30%><b>Feels Like</b></td><td> 45 F (7 C)</td></tr>
<tr bgcolor=efefef><td width=30%><b>Windchill</b></td><td> 45 F (7 C)</td></tr>
<tr bgcolor=efefef><td width=30%><b>Heat Index</b></td><td> NA</td></tr>
<tr bgcolor=efefef><td width=30%><b>Dewpoint</b></td><td> 27 F (-3 C)</td></tr>
<tr bgcolor=efefef><td width=30%><b>Relative Humidity</b></td><td> 53%</td></tr>
<tr bgcolor=efefef><td width=30%><b>Pressure</b></td><td> 30.01 and trending downwards</td></tr>
<tr bgcolor=efefef><td width=30%><b>Rainfall</b></td><td> -999.00 in (-25375 mm)</td></tr>
<tr bgcolor=efefef><td width=30%><b>Wind</b></td><td> From the West at 1.2 MPH</td></tr>
<tr bgcolor=efefef><td width=30%><b>Visibility</b></td><td> 10.0 miles.</td></tr>
<tr bgcolor=363663><td colspan=2 align=center><font color=e6e6ff><b>Sun/Moon Information</b></font></td></tr>
<tr bgcolor=efefef><td width=30%><b>Sun Rise</b></td><td> 7:33</td></tr>
<tr bgcolor=efefef><td width=30%><b>Sun Set</b></td><td> 18:19</td></tr>
<tr bgcolor=efefef><td width=30%><b>Moon Rise</b></td><td>22:15</td></tr>
<tr bgcolor=efefef><td width=30%><b>Moon Set</b></td><td> 9:38</td></tr>
<tr bgcolor=efefef><td width=30%><b>Moon Phase</b></td><td> Waning Gibbous</td></tr>
<tr bgcolor=efefef><td width=30%><b>Illuminiation</b></td><td> 86 %</td></tr>
<tr bgcolor=363663><td colspan=2 align=center><font color=e6e6ff><b>Warnings?</b></font></td></tr>
<tr bgcolor=efefef><td colspan=2>No weather alerts or special notices for this region at this time.<p></td></tr>
</table>
<p>
Weather information brought to you by:<p>
<a href=https://www.wunderground.com/?apiref=195759c3816c5574><img src=http://icons.wxug.com/graphics/wu2/logo_130x80.png></a>
</center>
</body>
</html>
phpweather.sh
SH#!/bin/ash
rm /www/weather/index.php
python /Shared/scripts/phpweather.py
exit 0
Comments