Displaying Web APIs on an Arduino LCD using Node.js

Originally published at: http://www.sitepoint.com/web-apis-on-arduino-lcd/

LCDs. They’re fun. It’s one of the coolest ways to bring a voice to your Arduino as it speaks through glowing text. In this article we’re going to explore how to bring in data from a web API and display it on your Arduino’s LCD using Node.js.

My favourite meetup of every month here in Sydney is the IoT Sydney meetup. It’s a great meetup (you should come along!). At the end of every meetup, there is a random prize draw for those who RSVPed. I thought it was about time that this was done in true IoT style, so I put together a quick Arduino LCD set up that pulls in Meetup.com event RSVP info and then randomly selects a lucky member. Thought it might be a nice surprise and way easier than drawing names out of a hat!

It also falls in neatly with the current series of Internet of Things articles I’m writing here at SitePoint. I’m exploring the many different IoT possibilities out there with various devices. Last week, I looked at using IoT data in the Unity game engine and before that, I looked at how to pull in data from the Jawbone Up.

Let’s get started!

Your Arduino Sketch

The sketch we’ll be using for this example looks like so:

It is a slightly modified version of the one you’ll find in the Sparkfun Inventors Kit. There are other variations of LCD sketches you’ll find online that rearrange how things are connected up but will work the same way. You might just need to adjust which pins you define in the JavaScript below.

Our JavaScript Code

In our Node.js JavaScript file, we include the following:

var express = require('express'),
	app = express(),
	server = require('http').Server(app),
	port = 5000,
	five = require('johnny-five'),
	request = require('request'),
	_ = require('underscore'),
	board = new five.Board(),
	lcd;

board.on('ready', function() {
	lcd = new five.LCD({
		pins: [12, 11, 5, 4, 3, 2],
		rows: 2,
		cols: 16
	});

	this.repl.inject({
		lcd: lcd
	});
});

app.get('/chooseMember/:event_id', function(req, resp) {
	request({
		url: 'https://api.meetup.com/2/rsvps?key=474cc9332345ea7d7e135f50653c&event_id='+req.params.event_id,
		json: true
	}, function(error, response, body) {
		var members = _.pluck(body.results, 'member'),
			randomMember = members[_.random(members.length - 1)];
		
		resp.json(randomMember);

		console.log(randomMember.name);

		lcd.clear().print(randomMember.name);
	});
});

server.listen(port, function() {
  console.log('Listening on ' + port);
});

Setting This Up Step By Step

If you’re pretty clued into Node.js, much of that code will already make sense to you. I’ll explain each bit just to ensure everyone is on the same page and provide guidance along the way on anything else we’ll need to set up.

To start with, we set up our express server variables in preparation to run a localhost server on port 5000:

var express = require('express'),
	app = express(),
	server = require('http').Server(app),
	port = 5000,

We’ve then got a rather important thing to include, johnny-five. This is the npm library which gives us access to the functions we’ll need to control our Arduino via Node.js.

	five = require('johnny-five'),

After that, we’re including the request module. We’ll be using this to make http requests from our Node.js server to the Meetup API.

	request = require('request'),

To keep things very clean and simple, we’ll be using underscore to go through the arrays of data.

	_ = require('underscore'),

The final two variables are the board and lcd variables that we’ll use to store the Arduino objects that johnny-five creates.

	board = new five.Board(),
	lcd;

Continue reading this article on SitePoint

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.