How’s the weather? OpenWeatherMap API access and more
Have you ever wondered how smart speakers and other IoT devices – as well as our phones, tablets, smart watches, and other web-enabled electronics – know what the weather looks like? If you dig into the code, there’s a strong chance that – somewhere in the workflow – you’ll find a call to an application programming interface (API). And a great way to find out how APIs work is to try one for yourself, such as the OpenWeatherMap API.
Weather APIs enable apps, business tools, and other software – which may be coded in a different language to the target data source – to retrieve information such as a weather forecast and current temperature. Many businesses can be affected by the weather – either favorably or unfavorably – with profits linked to various climate conditions. And so, getting a weekly, daily, or hourly forecast can turn out to be useful – even more so if the process can be automated.
OpenWeather describes a wide range of business uses for its API – for example, owners of an amusement park may wish to raise their Google AdWords bids when the weather is sunny to help increase visitor numbers. And this could be automated by OpenWeatherMap API calls. Insurance firms could query historical weather data to validate customer claims. Energy operators benefit from weather information as the data helps to predict demand for heating and forecast electricity use.
There are many ways in which weather data can improve business products and services. Route planning is another example. Vehicles could be redirected onto higher roads when heavy rain is forecast, to avoid potential flooding.
Getting started with APIs
To get started with APIs, the first step is often to register and obtain an API key. This is used by the service provider to identify the application or program that is making the call. OpenWeather has a free tier, but there are also paid for options, and the API key will be linked to the service that has been chosen by the user.
For this example, we’ll be using OpenWeather’s free tier, which has some restrictions that users will need to observe. But the service still has much to offer, such as the capability to make 60 API calls per minute and up to 1 million API calls per month. Other features of the OpenWeather API free tier, include access to 5-day weather forecasts for any location on the planet, described in 3-hour time-steps.
Creating a new OpenWeather account is straightforward and takes a minute or so. In our case, the email confirmation arrived promptly, and we could grab the API key from the account dashboard.
The next step is to code up our API request, and we’ll be using Python for that, making use of requests and json modules (hat tip to geeksforgeeks.org).
Easy Python access via the web
Don’t worry if Python isn’t available directly on your machine. You can simply fire up a colab notebook (Google’s version of Jupyter Labs) on your browser to follow along with our OpenWeather API example. We’ll be investigating the current temperatures in Bristol, UK; Kuala Lumpur, Malaysia; Philadelphia, US; and Sydney, Australia. The locations represent Hybrid (publisher of TechHQ, and a range of other websites) offices around the world.
Below is the code we used for the OpenWeather API, in text format, making it easy to cut and paste. Replace the x’s with the values of your API key.
import requests, json API_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" latitude = 51.45523 longitude = -2.59665 Base_URL_for_API_call = "https://api.openweathermap.org/data/2.5/weather?" Final_URL = Base_URL_for_API_call + "appid=" + str(API_key) + "&lat=" + str(latitude) + "&lon=" + str(longitude) print(Final_URL) #send API call and get results results = requests.get(Final_URL) data = results.json() print(data) #extract temperature value from Python dictionary main = data.get("main") print(main.get('temp'))
Running the Python code to make the API call and receive the related response (swapping in the various longitude and latitude values for each of the office locations), you get the following:
- Bristol, 3.5 degC (afternoon), 1.3 degC (evening)
- Kuala Lumpur, 25.9 degC (night)
- Philadelphia, 5.6 degC (morning)
- Sydney, 19.6 degC (night)
As mentioned, there’s a wealth of other weather information available in the JSON response to the OpenWeather API call. For example, in Bristol, there are broken clouds, whereas Sydney has clear skies. Getting hands-on with APIs is a great way to familiarize yourself with what’s possible. And the experience will no doubt inspire application ideas and other ways in which weather data – as well as other information sources – can be integrated into business tools and devices.
Perhaps you now fancy creating an API-powered IoT smart irrigation system? Or maybe integrating weather data into a customized chatbot or personal assistant? There’s a long list of ideas out there. And that’s just weather data. APIs enable automated access to a wealth of information. What’s more, programs can run in the background, leaving users free to do other things such as enjoying the sunshine, if they are lucky.
27 January 2023
27 January 2023
27 January 2023