Getting the current weather forecast

3 min read

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:

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:

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:

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:

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:


Packt

Share
Published by
Packt

Recent Posts

Harnessing Tech for Good to Drive Environmental Impact

At Packt, we are always on the lookout for innovative startups that are not only…

2 months ago

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago