Brad Buskey
Published © GPL3+

GetWeather For Omega2+

Pull weather information from WeatherUnderground and display it on the Onion Omega 2+ OLED.

BeginnerWork in progress15 minutes3,705
GetWeather For Omega2+

Things used in this project

Hardware components

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

Story

Read more

Code

Initial Setup

Plain text
There is a few things you will need to do in order for this to all work. First and foremost, you will need to have a Weather Underground API Key in order to utilize this script. It is free for the casual developer and can be obtained from the following location: https://www.wunderground.com/weather/api

Below are the modules from OPKG that need to be installed. Also, do not forget to make your shell script executable.
opkg update
opkg install python3 python-light python-urllib3 pyOledExp pyOmegaExpansion oled-exp ogps

chmod +x getweather.sh

getweather.sh

SH
This script calls the GPS chip to get the current location, writes that to a file (just like the GetGPS script) and then calls the showweather.py script that does the meat of the work. Do not forget to change this file to be an executable file with the "chmod +x getweather.sh" command.
#!/bin/ash

# Remove the original gpsweather.txt. Start with fresh file.
rm /root/gpsweather.txt

# Get the GPS information and write it to a file.
ubus -S call gps info > /root/gpsweather.txt

# Call the python script to write to the OLED.
python /root/showweather.py

# Exit the script.
exit 0

showweather.py

Python
This script will read GPS info from the chip and using the Lat/Lon and pull the weather data from the nearest weather station and display the requested information on the OLED screen. My own tests have been successful.
#!/usr/bin/env python

#=====================================================
# Pull in the weather information from the Weather
# Underground based on the current GPS Location and
# display the results on the OLED screen.
#
# Written by: Brad Buskey
# Contact me: deckyon@gmail.com
#=====================================================

# Initialize required libraries
from OmegaExpansion import oledExp
import time
import json
import os
import urllib2

# Initialize and clear the OLED expansion module
oledExp.setVerbosity(0)
oledExp.setTextColumns()
oledExp.driverInit()

# Use your API key here.  Get it from https://www.wunderground.com/weather/api
apikey = ""

# Check for the existence of the gpsweather.txt file.
if os.path.getsize('/root/gpsweather.txt') == 0:
	# File is empty or doesnt exist
	oledExp.setCursor(3, 0)
	oledExp.write("GPS Info doesnt seem")
	oledExp.setCursor(4, 0)
	oledExp.write("to exist.")
	oledExp.setCursor(6, 0)
	oledExp.write("Check GPSInfo file.")
	exit()

#Read in the gps information from the file generated by the shell script.
with file('/root/gpsweather.txt', 'r') as gpsfile:
	gpsinfo = json.load(gpsfile)

# Check for the existence of the GPS signal.
if gpsinfo.has_key('signal'):
	# If the GPS chip could not get a signal, then display that and quit.
	oledExp.setCursor(3, 0)
	oledExp.write("No GPS signal found")
	oledExp.setCursor(5, 0)
	oledExp.write("Please try later!")
	exit()
elif gpsinfo.has_key('latitude'):
	# If the location information exists, grab the required lat and lon for the script to continue.
	lat=gpsinfo['latitude']
	lon=gpsinfo['longitude']

# Pull in the data from the Weather Underground website
GetURL = "http://api.wunderground.com/api/"+apikey+"/conditions/q/"+lat+","+lon+".json"
weatherdata = urllib2.urlopen(GetURL)
weatherdict = weatherdata.read()
weatherinfo = json.loads(weatherdict)

# Pull in the information from the URL to put together the output
cty = weatherinfo['current_observation']['display_location']['city']
sta = weatherinfo['current_observation']['display_location']['state']
cur = weatherinfo['current_observation']['weather']
tem = weatherinfo['current_observation']['temperature_string']
wsp = weatherinfo['current_observation']['wind_string']
rai = weatherinfo['current_observation']['precip_today_string']
loc = cty + ", " + sta

# Start writing the data to the OLED display.
oledExp.setCursor(0, 0)
oledExp.write(loc)
oledExp.setCursor(1, 0)
oledExp.write("_____________________")
oledExp.setCursor(2, 0)
oledExp.write(cur)
oledExp.setCursor(3, 0)
oledExp.write(tem)
oledExp.setCursor(4, 0)
oledExp.write(rai)
oledExp.setCursor(5, 0)
oledExp.write(wsp)

# Exit the script cleanly.
exit()

weatherinfo.txt

Plain text
This is an example of what should be pulled down from Weather Underground.
{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "conditions": 1
  }
	}
  ,	"current_observation": {
		"image": {
		"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
		"title":"Weather Underground",
		"link":"http://www.wunderground.com"
		},
		"display_location": {
		"full":"Louisville, KY",
		"city":"Louisville",
		"state":"KY",
		"state_name":"Kentucky",
		"country":"US",
		"country_iso3166":"US",
		"zip":"40201",
		"magic":"1",
		"wmo":"99999",
		"latitude":"38.25000000",
		"longitude":"-85.76000214",
		"elevation":"128.0"
		},
		"observation_location": {
		"full":"Louisville, Kentucky",
		"city":"Louisville",
		"state":"Kentucky",
		"country":"US",
		"country_iso3166":"US",
		"latitude":"38.220798",
		"longitude":"-85.751801",
		"elevation":"491 ft"
		},
		"estimated": {
		},
		"station_id":"ME1143",
		"observation_time":"Last Updated on January 30, 2:16 PM EST",
		"observation_time_rfc822":"Mon, 30 Jan 2017 14:16:39 -0500",
		"observation_epoch":"1485803799",
		"local_time_rfc822":"Mon, 30 Jan 2017 14:28:37 -0500",
		"local_epoch":"1485804517",
		"local_tz_short":"EST",
		"local_tz_long":"America/New_York",
		"local_tz_offset":"-0500",
		"weather":"Clear",
		"temperature_string":"36.0 F (2.2 C)",
		"temp_f":36.0,
		"temp_c":2.2,
		"relative_humidity":"49%",
		"wind_string":"From the SW at 2.0 MPH Gusting to 8.0 MPH",
		"wind_dir":"SW",
		"wind_degrees":219,
		"wind_mph":2.0,
		"wind_gust_mph":"8.0",
		"wind_kph":3.2,
		"wind_gust_kph":"12.9",
		"pressure_mb":"1018",
		"pressure_in":"30.07",
		"pressure_trend":"-",
		"dewpoint_string":"19 F (-7 C)",
		"dewpoint_f":19,
		"dewpoint_c":-7,
		"heat_index_string":"NA",
		"heat_index_f":"NA",
		"heat_index_c":"NA",
		"windchill_string":"36 F (2 C)",
		"windchill_f":"36",
		"windchill_c":"2",
		"feelslike_string":"36 F (2 C)",
		"feelslike_f":"36",
		"feelslike_c":"2",
		"visibility_mi":"10.0",
		"visibility_km":"16.1",
		"solarradiation":"--",
		"UV":"3","precip_1hr_string":"-999.00 in ( 0 mm)",
		"precip_1hr_in":"-999.00",
		"precip_1hr_metric":" 0",
		"precip_today_string":"0.02 in (1 mm)",
		"precip_today_in":"0.02",
		"precip_today_metric":"1",
		"icon":"clear",
		"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
		"forecast_url":"http://www.wunderground.com/US/KY/Louisville.html",
		"history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=ME1143",
		"ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=38.220798,-85.751801",
		"nowcast":""
	}
}

weathergps.txt (No Signal)

Plain text
This is the output if there is no GPS signal. This is the final piece for me to do for this project to work completely. The script will check for this instance of the file, as well as an empty gpsinfo.txt
{"signal":"false"}

weathergps.txt

Plain text
This is the file created by the Shell script and read by the Python script. This is just an example of when there is a good GPS signal and it can get a lock and return the location.
{"age":6357,"latitude":"38.1234","longitude":"-80.1234","elevation":"1234.5","course":"12.34","speed":"N"}

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