How to get weather information from Open Weather Map with Your Raspberry Pi.

Weeks ago I have been working in a small project with my Raspberry Pi which consist to get the weather details from my city (Loja, Ecuador) and show them in the shell of linux. It’s very simple.

Open Weather Map (OWM)is one of the most used online services for weather details. It has a free plan and an active community of developers so you can find a lot of information.

The Code

In your Raspberry Pi you need to type the next command:

$ pip install pyowm

This command will install a pyhton client (PyOWN) for Open Weather Map

Now you will need the Api Key from OWM. This step is very simple, you can follow the next tutorial to get it.

Now create a pyhton script and copy and paste the follow code

import pyowm 
# You MUST provide a valid API key
owm = pyowm.OWM('xxxxxxxxxxxxxxxxxxxx')
# Here put your city and Country ISO 3166 country codes
observation = owm.weather_at_place('Loja,EC')
w = observation.get_weather()
# Weather details from INTERNET 
wind = w.get_wind()
speed = wind ["speed"]
deg = wind ["deg"]
humidity = w.get_humidity()
temperature = w.get_temperature('celsius')
temp = temperature["temp"]
rain = w.get_rain()

#If there is no data recorded from rain then return 0, otherwise #return the actual data
if len(rain) == 0: 
lastrain = 0
lastrain = rain["3h"]
# Print the data 
print "The velocity of the air is: " + str(speed) + " m/s" 
print "The wind direction is: " + str(deg) + " Degrees"
print "The humidity is: " + str(humidity) + " %"
print "The temperature is: " + str(temp) + " Celcius"
print "The precipitation is: " + str(lastrain) + " mm"

Then when you run the python script, if everything is ok, you will see something like this:

And that is all. Like I said before is very simple. I hope this will be useful in your projects. Enjoy it!