Javascript & Node.js®

 [email protected] ’s features can be accessed from a compatible host, such as Raspberry Pi, using the Firmata protocol over serial (for more information see Programming Model).

Firmata provides a standardized mechanism to exchange information between a real-time processor such as  [email protected] and a host, useable by many programming languages.

Javascript is an interpreted language like Python, and therefore relies on an interpreter to be running to execute its functions. The most common javascript interpreter is the web browser, and one of the fastest is the one built into Google’s Chrome browser.

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine, and uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. The Node.js 200,000+ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Johnny Five One of those many libraries includes node firmata, which means that once node.js is installed, we can use the wide range of node packages directly with  [email protected] ’s hardware, allowing the rapid creation of web-based sensor systems.

In this example we are going to use Johnny-Five, which uses node firmata, and is an excellent programming framework which abstracts the hardware and makes cloud connected device programming much easier.


Using Node Firmata with Ardhat

Raspian Jessie comes with a version of Node.js installed; unfortunately it is an older version that does not play well with many newer libraries, so we need to remove it and reinstall a newer version of Node.js (v4.x) together with npm.

To do this we must uninstall the built-in version and re-install using the instructions below.

To enter these commands more easily, connect to the Pi using `ssh`, so that you can copy and paste them into the terminal.

First we remove all the built in packages (but leave your current workspace if any, which by default is at ~/.node-red) .

Uninstall legacy node

~ $ sudo apt-get remove nodered

~ $ sudo apt-get remove nodejs nodejs-legacy

~ $ sudo apt-get remove npm

# if you installed npm

Now we can proceed to re-install Node.js following the instructions below depending on your processor type. (As the Pi 2 uses a different processor (Arm v7) compared with the original Pi (Arm v6) the method of installing node.js is slightly different).


Raspberry Pi 2

Install Node.js on Raspberry Pi 2 - and other Arm7 processor based boards

~ $ curl -sL | sudo bash -

~ $ sudo apt-get install -y build-essential python-dev python-rpi.gpio nodejs

This also installs some additional dependencies.


Pi (version 1), Pi Zero, Pi A+/B+

Install Node.js on Pi (version 1), Pi Zero, Pi A+/B+

~ $ wget

~ $ sudo npm cache clean

~ $ sudo dpkg -i node_archive_armhf.deb

~ $ sudo apt-get install build-essential python-dev python-rpi.gpio

This also installs some additional dependencies.

Install the latest stable version of Node-RED and Johnny-Five

~ $ sudo npm install -g --unsafe-perm node-red

~ $ npm install johnny-five

Now we are ready to run our javascript program on node.js driving Ardhat.

Create a file with the following contents:

var five = require("johnny-five"),
    board = new five.Board({ port: "/dev/ttyS0" });

board.on("ready", function() {
  // Create an Led on pin 9
  var led = new five.Led(9);

  // Strobe the pin on/off, defaults to 100ms phases


Save it as test.js, and run it with

Run the test Javascript program

~ $ node test.js

1455005904950 Connected /dev/ttyS0

1455005910077 Repl Initialized


Ardhat’s activity LED will blink quickly.

“Repl Initialized” indicates that the Johnny-Five “Read Evaluate Print Loop” is running, which means that you have a node prompt at which you can enter node commands.

For a more in-depth example, see this article which shows how to build a simple home-monitoring system, similar to commercial products like Nest and Hive.



NodeRED An alternative to programming in javascript directly is to use the visual programming approach offered by Node-RED.

Node-RED provides a browser-based flow editor that makes it easy to wire together flows using a collection of ‘Nodes’ in a ‘palette’. Flows can then be deployed in a single-click. For more info see the Node-Red website , or this excellent tutorial on Adafruit.