Close

Writing the Code

A project log for Raspberry Pi Weather App

Using the Raspberry Pi to get weather from the internet

Vaneh BoghosianVaneh Boghosian 04/16/2018 at 22:320 Comments

Create an Open Weather Map account to receive a key. This key will allow your code to access the weather of different locations available on the Open Weather Map website.

(My key is 9276659d9dc88d95bfbd5db39938c052)

To be able to use Open Weather Map, their library must first be downloaded.

Steps to downloading the pyowm library:

  1. Open the raspberry pi terminal on the computer
  2. In the terminal type either sudo pip3.2 install pyowm or sudo pip3 install pyowm depending on which version is installed on the pi. (I used the sudo pip3 install pyowm one)

Once the library has been downloaded it is time to write the code!

First we must import the library we just downloaded so we type:

import pyowm

Next we insert the key into the code which you either created an account to get or are using mine:

owm = pyowm.OWM('9276659d9dc88d95bfbd5db39938c052')

*Make sure the key is written in between apostrophes

To find the weather at a specific location, we use the weather_at_place function that is in the pyowm library. The city and the country must be written in the format  (“City, Country”). The code below is an example of trying to find the weather at the city of Pasadena which is in the United States. If there are cities with the same name, be sure to specify the location. For example, typing in (“Glendale, US”) gives the weather of the Glendale in Arizona state. To get the weather information of the Glendale city that is in California, the city’s direction is specified so it is written as (“North Glendale, US”)

The following code shows the weather in Pasadena, United States.

observation= owm.weather_at_place("Pasadena, US")

Fetch the weather information in the specified location and set the data found to a variable

w= observation.get_weather()

There are many different types of information we can choose to receive.

We can get the wind speed by adding a function to the variable we are storing the weather information into; you take the variable you stored the weather information into which in my case was w, and add the function after it to get the wind speed which is .get_wind(). You store this information into a variable (I chose to name this variable wind).

wind= w.get_wind()

To get the temperature in fahrenheit you follow the same steps as above but this time the function to get the temperature is get_temperature() and inside the parentheses specify whether you want the temperature in fahrenheit or celsius.

temperature= w.get_temperature('fahrenheit')

When you run this function, the maximum and minimum temperature is also printed out. Let’s say you just want the temperature in fahrenheit and nothing else. Create a new variable and put temperature.get('temp'). The .get() function returns to you a value for the given key. So when we put .get('temp'), this will return to us only the value of the temperature.

temp_value= temperature.get('temp')

To print out what we want, we use the print() function. Inside the parenthesis we write the variable name that we want information from.

print(w) prints out the reference time

print(wind) prints the speed and degree of the wind

print(temperature) prints out the temperature as well as the highest and lowest temperatures of the day.

Discussions