I have been working on an idea for my project. I am using my BBB to get the weather forecast and to let me know if I need to take an umbrella when I go out. (To be honest I don't use an umbrella but this is is for a "proof of concept" idea).
I am using python here to collect the forecast from Weather Underground, the api page is very helpful, and when I have the forecast as a json document I can extract the data I am looking for and do stuff with it. Currently I am displaying the weather forecast form my local area for the day that the program is being run and the next day. I have coded the place that I want the weather for into the string that makes up the URL, as my project is going to be setup per user I think that this is OK. See code below:
# program to grab the weather forecast from weather underground # author Mark Venn # based on code from "Getting Started with Raspberry Pi" # currently collects the forecast for current day and next day. Lights an LED if the chances of rain are over 60% # imports import requests import Adafruit_BBIO.GPIO as GPIO import time # configure GPIO pins GPIO.setup("P8_8",GPIO.OUT) GPIO.output("P8_8",GPIO.LOW) # wunderground api key key = 'xxxxxxxxxxxxxxxx' # build string using hard coded web address & api key variable ApiUrl = 'http://api.wunderground.com/api/' + key + '/geolookup/forecast/q/UK/Bembridge.json' while True: # get the weather forecast from wunderground and store in variable r = requests.get(ApiUrl) # take response and parse it into python dictionary object forecast = r.json # extract relevant data. # use variable to collect day of the week day = forecast['forecast']['txt_forecast']['forecastday'][0]['title'] # display next day nextDay = forecast['forecast']['txt_forecast']['forecastday'][2]['title'] # chance of rain rain = forecast['forecast']['txt_forecast']['forecastday'][0]['pop'] rain = int(rain) # display current day print day #directly display the required info print forecast['forecast']['txt_forecast']['forecastday'][0]['fcttext'] print nextDay # TEST CODE # print rain #directly display the required info print forecast['forecast']['txt_forecast']['forecastday'][2]['fcttext'] if rain >= 60: GPIO.output("P8_8",GPIO.HIGH) else: GPIO.output("P8_8",GPIO.LOW) time.sleep(180) # TEST CODE # # prints entire json document #print forecast
The data I need for the forecast is held in fcttext in 2 key / value pairs and this is displayed.
root@beaglebone:~# python text_forecast_v3.py
Monday
Mostly cloudy. Lows overnight in the low 40s.
Tuesday
Cloudy and becoming windy. Periods of light rain early. Turning colder. High 46F. Winds N at 20 to 30 mph. Chance of rain 40%.
Monday
Mostly cloudy. Lows overnight in the low 40s.
Tuesday
Cloudy and becoming windy. Periods of light rain early. Turning colder. High 46F. Winds N at 20 to 30 mph. Chance of rain 40%.
Monday
Cloudy. Lows overnight in the low 40s.
Tuesday
Light rain early. Then becoming windy in the afternoon. Turning colder. High 47F. Winds N at 20 to 30 mph. Chance of rain 40%.
I am displaying this in the console at the moment, I might use some kind of GUI at a later date; my son odion is often telling me that I should put stuff into GUIs but A console is good enough for me for now
The code to get the forecast could be run on any machine that can run python 2.7 but now we come to the reason for running this on the BBB. There is a single LED connected to P8-8 and when the value of the precipitation chance in percentage is over 60% this LED lights up. The whole check is done every 3 minutes at the moment but I will be playing with the timings later. It is likely that I can get away with every 6 hours or so but I will see.
I have taken the idea for this from the "Getting Started with Raspberry Pi" book and made a few changes as well as converting it to the BBB; I will probably set this up on the Pi next and see if there are any differences in the way it works.
The Weather Underground api looks very wide ranging and I will see what else I can get out of it, I think that as we in the UK head into winter something that pulls the expected maximum and minimum temperatures will be useful. I have found where this info is held in the json document:
"period":1,
"high": {
"fahrenheit":"48",
"celsius":"8"
},
"low": {
"fahrenheit":"41",
"celsius":"5"
}
Will have to play around with this info and see how I can make use of it.
I will be using my BB_VIEW display to see how I can display the info, not sure what or how yet, this will be in another blog. I am going to be writing about my experiences with my new Pi B+ so check out my blogs on there.
Bye for now, off to check if my LED is alight yet!
Top Comments