Getting & using data from OpenWeatherMap

It is easy to get local or international weather data (such as current temperature, humidity, wind speed & so on) using the OpenWeatherMap API.

This could be very useful to me as part of my bird box project for 2015.

But my immediate concern involves our central heating system, and how to determine the local weather conditions.
At the moment I can't measure the outside temperature using my RaspberryPi server. The unit is just too far from an outside wall, making the cabling route difficult.

OpenWeatherMap and the API

OpenWeatherMap is one of several weather sites offering free access to data via an API (application programming interface). I'm a bit confused because the website mentions an apiKey which is allegedly required, although I don't seem to have had any problems without it (accessing just the basic data).

Edit Nov 2015: as of October 2015 you now need an apiKey

The site is owned by Extreme Electronics Ltd and they provide a number of chargeable support contracts from Developer to Enterprise, in addition to the free API.

All I'm really interested in at the moment is a rough idea of the outside temperature. So just using this web string:-

“http://api.openweathermap.org/data/2.5/weather?q=Horsham,uk&units=metric”

…I can grab metric data for the Horsham area in json format.

Getting & using data from OpenWeatherMap

By manually ‘cleaning up' the incoming string in a text editor (i.e. adding indents and line feeds) we can get a better view of the json data:-

{
“coord”:{
“lon”:-0.33,
“lat”:51.06},
“sys”{
“type”:1,
“id”:5089,
“message”:0.0804,
“country”:”GB”,
“sunrise”:1416814383,
“sunset”:1416844991},
“weather”:[{
“id”:800,
“main”:”Clear”,
“description”:”Sky is Clear”,
“icon”:”01d”}],
“base”:”cmc stations”,
“main”:{
“temp”:7.41,
“pressure”:1027,
“humidity”:81,
“temp_min”:6,
“temp_max”:9.1},
“wind”:{
“speed”:2.1,
“deg”:10,
“var_beg”:320,
“var_end”:40},
“clouds”:{“all”:0},
“dt”:1416838358,
“id”:2646557,
“name”:”Horsham”,
“cod”:200
}

This makes it easier to understand the data structure, and we can now see there are simple string pairs like “name” : “Horsham” and more complex objects such as “main”.

A PHP example

Using PHP on my web server, this crude bit of code is all that is required to display basic data:-

<?php
$jsonurl = “http://api.openweathermap.org/data/2.5/weather?q=Horsham,uk&units=metric”;
$json = file_get_contents($jsonurl);
$weather = json_decode($json);
$station = $weather->name;
$humidity = $weather->main->humidity;
$tempC = $weather->main->temp;
$min_tempC = $weather->main->temp_min;
$max_tempC = $weather->main->temp_max;
echo “Weather in “.$station.”<br>”;
echo “<br>”;
echo “Humidity: “.$humidity.”%”;
echo “<br>”;
echo “current temperature: “.$tempC.”‘C”;
echo “<br>”;
echo “minimum temperature: “.$min_tempC.”‘C”;
echo “<br>”;
echo “maximum temperature: “.$max_tempC.”‘C”;
?>

…and selecting this file with Firefox, the browser window looks like this:-

For my central heating project, I already generate a temperature graph using PHP…

…but the existing data (from 3 temperature sensors) is created and put into a file by my Gambas program.

When selected by a remote web browser, the PHP page reads the file and plots the data for the last 5 days.

 

For more detail: Getting & using data from OpenWeatherMap


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top