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

Hello, IoT World!

In this lab, you will use an LED Module to send digital output in the form of a blinking LED. This is the Hello, World of the IoT space.

Table of Contents

Setting Up the Board

For this lab, you will create a Thing that can blink an LED.

  1. Connect the Grove shield to the Edison (mounted to the Arduino adapter).

Building the Application

There are five (5) steps to building the application in this lab:

  1. Define the application metadata and dependencies.
  2. Install the dependencies.
  3. Install specific dependencies for the Intel Edison.
  4. Define the device abstractions.
  5. Handle the board.on(“ready”) callback.

Define Application Metadata and Dependencies

Since you are building a Node.js application, you will use a package.json file to define the application metadata and the application dependencies. This helps make the application more portable, enabling the dependencies to be installed anywhere you run the application (e.g. on any device you deploy it to). The package.json file is used to give information to NPM to identify the project nd handle the project’s dependencies. It can optionally contain other metadata, including a project description, the version of the project, license information, configuration data, etc.

The package.json is kept at the application root and requires two fields:

  • Application name - The name must be less than or equal to 214 characters, may not start with a . or _, may not have uppercase letters in the name, and must not contain non-URL-safe characters.
  • Application version.

Optionally, for these labs, you will include:

  • The repository information where the source code is maintained - this will help you to identify the master source if you need to reference it.
  • Application description - this will help you understand what each lab sample does.
  • A pointer to the Github issues tracker for the lab samples (in case you find something you want to tell us about).
  • A license specification (MIT).
  • The main field, which specifies the JavaScript file to run if you execute node . in the application directory.
  • Dependencies, which are specified in a simple object that maps a package name to a version range.

Start by creating the basic package.json file in the application directory.

  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": "blinky",
    "version": "0.0.1",
    "repository": {
      "type": "git",
      "url": "https://github.com/ThingLabsIo/IoTLabs"
    },
    "bugs": {
      "url": "https://github.com/ThingLabsIo/IoTLabs/issues"
    },
    "description": "Hello, World using Node.js and Johnny-Five blinks an LED.",
    "main": "blinky.js",
    "license": "MIT",
    "dependencies": {
      "johnny-five": "latest"
    }
  }
  

Install Dependencies Using NPM

With the package.json file created you can use NPM to pull down the necessary dependencies.

  1. Open a terminal window (Mac OS X) or Node.js command prompt (Windows)
  2. Execute the following commands (replace C:\Development\IoTLabs\MyLab with the correct path for your lab working folder where the package.json file resides):
cd C:\Development\IoTLabs\MyLab
npm install

Install Specific Dependencies for the Intel Edison Board.

The Intel Edison requires one additional dependency - a plugin for Johnny-Five named edison-io. This is the plugin the maps the Johnny-Five abstractions to the physical board architecture. To add the edison-io dependency you can use NPM.

  1. From the terminal window (Mac OS X) or Node.js command prompt (Windows) in your application directory, execute the following command:
npm install edison-io --save

This will install the edison-io package and add it to the dependencies list in your package.json file. Open the package.json file to verify it was added.

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 blinky.js in your lab working directory (e.g. C:\Development\IoTLabs\MyLab).

Within the application file (blinky.js), you need to define the abstractions of the devices you will be using within the application. There are four (4) 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 hold the pin number value for the LED.
  1. Within the blinky.js file, add the following code:
    'use strict';
    var five = require ("johnny-five"); 
    var Edison = require("edison-io");
    // Pin 13 is the default pin on the large Arduino breakout board.
    var LEDPIN = 13

    // Create a Johnny Five board instance to represent your Arduino.
    // board is simply an abstraction of the physical hardware, whether it is 
    // an Intel Edison, Arduino, Raspberry Pi or other boards. 
    // For boards other than Arduino you must specify the IO plugin.
    var board = new five.Board({
    io: new Edison()
    });
    

In this code you define four (4) 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.
  • LEDPIN - variable to hold the pin number value for the LED.

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). Johnny-Five provides a collection of objects that represent the board, the pins on the board, and various types of sensors and devices that could be connected to the board. For this lab you are going to write code that is fairly true to the base Arduino sketch programming model (we’ll get into the abstractions that Johnny-Five provides you later). This will help you understand some of the basic concepts for how prototyping boards work.

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).

  • You will set digital pin (the LEDPIN variable above) as an output pin (vs. an input pin), meaning the application is expecting to send voltage out from the pin as opposed to reading the voltage coming into the pin.
  • You will create a loop that runs once per second.
  • Inside that loop, you will write out to the pin either LOW or HIGH voltage. Since the pin is a digital pin, its only options are 0 and 1 - in the world of Arduino (and similar) boards that is LOW and HIGH. When you send 0 (or LOW) to the pin, that is equivalent to off (sending no voltage). When you send 1 (or HIGH) to the pin that is equivalent to on (sending full voltage).
  1. Add the following code to blinky.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..."); 
      
    // Set the pin you connected to the LED to OUTPUT mode  
    this.pinMode(LEDPIN, five.Pin.OUTPUT); 

    // Create a loop to "flash/blink/strobe" an led  
    var val = 0;
    this.loop( 1000, function() {
      this.digitalWrite(LEDPIN, (val = val ? 0 : 1));
    });
  });
  

Johnny-Five actually has an object model for an LED and we could also have simply done the following, but one of the goals was for you to see how the digitalWrite() function works before abstracting it away.

board.on("ready", function() {
	var led = new five.Led(LEDPIN);
	led.blink(1000);
});

Run the Application

The Blinky 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.
    • Right-click on root and select Create directory
    • Name the directory labs
  9. Drag the package.json and blinky.js files to the labs 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).
  1. To install the application dependencies on the Edison, navigate to the directory where the application files are located and run npm install by executing the following commands:
cd labs
npm install

Run the Application on the Board

From the remote session (Command Prompt or Terminal), execute the following command in the application directory (remember, using . will tell node.exe to execute the JavaScript file referenced in the main property of the package.json file):

node .

You should the indicator LED on the Arduino shield start to blink once per second.

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. Creating a Node.js application using Johnny-Five.
  2. Working with digital output.
  3. Running the application on a device.

Go to ‘Responding to Sensor Input’ ›