DESK-MX6-L-AN-0012: Using node.js for Embedded applications

From DAVE Developer's Wiki
Jump to: navigation, search
Info Box


200px-Emblem-important.svg.png

This application note has been validated using the kit version in the History table.


History
Issue Date Notes
2024/06/13 DESK-MX6-L 4.x.x


Introduction[edit | edit source]

Nowadays several designs are based on latest Javascript technologies available for implementing advanced (Industrial) Graphical User Interface in a easy way.

This Technical Note illustrates how to use the SBC Axel for installing the well known node.js runtime application framework.

node.js allows to create desktop applications in JavaScript, HTML, and CSS.

node.js on DESK[edit | edit source]

node runtime can be easily installed in the target root file system wth the following steps.

Cloning nvm git repository[edit | edit source]

Just properly configure the environment and start the git clone:

export NVM_DIR="$HOME/.nvm" && (
  git clone https://github.com/nvm-sh/nvm.git "$NVM_DIR"
  cd "$NVM_DIR"
  git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
) && \. "$NVM_DIR/nvm.sh"

Once installed, it should be enough to setup nvm adding to your ~/.profile the following lines:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Installing node.js[edit | edit source]

Once nvm is installed, simply ask it for installing the latest LTS version of node.js:

root@desk-mx6:~# nvm install --lts
Installing latest LTS version.
Downloading and installing node v20.14.0...
Downloading https://nodejs.org/dist/v20.14.0/node-v20.14.0-linux-armv7l.tar.xz...
##################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.14.0 (npm v10.7.0)
Creating default alias: default -> lts/* (-> v20.14.0)
root@desk-mx6:~#

The installed version can be checked:

root@desk-mx6:~# node --version
v20.14.0
root@desk-mx6:~#

Accessing gpio via node.js[edit | edit source]

As a usage example, it is possible to access a GPIO. Please refer to this Application Nore for more information about the hardware adapter useful for this example.

First of all, install the onoff package for toggling the GPIO status:

root@desk-mx6:~# npm install onoff

added 6 packages in 57s
npm notice
npm notice New minor version of npm available! 10.7.0 -> 10.8.1
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.1
npm notice To update run: npm install -g npm@10.8.1
npm notice
root@desk-mx6:~#

As per the device tree code example:

  MX6QDL_PAD_EIM_D21__GPIO3_IO21  0x1b0b1         /* LED0 - gpio85        */
  MX6QDL_PAD_EIM_D22__GPIO3_IO22  0x1b0b1         /* LED1 - gpio86        */
  MX6QDL_PAD_EIM_D28__GPIO3_IO28  0x1b0b1         /* LED2 - gpio92        */
  MX6QDL_PAD_EIM_D20__GPIO3_IO20  0x1b0b1         /* LED3 - gpio84        */

it is possible to see which gpio number is associated to the LEDs.

As seen by the kernel gpio interface:

root@desk-mx6:~# cat /sys/kernel/debug/gpio
gpiochip0: GPIOs 0-31, parent: platform/209c000.gpio, 209c000.gpio:
 gpio-0   (                    |power-seq-gpio1     ) out hi
 gpio-4   (                    |reset               ) out hi ACTIVE LOW
 gpio-24  (                    |regulators:usb_otg_v) out lo

gpiochip1: GPIOs 32-63, parent: platform/20a0000.gpio, 20a0000.gpio:
 gpio-62  (                    |spi0 CS0            ) out hi ACTIVE LOW

gpiochip2: GPIOs 64-95, parent: platform/20a4000.gpio, 20a4000.gpio:
 gpio-83  (                    |cd                  ) in  lo IRQ ACTIVE LOW
 gpio-93  (                    |5-0048              ) in  hi IRQ ACTIVE LOW
 gpio-95  (                    |regulators:usb_h1_vb) out hi
...
...

the LEDs belong to the gpiochip2 device (from number 64 to 95).

In this way, it is possible to create a Javascript code gpio-onoff.js which toggle/blink a LED (see code here):

'use strict';

const Gpio = require('onoff').Gpio;     // Gpio class
const led = new Gpio(86, 'out');        // export gpio86 as an output

// Toggle the state of the LED connected to gpio86 every 500ms
const iv = setInterval(_ => led.writeSync(led.readSync() ^ 1), 500);

// Stop blinking the LED after 5 seconds
setTimeout(_ => {
        clearInterval(iv); // Stop blinking
        led.unexport();    // Unexport GPIO and free resources
}, 5000);

Running the code, LED1 (gpio86) blinks/toggle every 500ms and after 5s it stops:

root@desk-mx6:~# node gpio-onoff.js
root@desk-mx6:~# 

Simple web server[edit | edit source]

Using the nodejs Javascript runtime it is possible to easily create an embedded webserver with few lines of code:

root@desk-mx6:~# cat server.mjs
// server.mjs
import { createServer } from 'node:http';

const server = createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World!\n');
});

// starts a simple http server locally on port 3000
server.listen(3000, '192.168.0.91', () => {
  console.log('Listening on 192.168.0.91:3000');
});

// run with `node server.mjs`
root@desk-mx6:~# node server.mjs
Listening on 192.168.0.91:3000

and the web server can be contacted from the network at its IP address:

node.js web server