Create a Weather Texting Python Script

Weather SMS

Advertisements

For this blog post, it will be a little different, as this just consists of a 49 line python script. I wanted to create a simple script that would text me the weather in my area each morning when I woke up. Although I normally create projects in C#, I choose to do this in python, as it would both be simpler, while also allowing more flexibility in how data was sent across multiple APIs. The full code for this post can be found at this Github repo.

The first step in sending an SMS was to sign up for Twilio. It is important to note however, that while they do give you some trial money to send messages, you will have to eventually pay for their service if you send enough messages. The good news is currently, they give you around $15.00, and it only costs 0.01¢ per SMS.

Once you sign up for their service, you will have two items you will need to use for your python script, your account id and token. Once you have those, you can input them into the code like so.

import pyowm, json
from twilio.rest import Client

#Twilio settings
account = 'ACCOUNT ID'
token = 'TOKEN ID'

Next, we need to get the weather data in our area. To do this, we will use pyowm, which is a wrapper of OpenWeatherMap. Once you have the API key from OpenWeatherMap, you can input it in the code below.

#API key for pyowm
owm = pyowm.OWM('API KEY')

Now that all of our configurations have been set up, we can now start pulling and formatting data to send in a SMS. The first step is getting all weather data from the location we want to get the weather from. In python this is very easy and can be done in one line.

#Get location data for the relevent area
observation = owm.weather_at_place('CITY,COUNTRY')

It is important to note, that for the COUNTRY tag, you have to use your country abbreviations. For example, if you live in the United States, use US.

Next, we get the weather data from the location we specified earlier using the command below.

#Store relevent data from the declared place
w = observation.get_weather()

Now that we have our data, we must sort and format it. For our project, we only want four items, the current temp, the min and max temp for the day, and the wind gust. To do this, we just call two functions.

#Weather details
wind = w.get_wind()
temp = w.get_temperature('fahrenheit')

If we were to print wind and temp right now, the data would be in JSON format. To fix this, we must use the json package within python. First we dump our data into the json dump function, then load that data to further sort and fix the data.

#Dump and load json string
windDump = json.dumps(wind)
windLoad = json.loads(windDump)

Now we just specify what data of the json line that we want to keep and store.

#Store speed number
speed = windLoad["speed"]

Next, we will do the same thing for the temperature, splitting the output into three numbers, one for the current temp, one for the max temp, and one for the min temp.

#Dump and load jason string
tempDump = json.dumps(temp)
tempLoad = json.loads(tempDump)

#Store final, max, and min temp
tempFinal = tempLoad["temp"]
tempMax = tempLoad["temp_max"]
tempMin = tempLoad["temp_min"]

Now we are ready to format our final message to be displayed over SMS. To do this, we will concatenation our final message string to fit all relevant data, using new line characters for better formatting.

#Create the final message to display
finalMessage = ("Weather for CITY" 
    + "\n\nCurrent Temp: " + str(tempFinal) 
    + "\n\nMax Temp: " + str(tempMax) 
    + "\n\nMin Temp: " + str(tempMin) 
    + "\n\nWind Speed: " + str(speed))

Finally, we just have to declare our Twilio client and send our message. This last bit is very easy with the Twilio API, and can be done with the following lines.

#Use twilio client
client = Client(account, token)

#Send Message
client.messages.create(from_='TWILIO NUMBER', to='YOUR NUMBER', 
    body=finalMessage)

We then get an end result like this.

The whole code can be found below.

import pyowm, json
from twilio.rest import Client

#Twilio settings
account = 'ACCOUNT ID'
token = 'TOKEN ID'

#API key for pyowm
owm = pyowm.OWM('API KEY')

#Get location data for the relevent area
observation = owm.weather_at_place('CITY,COUNTRY')

#Store relevent data from the declared place
w = observation.get_weather()

#Weather details
wind = w.get_wind()
temp = w.get_temperature('fahrenheit')

#Dump and load json string
windDump = json.dumps(wind)
windLoad = json.loads(windDump)

#Store speed number
speed = windLoad["speed"]

#Dump and load jason string
tempDump = json.dumps(temp)
tempLoad = json.loads(tempDump)

#Store final, max, and min temp
tempFinal = tempLoad["temp"]
tempMax = tempLoad["temp_max"]
tempMin = tempLoad["temp_min"]

#Create the final message to display
finalMessage = ("Weather for CITY" 
    + "\n\nCurrent Temp: " + str(tempFinal) 
    + "\n\nMax Temp: " + str(tempMax) 
    + "\n\nMin Temp: " + str(tempMin) 
    + "\n\nWind Speed: " + str(speed))

#Use twilio client
client = Client(account, token)

#Send Message
client.messages.create(from_='TWILIO NUMBER', to='YOUR NUMBER', 
    body=finalMessage)

In order to make this task occur daily, I followed this guide, where you would just create a Bat file to run the python script, and create a process in Windows Task Scheduler.

As you can see, this whole process is fairly easy, and can be done in less then 50 lines of python. Although the process is entirely free, Twilio gives you enough capital that if you only send one SMS per day, it can last you 150,000 days.

Advertisements

2 Comments on “Create a Weather Texting Python Script”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: