Rain Study 2 © by amandabhslater
Currently my wife and I are working on the house and decided to redo the front and back yards; they are what we love most about our house. We hired someone who knows a lot more about plants to help pick out what to plant and where. She has come up with some fun and neat ideas including lots of ferns, which I love. Georgia gets lots of rain in the fall, winter, and spring but summers can be very hit or miss. So our landscape architect gave me a rule for watering.
If the rainfall has been less than 1 inch in the last three days then water the garden. Wait three more days and repeat the same logic.
As soon as she told me this algorithm lights went off that this would work as great as a simple script that email’s me to water my new garden. I am also a new owner of a raspyberry pi (details later), which will work perfectly to do the monitoring and notifying.
Here is what I am currently thinking for the setup:
Python 2.7 using the following library’s
Here is what I have coded so far:
Created on Sep 12, 2012
from bs4 import BeautifulSoup
from bs4 import SoupStrainer
MORNINGSIDE_STATION = 'KGAATLAN54'
BASE_URL = 'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID='
print 'Retrieving rainfall from ' + station
weatherData = urllib2.urlopen(BASE_URL+station)
html = weatherData.read()
onlyTables = SoupStrainer(class_="contentData")
soup = BeautifulSoup(html.encode("utf8"), "lxml", parse_only=onlyTables)
contentTable = soup.find(text='Precipitation:')
contentTable = contentTable.find_parent('tr')
inchStr = contentTable.find(class_='b')
rainfall = float(inchStr.contents.string)
date = datetime.date.today()
iso = date.isoformat()
print 'Station reporting ' + str(inches) + ' inches on ' + iso
sql = "INSERT INTO Rainfall(Inches, Date) VALUES('"+str(inches)+"','"+str(iso)+"');"
db = sqlite3.connect('rainfall.db')
if __name__ == '__main__':
fetchRainfall uses the urllib to get the html page from wunderground.com. I limit my soup parsing to one contentData div on the site since it contains the actual data I care about. From there I found the first “Percipitation:” text node (note the second text node is the months total which is not what I wanted.) From there it was pretty simple the get the parent row and then from the parent dig back down to the reported inch level.
recordRainfall puts the inch level and date into a simple sql database for analysis. Note I configured the db and tables outside this script and if you want to run this example so will you.
This is just the start of the script, but I figured I should post this to give people ideas on how to get daily rain fall levels using python.
Future task include:
- Setup a github repo
- Setting up the logic for sending emails
- Making it generic for all weather stations
- Install scripts
The above script is current running as a cron job on my macbook at 11:55pm to collect the most recent rainfall levels for that day. I will eventually make it to were I can run the script on the next day, but get the rainfall levels for the previous day. This would allow me to collect the full 24 hours 0-24 data instead of data from 0 – 11:55. In Georgia 5 minutes can really be a 1/4 inch, so it is important.
Let me know what you find out about this script when you play around with it.