Skip to main content
Version: BSP 7.x.y

Node.js and Node-RED on Linux

Introduction​

info

For Torizon users, you may want to check Node.js on Torizon and Node-RED on Torizon.

This Article explains how to install Node.js, npm, and Node-RED to a Linux Image, then Blink a LED using the GPIOs.

Node.js Setup​

While Building a Linux Image​

Node.js can be added to an OpenEmbedded Linux build for inclusion into the image's rootfs. After establishing the image build configuration, additionally, append the following line to the oe-core/build/conf/local.conf file:

local.conf
CORE_IMAGE_EXTRA_INSTALL += "nodejs"

You may also include NPM:

local.conf
CORE_IMAGE_EXTRA_INSTALL += "nodejs nodejs-npm"

You may find the page TipsAndTricks/NPM from the Yocto Project Wiki useful.

Performance & Suitability​

Since Node.js is a server-side interpreter for the Javascript language, it is expected to perform slower than a compiled language such as C/C++. However, because of its event-driven paradigm, it may well suit web I/O intensive tasks, such as implementing a web server.

Still, Node.js can also be viable for tasks that aren't very CPU intensive, which may be a fast development solution since Javascript is a high level programming language.

Node-RED Setup​

Installing Node-RED​

Install Node-RED with the following command:

# npm install -g node-red --unsafe-perm

Running Node-RED​

Start Node-RED by running the following command on the terminal:

# node-red

Examples​

GPIO - Blinking LED​


var fs = require('fs');

var export_path = '/sys/class/gpio/export';
var blink_period = 500; //in milisseconds

var gpio_53 = new NewGpio(53, 'out', 0);
function NewGpio (id, direction, state) {
this.number = id;
this.path = '/sys/class/gpio/gpio' + id + '/';
this.direction = direction;
this.value = state;
}

function export_pin (pin, callback){
//callback has error as argument, or null if no error
console.log('first');
console.log(pin);
fs.stat(pin.path, function(err, stats) {
console.log('second_not_exp');
console.log(pin);
if (!err) callback(null, pin);//if gpio already exported
else{//if gpio not exported yet
//export it
fs.writeFile(export_path, pin.number, function (err) {
console.log('second_exp');
console.log(pin);
if (err) callback(err, null);
else callback(null, pin);
});
}
});
}

function configure_pin(pin, callback){
//callback has error as argument, or null if no error
console.log('third');
console.log(pin);
fs.writeFile(pin.path + 'direction', pin.direction, function (err) {
if (err) callback(err, null);
else callback(null, pin);
});
}

function blink(pin){
console.log('fourth');
console.log(pin);
fs.writeFile(pin.path + 'value', pin.value, function (err) {
if (err) throw err;
pin.value ? pin.value = 0 : pin.value = 1;
});
}

//here the code execution starts
export_pin( gpio_53, function (err, pin) {
if (err) throw err;
configure_pin(pin, function (err, pin) {
if (err) throw err;
setInterval(function(){
blink(pin);
}, blink_period);//call blink periodically
});
});



Send Feedback!