DESK-MX8M-L-AN-0006: 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-MX8M-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 ORCA SBC 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-mx8mp:~# 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-mx8mp:~#

The installed version can be checked:

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

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

As a usage example, it is possible to access a GPIO.

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

root@desk-mx8mp:~# npm install onoff

added 6 packages in 27s
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-mx8mp:~#

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

root@desk-mx8mp:~# cat gpio-onoff.js
'use strict';

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

// Toggle the state of the GPIO every 1000ms
const iv = setInterval(_ => led.writeSync(led.readSync() ^ 1), 1000);

// Stop blinking the LED after 10 seconds
setTimeout(_ => {
        clearInterval(iv); // Stop blinking
        led.unexport();    // Unexport GPIO and free resources
}, 10000);
root@desk-mx8mp:~#

In the script, the GPIO used is:

  • J8.40 mapped to SAI5_RXC
  • the related GPIO muxed pin is GPIO3_IO20
  • this corresponds to (3 -1)*32 + 20 => 84
  • the GPIo used is the number 84

and run the script:

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

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