Brad Buskey
Published © GPL3+

Using Python and PHP to Pull the Weather!

Combining python3, php7 and crontab, dynamically create a PHP page that shows the weather for a certain Personal Weather Station.

BeginnerShowcase (no instructions)15 minutes920
Using Python and PHP to Pull the Weather!

Things used in this project

Hardware components

Omega2 Plus
Onion Corporation Omega2 Plus
×1
Expansion Dock
Onion Corporation Expansion Dock
×1

Software apps and online services

Weather Underground

Story

Read more

Schematics

Example Output

Here is what the web page actually looks like.

Code

OPKG Installs

Plain text
These are the packages I believe are required for this to work. I may or may not have left any off. Once you have the installs completed, you will need to visit the following in order to make sure you have PHP up and running on the web server.
https://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
This is the meat of the script. You will need your own API key from Weather Underground (linked in the description) and you will need to know what PWS to use. I will leave my PWS in the script below, but not my API Key. The file path for Python creating the file is assuming you have installed PHP and have it configured as well as have the directory set as /www/weather/index.php. Change that path to match yours (Line 52)
#!/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
Here is just an example of the output from the script. It is a very simple table, and could be done with HTML, however my plans are to continue development on this and wanted to go ahead and get it set up as a PHP. This does not need to be copied/pasted or created by you, the Python script will create this.
<?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
This is the shell script that will remove the old file and call the python script to create the new one. Do not forget to chmod +x on this file. Also, you will need to put the scripts into your own location. These show where mine are going. I use /Shared/scripts as a Samba share so I can easily access and edit them with a more robust text editor.
#!/bin/ash
rm /www/weather/index.php
python /Shared/scripts/phpweather.py
exit 0

Credits

Brad Buskey

Brad Buskey

6 projects • 14 followers
Just getting started.. Currently have the Onion Omega 2+, Raspberry Pi 3 B +, Raspberry Pi Zero W. Working in Python.

Comments