Want to stay informed? Share your e-mail address for news:

Click here for our GitHub Repository

Node.js for Microcontrollers

Node.js has a nice module called repl, which implements a read-eval-print loop. It is used to show the prompt when calling Node.js with no arguments.

In this example we will create such a loop, add a toggle command to toggle the LED and tell low.js to present it through the UART to USB adapter of the microcontroller board.

Enter the following code into /index.js:

let repl = require('repl');
let uart = require('uart');
let gpio = require('gpio');

let state = 0;
gpio.pins[pinnumber].setType(gpio.OUTPUT).setValue(state);

let stream = new uart.UART({pinRX: 3, pinTX: 1, baud: 115200});
let replServer = repl.start({input: stream, output: stream, terminal: true, useColors: false});

replServer.defineCommand('toggle', () => {
    state = !state;
    gpio.pins[pinnumber].setValue(state);

    stream.write('LED toggled\n');
});

After syncing and starting the program, you can use a terminal program to connect to the microcontroller via UART/USB and the baud rate of 115,200 and toggle the LED by entering .toggle.

> .toggle
LED toggled
>