Table of Contents
- Table of Contents
- Setting Up the Board
- Building the Application
- Run the Application
- Conclusion & Next Steps
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.
- Connect the Grove shield to the Edison (mounted to the Arduino adapter).
- Connect the Grove Button Sensor to port D4
- 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:
- Define and install the application metadata and dependencies.
- Define the device abstractions.
- 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.
- 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…
Define the Device Abstractions
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.
Within the button-led.js file, add the following code:
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
- You will create instances of each object type (
- You will handle the
Button.on('release')events by defining the call back function for each.
- You will change the state of the
Ledobject 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
- Add the following code to button-led.js
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.
- 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.
- Navigate to the labs directory you previously created (e.g. $root\labs)
- 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)
- 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).
Run the Application on the Board
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:
- Using Johnny-Five abstractions for sensors and actuators.
- Responsing to sensor events using the
- Using data collected from input sensros to control output actuators.