Table of Contents
- Table of Contents
- Setting Up the Board
- Building the Application
- Run the Application
- Conclusion & Next Steps
Setting Up the Board
For this lab, you will create a Thing that can blink an LED.
- 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:
- Define the application metadata and dependencies.
- Install the dependencies.
- Install specific dependencies for the Intel Edison.
- Define the device abstractions.
- 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
_, 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).
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.
- 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
- Add the following code…
Install Dependencies Using NPM
With the package.json file created you can use NPM to pull down the necessary dependencies.
- Open a terminal window (Mac OS X) or Node.js command prompt (Windows)
- Execute the following commands (replace C:\Development\IoTLabs\MyLab with the correct path for your lab working folder where the package.json file resides):
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.
- From the terminal window (Mac OS X) or Node.js command prompt (Windows) in your application directory, execute the following command:
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
- 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.
- Within the blinky.js file, add the following code:
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
LEDPINvariable 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).
- Add the following code to blinky.js
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.
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.
- Open FileZilla
- Set the Host to the IP address of your device.
- Set the Username to the administrative username (e.g. root).
- Set the Password to whatever you set the password for the device.
- Set the Port to
- Click Quickconnect
- In the Local site window (typically the left side), navigate to your local application directory.
- 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
- 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)
- In PuTTY, double-check that you are in the Session screen by looking at the left-hand tree menu.
- Select the Serial radio button under Connection type.
- 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.
Always use 115200 for the baud rate.
- Serial line:
- Click Open to open the serial connection.
- 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.
- Open Terminal
screen /dev/cu.usbsthen press Tab to autocomplete.
115200 -Lafter the device identifier, and press Enter.
- At the login prompt, login with the username root and the password (if you created one).
- To install the application dependencies on the Edison, navigate to the directory where the application files are located and run
npm installby executing the following commands:
Run the Application on the Board
From the remote session (Command Prompt or Terminal), execute the following command in the application directory (remember, using
main property of the package.json file):
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:
- Creating a Node.js application using Johnny-Five.
- Working with digital output.
- Running the application on a device.