ESP32 low.js: Hello World

Introduction

On this tutorial we will check how to create a simple “Hello World” application using low.js and the ESP32.

Low.js is a port of Node.js (a JavaScript runtime) with lower system requirements, allowing it to run on microcontroller boards based on the ESP32 [1].

Note however that low.js is only supported on ESP32-WROVER modules and not on ESP-WROOM-32 modules, as these don’t have the 4 MB of PSRAM required [2]. So, you should take this in consideration when choosing your ESP32 board to test low.js.

You can read here a comparison between low.js and other popular ESP32 frameworks. You can check here the GitHub page of the project.

For this tutorial, we will develop a simple application that will initialize the ESP32 serial interface and periodically print a “Hello World” message to it. Then, we will establish a serial connection with the device and receive the message.

On this tutorial we won’t be covering the procedure for flashing low.js or uploading programs to it, as these processes may change with time. So, you should follow the steps explained in detail on the getting started page of the project, which should reflect the most updated documentation available.

The code

We will start our code by importing the uart module, which will allow us to initialize the serial interface and write content to it. By importing this module, we will have access to the UART class, which will allow us to do both.

let uart = require('uart');

So, we will create an instance of the UART class. Its constructor receives as input a JavaScript object representing the options for initializing the serial interface [3].

We will use this object to set the Rx and Tx pins and the baud rate. This is done by adding to this object the pinRX, pinTX and baud attributes, respectively, and assigning to them the values we want to use.

I’ll be using pins 3 and 1 as Rx and Tx respectively since, for the board I’m using, these are the pins connected to the onboard serial to USB converter.

We will be using a baud rate of 115200, which will be the value we need to use when connecting to the board using a serial program running on a computer, to receive the message.

let stream = new uart.UART({pinRX: 3, pinTX: 1, baud: 115200});

To finalize, we will print a “Hello World” message periodically to the serial port. We will do this by setting a callback function to be executed periodically.

So, we need to call the setInterval function, which receives as first argument the callback function to be executed when the timer elapses and as second argument the time to wait between calls to the callback function. This last value should be specified in milliseconds [4].

To keep the code short, we will define our callback as a JavaScript arrow function. This syntax allows us to define an inline anonymous function and pass it directly as argument of the setInterval function.

On the implementation of our function, we will simply call the write method of the UART class object, passing as input a string with the message we want to write to the serial port.

setInterval(() => {
   stream.write("Hello World\n");
}, 1000);

The final complete code can be seen below.

let uart = require('uart');
 
let stream = new uart.UART({pinRX: 3, pinTX: 1, baud: 115200});
 
setInterval(() => {
   stream.write("Hello World\n");
}, 1000);

Testing the code

To test the code, simply upload the previous script to your ESP32, following the procedure explained on the getting started page of low.js.

Once the procedure finishes, open a serial program of your choice. I’ll be using the Arduino IDE, as it has a built in serial monitor, but you can use other alternatives, such as Putty.

So, simply establish a serial connection to the ESP32, using a baud rate of 115200 (the value we set on the low.js program). You should see an output similar to figure 1, which shows the “Hello World” message getting printed on the serial monitor of the Arduino IDE.

ESP32 low.js Hello World via serial interface
Figure 1 – Receiving a serial message from the ESP32 running low.js.

References

[1] https://www.lowjs.org/

[2] https://www.lowjs.org/supported-hardware.html

[3] https://www.lowjs.org/lowjs_for_esp32/module-uart.html

[4] https://nodejs.org/api/timers.html#timers_setinterval_callback_delay_args

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading