Building Connected Things with Node.js, Johnny-Five, and Microsoft Azure

Responding to Sensor Input

In this lab, you will use a Grove Button Sensor as an input sensor and use the data comeing from the button to turn an LED Module on and off.

Table of Contents

Setting Up the Board

In this lab, you will us a Grove Button Sensor as an input sensor and use the data comeing from the button to turn an LED Module on and off.

  1. Connect the Grove shield to the Edison (mounted to the Arduino adapter).
  2. Connect the Grove Button Sensor to port D4
  3. Connect the LED Module to port D6, and insert an LED into the LED Module.

Building the Application

There are three (3) steps to building the application in this lab:

  1. Define and install the application metadata and dependencies.
  2. Define the device abstractions.
  3. Handle the board.on(“ready”) callback.

Define an Install the Application Metadata and Dependencies

Defining and installing the application metadata and dependencies works similarly to steps 1-3 in the Hello, IoT World! lab.

Start by creating a new application directory and a new package.json file.

  1. Using your favorite/preferred text/code editor, create a file in the directory you want to use for this lab (e.g. C:\Development\IoTLabs\MyLab) named package.json
  2. Add the following code…
    "name": "button-led",
    "version": "0.0.1",
    "repository": {
      "type": "git",
      "url": ""
    "bugs": {
      "url": ""
    "description": "Responding to sensor input using Node.js, Johnny-Five, a Grove Button Sensor and LED Module.",
    "main": "button.js",
    "license": "MIT",
    "dependencies": {
      "johnny-five": "latest",
      "edison-io": "latest"

Define the Device Abstractions

The application code resides in a JavaScript file in the same directory as the package.json file.

  1. Create a file named button-led.js in your lab application directory (e.g. C:\Development\IoTLabs\MyLab).

    Within the application file (button-led.js), you need to define the abstractions of the devices you will be using within the application. There are five (5) variables that matter:

    • A variable to reference the Johnny-Five framework object.
    • A variable to reference to the Edison-IO plugin object.
    • A variable to represent the physical board.
    • A variable to represent the physical button.
    • A variable to represent the physical LED.

  2. Within the button-led.js file, add the following code:

    'use strict';
var five = require ("johnny-five"); 
var Edison = require("edison-io");

// Define the sensors you will use
var button, led;

// Define the board, which is an abstraction of the Intel Edison
var board = new five.Board({
    io: new Edison()

In this code you define five (5) variables that you will be working with:

  • five - represents the Johnny-Five framework capabilities, which provide a type of object model for working with boards like Arduino and Edison.
  • Edison - a variable that represents the edison-io plugin for Johnny-Five.
  • board - a representation of the physical board you are using.
  • button - a represntation of the physical Button Sensor you are using.
  • led - a represntation of the physical LED Module you are using.

Handle the board.on(“ready”) Callback

Johnny-Five provides a board ‘ready’ construct that makes a callback when the board is on, initialized and ready for action. Inside the anonymous callback function is where your application code executes (this function is invoked when the board is ready for use).

In the following code, you will create a callback function that is invoked when the board is initialized and ready (this is a Johnny-Five concept). In this lab, you will begin to work with Johnny-Five abstractions of the sensors and actuators - specifically Led and Button.

  • You will create instances of each object type (Led andButton).
  • You will handle the Button.on('press') and Button.on('release') events by defining the call back function for each.
  • You will change the state of the Led object in the callback functions.

In an earlier lab you likely used digitalWrite(pin, value) to set the pin state to 0 (LOW) or 1 (HIGH). In this lab, that code is abstracted into the and Led.on() functions.

  1. Add the following code to button-led.js
  // The board.on() executes the anonymous function when the
  // board reports back that it is initialized and ready. 
  board.on("ready", function() { 
    console.log("Board connected..."); 
    // Plug the LED module into the
    // Grove Shield's D6 jack.
    led = new five.Led(6);
    // Plug the Button module into the
    // Grove Shield's D4 jack.
    button = new five.Button(4);
    // *********************************************
    // The button.on('press') invokes the anonymous 
    // callback when the button is pressed.
    // *********************************************
    button.on('press', function() {
    // *********************************************
    // The button.on('release') invokes the
    // anonymous callback when the button is
    // released.
    // *********************************************
    button.on('release', function() {

Run the Application

The Button-Led application will run on the Intel Edison, but first, you have to get the code on the board.

Copy the Application Files to the Edison

The application will execute on the Intel Edison.

  1. Open FileZilla
  2. Set the Host to the IP address of your device.
  3. Set the Username to the administrative username (e.g. root).
  4. Set the Password to whatever you set the password for the device.
  5. Set the Port to 22
  6. Click Quickconnect
  7. In the Local site window (typically the left side), navigate to your local application directory.
  8. In the Remote site windows (typically on the right side) - this is the file system on the Edison.
    • Navigate to the labs directory you previously created (e.g. $root\labs)
  9. Drag the package.json and button-led.js files to the blinky directory on the Edison.

Install the Application Dependencies

You can remotely execute NPM on the board to install the application dependencies. The instructions are a little different, depending on whether you are using a Windows PC or a Mac. Select the appropriate tab below and follow the instructions.

Installing Application Dependencies Using Windows (PuTTy)

  1. In PuTTY, double-check that you are in the Session screen by looking at the left-hand tree menu.
  2. Select the Serial radio button under Connection type.
  3. Specify the destination you want to connect to:
    • Serial line:
      Use the COM port number designated for your device. This should take the form of COM# where # is the number of your USB Serial Port (for example, COM3). See Intel's documentation for more information.
    • Speed:
      Always use 115200 for the baud rate.
  4. Click Open to open the serial connection.
  5. When you see a blank command prompt, press Enter. At the login prompt, login with the username root and the password (if you created one).

Installing Application Dependencies Using Mac (Screen)

You can execute commands on the Edison using the screen utility.
  1. Open Terminal
  2. Type screen /dev/cu.usbs then press Tab to autocomplete.
  3. Add 115200 -L after the device identifier, and press Enter.
  4. At the login prompt, login with the username root and the password (if you created one).

Run the Application on the Board

From the remote session (Command Prompt or Terminal), execute the following command in the application directory. In this lab you are specifying the JavaScript file that node.exe should execute.

node button-led.js

Once the application is running, you can press the button and see the LED light up (you will also see the PRESSED log message in the remote session). As long as the button is pressed, the LED will remain on. When you release the button, the button.on('release') callback is invoked and the LED turns off (you will also see the RELEASED log message).

When you want to quit the application, press CTRL + C twice to exit the program without closing the window (you may also have to press Enter).

Conclusion & Next Steps

In this lab, you learned how to write a Node.js/Johnny-Five application that writes LOW (0) and HIGH (1) signals to a digital output pin to make an LED blink. In itself, this may not be very exciting, but the core concept is necessary.

In this lab you learned the following concepts:

  1. Using Johnny-Five abstractions for sensors and actuators.
  2. Responsing to sensor events using the sensor.on('eventType') pattern.
  3. Using data collected from input sensros to control output actuators.

Go to ‘Building the Thingy 4 Edison’ ›