There was a time when we were dependent on different mediums for different types of information. We had our radios for music, news, and weather reports, libraries for books and internet cafe for the internet. As technology is advancing every day, people and things are getting connected.
We can see weather forecasting in our smartphones, that is, of course, a feature of our smartphone but what if we want to do it by ourselves, or we want to know how our smartphone fetches weather forecast from all around the world. Well not on a smartphone but we can always learn all this fun stuff on our favourite Raspberry Pi.
In this article, we are going to fetch weather data of our city into our Raspberry Pi.
Things we need
- Raspberry Pi
- Power Supply (5v, 2.5 Amps),
- Internet Connection,
- Weatherbit account
How it works??
Gathering real-time weather data is difficult for us, so we use a third party who can provide us with weather reports. There are lots of APIs but most of them are paid, Weatherbit provides us with a free API for basic usage. Weatherbit collects weather data from metrological departments and stores it in there databases. Then there are APIs which helps us connect with the database. The APIs take some parameters, filter our data and responds with data we asked for.
With a working internet connection, our Raspberry Pi can make an HTTP call and call a web API. The parameters of API can be a city name or ZIP code etc.
Configure Weatherbit
In your browser open www.weatherbit.io, and Sign up with your email address.
Login in with your account and you will be redirected to https://www.weatherbit.io/account/dashboard, copy your API key and paste it in the code below
Go to API Docs, click on API Documentation you will be redirected to https://www.weatherbit.io/api where you can find multiple APIs for different types of weather data. For this article, I’m using ‘Current Weather API’.
Go to Current Weather API documentation www.weatherbit.io/api/weather-current
In this document, you will find parameters of API and API callback format. You can extract data of your choice from the callback.
API requests
There are various ways to get current weather data of any city with an API callback. You can provide geographical coordinates of the city, city name, postal code, city ids, airport ICAO or station ID.
We will send an HTTP GET request with formatted URL which will give us data for our city.
The format of the URL is-
https://api.weatherbit.io/v2.0/current?city=<CityName>&country=<Country>&key=<API_KEY>
Python Code
Python’s request module is used for sending a GET request to Weatherbit API. The URL is formated by iterating over dictionary keys and values. The API’s callback is in JSON format so we used Data.json() to extract JSON data from the callback.
import requests
from pprint import pprint
Key = 'Your API key'
params = {
'city': 'london',
'country' : 'UK',
'units' : 'M'}
BASE_URL = "https://api.weatherbit.io/v2.0/current?"
for key value in params.items():
BASE_URL += key + '=' + value + '&'
BASE_URL += 'key={}'.format(Key)
Data = requests.get(BASE_URL)
Weather = Data.json()
pprint(Weather)
The GET request will return data in the following format
The callback contains an object ‘data’ which contains all the data of the city. The data object includes temperature, UV index, AQI, wind direction, precipitation, sunrise and sunset time. We can extract data of our concern and leave the rest of unused data.
Conclusion
In the article, we learned how we can fetch weather data into our device. We learned how to use APIs to fetch data, to extract data from API response.
Now that we have our data, we can use this weather forecast for our projects. We can also connect an LCD display and display weather data in the LCD display.
Have fun using your own weather station.
Share your weather forecaster inventions with us on and
Hope you enjoyed this article..
Check out our Raspberry Pi 4 Clear Case
We have recently launched: Stem:Bit - The Programmable Blocks Kit for micro:bit