node.js will serve as an intermediary between the final program we use, Node-Red, and the Arduino board running Firmata. In fact, Node-Red is basically just a front end for node.js, so anything we program in Node-Red can be run in node.js. In fact, I often run programs directly as a .js file to assist in debugging, as Node-Red isn't the best debugging environment at times. (See far below on how to do this, if you are as much of a beginner as I was).

I've detailed some resources under Further Resources to assist getting acquainted with node and how to install the various parts we'll eventually need below. This is all described for Windows, but it's not drastically different for Linux (I've installed it on a Raspberry Pi, which runs Debian, a version of Linux).

Installation of node.js[edit | edit source]

To install node.js, simply visit https://nodejs.org/en/ and install the LTS version (the full version is not needed for this project).

Installing various packages needed for this project[edit | edit source]

Installation of packages using npm

You install the various node.js libraries using a program called 'npm' - node package manager. It manages the various node.js libraries and packages available.

You must go to the directory you installed node.js in via a command prompt - the easiest way to do this is to use node.js's command prompt in your start menu. Another option is to hold Shift and right click in the node.js installation folder - the option to 'Open Command Window here' will appear.

Once you have a command prompt, you simply use the following command and you are on your way:

npm install -g packagename I recommend installing all node packages with the '-g' added - this makes them global packages and prevented conflicts and dependency issues.

Installing packages from behind a proxy using npm

If you are behind a proxy (usually when you access the internet from work etc) you will need to tell npm this. You do this via a command prompt again and using the following commands:

npm config set proxy http://"username:password"@proxyaddress.up.ac.za:8080 npm config set https-proxy proxy http://"username:password"@proxyaddress.up.ac.za:8080 the '8080' is the proxy port number, the 'proxyaddress.up.ac.za' is your proxy address and the username and password are obviously your username and password you can check that these settings have been implemented by typing npm config list Installing Johnny-Five A recommended library of JavaScript functions is the Johnny-Five library ( http://johnny-five.io/ or https://github.com/rwaldron/johnny-five) which I've used extensively in this project. Johnny-Five is a Javascript library designed to control a number of boards, Arduino included, for the building of robots. As such the Johnny-Five library has a large number of functions to read sensors and control devices, ideal for a Laboratory automation and control.

Install globally using

npm install -g johnny-five Note that wherever you run your node-red flows from or your JavaScript files will have to have Johnny-Five installed in that directory, so installing johnny-Five globally doesn't help much. I recommend using your .node-red user directory as your base of operations and install johnny-five there.

Installing SerialPort

Serialport is a package needed to communicate using a USB cable via the serial protocol to your Arduino. Unfortunately it can be a real hassle to install (Ref: (https://github.com/EmergingTechnologyAdvisors/node-serialport)

Firstly your node.js needs to be the latest version, which if you have just recently installed should then be fine. ( Ref: https://github.com/EmergingTechnologyAdvisors/node-serialport/issues/673 ),

Then the command that seems to work is (Ref: https://github.com/EmergingTechnologyAdvisors/node-serialport/issues/874 )

npm install -g --unsafe-perm serialport Good luck...

Installing git

git allows npm to install a number of user created packages available on the internet, download and install it from https://git-scm.com/download/win.

Running your first Javascript program[edit | edit source]

Coming from a Mechanical Engineering background, I found it absolutely impossible to find information on how to actually run a simple JavaScript file. With only Matlab experience, the concept of running files from the command prompts (and not a nice program or IDE) was both foreign and confusing. What follows here is an absolute beginner's guide to running your first Javascipt file.

This example is basically how to get to the point to get an LED on a breadboard to blink - see http://web.archive.org/web/20200710020300/http://node-ardx.org:80/exercises/1 for the full example (without this intro).

Firstly, you'll need a Javascript editor. I installed Visual Studio Code (https://code.visualstudio.com/), but there are a list of options ( e.g.http://www.javaworld.com/article/2094847/enterprise-java/review-10-javascript-editors-and-ides-put-to-the-test.html)

Each Javascript piece of code will form a project, so create a new folder for each new project you are running.

Open your editor in that folder. Create a new file and copy over the code from http://web.archive.org/web/20200710020300/http://node-ardx.org:80/exercises/1 into the file.

Save the file with a .js extension. This will let Visual Studio Code know that you are working in JavaScript and suddenly all the useful colouring of words and commands should appear. (You should create a number of other files to tell Visual studio some sundry information - that's covered here in detail but I've found most programs run without them- https://code.visualstudio.com/Docs/languages/javascript)

Open a command prompt in the folder where the .js file is located ( Shift -Right Click in windows and select 'Open Command Window Here')

Type 'node index.js' and hit enter ( or whatever you named your .js file that needs to run, but its standard to name it index.js)

It should run now - presto.

FA info icon.svg Angle down icon.svg Page data
Authors Bradley D Bock
License CC-BY-SA-3.0
Language English (en)
Translations Italian
Related 1 subpages, 1 pages link here
Aliases Setting up node.js and Johnny Five
Impact 343 page views
Created March 5, 2019 by Bradley D Bock
Modified March 2, 2022 by Page script
Cookies help us deliver our services. By using our services, you agree to our use of cookies.