The Raspberry Pi Weather Texter uses Python to obtain weather information, determine what I should probably wear, and then text me all of the information six every morning (when I get up for school). This following tutorial will allow your Pi to notify you daily on weather and suggested wear.

The following Python API will be used:

I’m assuming that you have set up the Pi with Raspbian Wheezy and have Internet set up. If not, check out this tutorial.

Step 1: Enable SSH

Before we start, we will want to enable SSH if it is not enabled already. Hook up the Pi to an external screen such as a TV. In the terminal of the Raspberry Pi, type in sudo raspi-config, go down to SSH, press enter, go down to Enable, and press OK.

Once you are back in the terminal, you will need to find your IP address. This can be done by typing in hostname -I.

You will no longer need the external screen.

Using your SSH software (for Windows you will have to install PuTTY, while Linux/Mac users can use the Terminal), log into the Raspberry Pi from your computer. PuTTY will just have to type in the IP address in the field next to the port (which should be 22); Terminal users will have to type in pi@INSERT_IP_ADDRESS_HERE. Once you press enter, you will reach a rather basic login screen:

If you haven’t changed the username already, it will by pi by default. So, type in pi and press enter. The password, if not changed, will be raspberry, so type raspberry and press enter.

You will see some Linux messages and then see a command prompt:

Step 2: Install APIs

Run the following commands in the Terminal if you do not have Python/Pip set up on your Raspberry Pi:

sudo apt-get install python-setuptools

sudo easy_install pip

Once these commands are finished, we shall install the Twilio API by typing in the following command:

sudo easy_install twilio

Step 3: Initialize Twilio

Create an account at Twilio and go through the setup. When asked to verify the Twilio account, make sure to use the number that will be receiving the texts from the Raspberry Pi. You will receive a Twilio number. Keep that in a safe place and click Get Started then Go To Your Account. Here, you will be able to find your Account SID as well as your Authorization Token.

Step 4: Python Coding

Either transfer the following code to the Raspberry Pi via FTP, or type it in manually in SSH. Either way, you will have to make the edits needed along the way:

#!/usr/bin/env python #Special thanks to python-calatin for the #Blog entry on obtaining weather data from Yahoo #Link: http://python-catalin.blogspot.com/2011/02/just-simple-python-weather-script.html import urllib from xml.dom import minidom from twython import Twython from twilio.rest import TwilioRestClient account_sid = "ACCOUNT_SID" # Insert your Twilio ID auth_token = "AUTH_TOKEN" #Insert your Twilio Token client = TwilioRestClient(account_sid, auth_token) wurl = 'http://xml.weather.yahoo.com/forecastrss?p=%s' # weather rss wser = 'http://xml.weather.yahoo.com/ns/rss/1.0' def weather_for_zip(zip_code): url = wurl % zip_code +'&u=c' dom = minidom.parse(urllib.urlopen(url)) forecasts = [] for node in dom.getElementsByTagNameNS(wser, 'forecast'): forecasts.append({ 'high': node.getAttribute('high'), # gets high temperature attribute }) ycondition = dom.getElementsByTagNameNS(wser, 'condition')[0] return { 'forecasts': forecasts , 'title': dom.getElementsByTagName('title')[0].firstChild.data } a=weather_for_zip("60126") # Insert your zip code here hight = a['forecasts'][0]['high'] # Defines hight as the high temperature temp = int(hight) * 9/5 + 32 # Converts from Celsius to Fahrenheit if temp < 60: # The sixty and the other numbers can be changed to user's preferences. message = client.messages.create(body="I recommend wearing something slightly warm... It is only " + str(temp) + " degrees!", to="NUMBER RECEIVING TEXTS", # Replace with number receiving texts from_="TWILIO NUMBER") # Replace with Twilio number message = client.messages.create(body="May I suggest a hoodie?", to="NUMBER RECEIVING TEXTS", # Replace with number receiving texts from_="TWILIO NUMBER") # Replace with Twilio number elif temp >= 60 and temp < 85: message = client.messages.create(body="It's going to be warm today at " + str(temp) + " degrees!", to="NUMBER RECEIVING TEXTS", # Replace with number receiving texts from_="TWILIO NUMBER") # Replace with Twilio number message = client.messages.create(body="I suggest wearing a button down shirt with your Calvin Klein Jeans!", to="NUMBER RECEIVING TEXTS", # Replace with number receiving texts from_="TWILIO NUMBER") # Replace with Twilio number else: message = client.messages.create(body="It's going to be hot today at " + str(temp) + " degrees!", to="NUMBER RECEIVING TEXTS", # Replace with number receiving texts from_="TWILIO NUMBER") message = client.messages.create(body="Perhaps a t-shirt and shorts will suffice?", to="NUMBER RECEIVING TEXTS7", # Replace with number receiving texts from_="TWILIO NUMBER") # Replace with Twilio number

The number should be in a format like this: +(Country Code)(Area Code)(Rest of Phone Number). For example:

+15555555555

Make sure to place the file in a spot that you will remember, such as the Desktop.

Step 5: Test the Code

In SSH, type the following code:

python weather.py

Make sure you are in the same directory as the file when you run. For example, if the file is on the Desktop and you are in the ~ directory, type in cd Desktop to get to the Desktop.

Run the code by pressing enter. You should be able to get a text from the Raspberry Pi stating the high temperature and what you should wear.

Step 6: Run it Daily

I want the Python program to run daily at 6 am. To do this, we must do two things: set the correct time zone and set up a cron job to run daily.

In order to do this, type in the following code and press tab:

sudo cp /usr/share/zoneinfo

You should see a bunch of subfolders:

Keep typing in the correct subfolder and tab until you have the correct time zone or closest city. For example, I would have typed in:

sudo cp /usr/share/zoneinfo/America/Chicago

Add the following to to the end (with a space at the beginning):

/etc/localtime

My line should look like this:

sudo cp /usr/share/zoneinfo/America/Chicago /etc/localtime

Press enter to confirm the changes. Then type in sudo crontab -e. This will open up crontab, which runs tasks at certain times. Scroll down to the bottom of the document, and type in the following:

0 6 * * * python /path/to/program/weather.py

Insert the path to your program where it says /path/to/program. If your program is on the Desktop (like mine is), you should have typed this:

0 6 * * * python /home/pi/Desktop/weather.py

You can change the 0 to the certain minute (0-59) and 6 to the certain hour (0-23) you want the program to run.

Press Ctrl-X to exit and Y to save. You should see the following message:

That’s it! The program will now run once at a day at the time specified!

I did want to provide special thanks to python-catalin for the Simple Python script! You can edit my script and add more parameters such as current temperature and conditions. Just click here for helpful information!