We need to take what we've learned in this chapter and put it together. Start by creating a file called weather-dashboard.js in your project folder, and setting up your Johnny-Five, Raspi-IO, and request libraries, constructing your Board object, and creating your board.on('ready') handler:
const Raspi = require('raspi-io')
const five = require('johnny-five')
const request = require('request')
const board = new five.Board({
io: new Raspi()
})
board.on('ready', () => {
})
Then, inside the board.on('ready') handler, construct and set up our LCD:
let LCD = new Five.LCD({
controller: 'PCF8574'
})
LCD.noBlink()
LCD.on()
Then, we'll create a function that gets the weather data, and set it on an interval of one minute:
function getWeather() {
request({
url: 'http://api.openweathermap.org/data/2.5/weather',
qs: {
q: [your city],
appid: [your API key],
units: ['metric' or 'imperial'],
json: true
}
}, (err, resp, body) => {
})
}
setInterval(getWeather, 60000)
In the request callback, we'll clear, and write to, the LCD:
LCD.clear()
LCD.home().print('Temp: ' + body.main.temp + ' Deg [F or C]')
LCD.setCursor(0, 1).print(body.weather.description)
Finally, call the getWeather() function at the start to prevent the project from taking a full minute before showing anything:
getWeather()
Once you have the full code together, load the project folder onto our Pi the following, navigate to the folder in your Pi SSH session, and run the following commands:
npm i
sudo node weather-dashboard.js
You should have the temperature and conditions for the city you put in appear on the LCD, and they should refresh every minute.
Now that we've seen a project where the Pi pulls from a nice neat JSON REST API, let's take a crack at getting data from a bit more difficult source: HTML scraping.