Introduction: NodeJs and Arduino

Javascript with embedded devices

In recent time, with the availability of cheap embedded boards and single board computers, there has been a boom in the physical computing world. If you are a web developer and want to get in on the act, sadly, you need to learn something new like the arduino IDE(based on C) or python. But now you can do the same using the language we all love: JAVASCRIPT. So grab a arduino and get started.

Step 1: The Arduino IDE

Download the Arduino IDE for your platform and install it(I am assuming you already have an Arduino). You can also use the Arduino Web Editor,however, you will need to be connected to the Internet to use that. Follow the link below:

Arduino IDE

Step 2: Upload Firmata

Introduction:

Firmata protocol is a generic protocol for communicating with microcontrollers from software on a host computer.

The Firmata library implements the Firmata protocol for communicating with software on the host
computer. This allows you to write custom firmware without having to create your own protocol and objects for the programming environment that you are using.

Read more here:

Firmata Library

Firmata Protocol

What you need to do:

Upload the Firmata Library to Arduino. The library is available as example with the arduino IDE.

Connect the arduino to your computer using the serial the cable. Then open the IDE.

Open the StandardFirmata sketch:

File > Examples > Firmata > StandardFirmata


Upload the library to arduino.

If you already have NodeJs setup, you can skip to step 4 and get your first project up and running.

Else follow step 3 to setup NodeJs.


Step 3: Setting Up NodeJs

Intoduction:

NodeJs is a javascript runtime for server side javasript. It is built on Chrome's V8 Javasript engine, which is basically the thing that makes Chrome the fastest browser in the market.

Further reading:

NodeJs

V8 Javascript engine

Setup:

Follow the link below to download and install node for your platform.(I recommend using node on Linux or Mac. Using it on Windows, you might face some issues.)

Download Node

Open a terminal and run the following commands:

node -v//It will give the current node version
npm -v   //It will give the npm version. npm is the package manager for Node.



Step 4: Hello World!!

Create a new folder for your project.

Inside the folder, create a file named package.json (You can use your favouite editor ).

package.json:

This file contains information about your project. See the pic.

Alternative: Use

npm init<strong><br></strong>

It will setup your package.json file for you.

The important part of the package.json file is the dependencies object. Our project has only one dependency: Johnny-Five

Johnny-Five: Johnny-Five is the JavaScript Robotics & IoT Platform, released by Bocoup in 2012.

We will use Johnny-Five to interact with the arduino.

Install Johnny-five by running the following in a terminal:

npm install johnny-five 

It will download the johnny-five node module and add it to your dependencies list. (It will create a folder named node_modules also).

Connect your arduino to your computer. We will be using the led connected to pin 13 of arduino board, so you don't have to connect anything to the board.

Create a new file called index.js in the same folder. Add the following code and save the file.


var five =require('johnny-five'); //require/import the johnny-five module
var board=new five.Board(); //the Board class from johnny-five module //When the board is ready create a led object on pin 13 and blink it //every 500 milliseconds. board.on('ready',function(){ var led=new five.Led(13); led.blink(500);});

Now run the file using the following command:

node index.js

That's it. You will see the on board led connected to pin 13 blinking.

To stop the app press Ctrl+c.