Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Getting the current weather forecast

Save for later
  • 4 min read
  • 06 Jul 2015

article-image

In this article by Marco Schwartz, author of the book Intel Galileo Blueprints, we will configure Forecast.io. Forecast.io is a web API that returns weather forecasts of your exact location, and it updates them by the minute. The API has stunning maps, weather animations, temperature unit options, forecast lines, and more.

(For more resources related to this topic, see here.)

We will use this API to integrate global weather measurements with our local measurements.

To do so, first go to the following link:

http://forecast.io/

Then, look for the Forecast API link at the bottom of the page. It should be under the Developers section of the footer:

getting-current-weather-forecast-img-0

You need to create your own Forecast.io account. You can do so by clicking on the Register button on the top right-hand side portion of the page. It will take you to the registration page where you need to provide an e-mail address and a strong password.

Then, you will be required to get an API key, which will be displayed in the Forecast.io interface:

getting-current-weather-forecast-img-1

Write this down somewhere, as you will need it soon.

We will then use a Node.js module to get the forecast. The steps are described in more detail at the following link:

https://github.com/mateodelnorte/forecast.io

Next, you need to determine the latitude and longitude that you are currently in. Head on to the following link to generate it automatically:

http://www.ip2location.com/

Then, we modify the main.js file:

var Forecast = require('forecast.io');
var util = require('util');

This will set our API key with the one used/returned before:

var options = {
APIKey: 'your_api_key'
},
forecast = new Forecast(options);

Next, we'll define a new API route for the forecast. This is also where you need to put your longitude and latitude:

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime
app.get('/api/forecast', function(req, res) {
forecast.get('latitude', 'longitude', function (err, result, data) {
   if (err) throw err;
   console.log('data: ' + util.inspect(data));
   res.json(data);
});
});

We will also modify the Interface.jade file with a new container:

h3.row
.col-md-4
   div Forecast
.col-md-4
   div#summary Summary

In the JavaScript file, we will refresh the field in the interface.

We simply get the summary of the current weather conditions:

$.get('/api/forecast', function(json_data) {
     $('#summary').html('Summary: ' + json_data.currently.summary);
   });

Again, the complete code can be found at the following link:

https://github.com/marcoschwartz/galileo-blueprints-book

After downloading, you can now build and upload the application to the board.

Next comes the fun part, as we will test our creation.

Go to the IP address of your board with port 3000:

http://192.168.1.103:3000/

You will be able to see the interface as follows:

getting-current-weather-forecast-img-2

Congratulations! You have been able to retrieve the data from Forecast.io and display it in your browser.

If you're not getting the expected result, don't worry. You can go back and check everything. Ensure that you have downloaded and installed the correct software on your board. Also ensure that you correctly entered your API key in the application.

Of course, you can modify your own interface as you wish. You can also add more fields from the answer response of Forecast.io. For instance, you can add a Fahrenheit measurement counterpart. Alternately, you can even add forecasts for the next hour and for the next 24 hours.

Just ensure that you check the Forecast.io documentation for all the fields that you wish to use.

Summary

In this article, we configured Forecast.io that is a web API that returns weather forecasts of your exact location and it updates the same every minute.

Resources for Article:


Further resources on this subject: