Overview
Johhny-Five is a well-documented javascript library that leverages Arduino’s StandardFirmataPlus
to allow for direct control of an Arduino, Raspberry Pi or other microcontroller straight forward and accessible. In this tutorial will walk through how to use it to control a physical output via an Arduino Uno.
Materials
- Arduino Uno board
- Arduino IDE
- node.js
- Johnny-Five module (
npm install --save johnny-five
)
Setup
- Create and initialize a new directory for this node.js system
mkdir j5_test
cd j5_test
npm init
- Install and save the Johnny-Five package
npm install --save johnny-five
- Upload the
StandardFirmataPlus
firmware to your Arduino- Open Arduino application and plug in your Arduino
- Open File>Examples>Firmata>StandardFirmataPlus
- Upload to your Arduino and close the Arduino application
- Use a text editor to open a new file and call it j5_test.js
- Copy and paste this code:
1 2 3 4 5 6 7 8 9 10 11 |
var five = require("johnny-five"); var board = new five.Board(); // The board's pins will not be accessible until // the board has reported that it is ready board.on("ready", function() { console.log("Ready!"); var led = new five.Led(13); led.blink(500); }); |
Twitter Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
var five = require("johnny-five"); var board = new five.Board(); var Twitter = require('twitter'); //use a flag to know when the arduino is ready var boardIsReady = false; var client = new Twitter({ consumer_key: 'your_consumer_key', consumer_secret: 'your_consumer_secret', access_token_key: 'your_token', access_token_secret: 'your_token_secret', }); // The board's pins will not be accessible until // the board has reported that it is ready board.on("ready", function() { console.log("Ready!"); //set the flag to true boardIsReady = true; }); //set up the twitter stream filter. here we are tracking the #trump hashtag var stream01 = client.stream('statuses/filter', {track: '#trump'}); //this is the event listener. stream01.on('data', function(tweet) { //we want to print the number of favorites for this tweek if(tweet.user.favourites_count != null){ console.log(tweet.user.favourites_count); } //if the board is ready, we want to blink the led with each tweet if(boardIsReady){ //blnk the LED HERE var led = new five.Led(11); led.fadeIn(100, function(){ led.off(); }) } }); |