You are currently viewing Javascript, programming a Microprocessor – Part 1

Javascript, programming a Microprocessor – Part 1

Using an IoT (Internet of Things) device feels out of reach for many developers. Many of us don’t have enough skills and haven’t even learned how to solder, programming a microcontroller requires different skills and knowledge, but those times are long gone. Today, microcontrollers are powerful enough to run JavaScript. And that opens up endless opportunities for us to work on and realize thousands of great and interesting projects. Today, with just a few lines of code, we can do wonders.

In this series of posts, we will be looking at how to control and program microcontrollers using high-level languages, focusing on JavaScript due to its popularity in the programming community.

First, we must know that we need some tool that somehow translates my JavaScript lines into a language that the microcontrollers must be able to process, for example: assembler. For them we will use Frameworks.

What is a framework?

A Framework is a model or structure that facilitates the development of a project and makes its realization much faster and more efficient.

javascript-lowjs

low.js

There are thousands of open source projects, low.js is one of them. It is a Node.js JavaScript runtime port in which you can create scalable Internet-native electronics applications easily and quickly. Thanks to its low minimum requirements it is possible to use it on low power microcontroller boards based on ESP32-WROVER.

Comparison:

To fully understand the advantages it offers, it is necessary to make a comparison with other existing frameworks:

Javascript

low.js VS ESP-IDF

ESP-IDF is used to program the ESP32 provided by Espressif (ESP32 supplier). For its use and programming it is necessary to have experience and knowledge in the C/C++ languages as well as the internal components of the microcontrollers. For a programmer with some knowledge it will be a great field of work and will allow him to get the most out of the ESP32 chips.

On the other hand, low.js requires much less programming time than ESP-IDF, being easier and often having fewer errors because we get the same result in fewer lines of code, being more readable. Above all, taking into account that low.js is developed on an improved and modified version of ESP-IDF.

Javascript

low.js VS Arduino

Programs and applications written for the microcontroller board are called sketches, written in simplified C/C++ using the Arduino API. It is used by many people and has infinite uses, so for any purpose someone has surely been dedicated to create and develop a library, which can be used as a template for a new project which would greatly simplify our work.

But… Arduino loses functionality when: we want to perform several functions in parallel, especially because not all API calls are non-blocking, Arduino in ESP32 supports Wifi networks, building servers with all functions requires a lot of manual work. HTTP, HTTPs or WebSockets are protocols that can be used with third-party libraries, but are often not standards-compliant or full-featured.

These weaknesses are in themselves some of the strengths of low.js, having above all a certain ease of use for jobs that require its use.

low.js VS Espruino

Espruino, another JavaScript framework for microcontrollers (which we will see in the next article). It runs on less powerful microcontrollers than low.js. This comes at the cost of not taking advantage of the capabilities of more powerful microcontrollers such as the ESP32.

The Espruino API is much less powerful than the low.js API, as Espruino does not provide the Node.js API, which would not fit on smaller microcontrollers. Espruino does provide some API calls that mimic the Node.js API calls, for example fs.writeFile, however, they do not work in the same way, fs.writeFile for example, is blocking (there does not seem to be any way to write a file asynchronously with Espruino).

The Espruino JavaScript engine is usually slower than low.js because it is not compiled to bytecode, but is executed in source code.

low.js VS MicroPython

MicroPython is a Python framework for microcontrollers, just as low.js is a port of Node.js. Both are very easy to use!

Our arguments for low.js versus MicroPython are the same as for Node.js versus Python: low.js allows the programmer to use one programming language, JavaScript, for the whole project consisting of a microcontroller software, a website and a cloud server, the API of low.js is asynchronous first, therefore it makes it much easier to scale projects.

But always keep in mind that if your Python skills are excellent and superior than JavaScript, MicroPython is yours.

Hardware support for low.js

low.js is executable in any ESP32-WROVER module.

The neonious:

The neonious is the microcontroller board made by the developers of low.js, neonious. It is optimized for low.js, with many features that are not possible with generic ESP32-WROVER boards:

  • IDE + integrated web browser based debugger well done for rapid prototyping.
  • Graphical package manager for npm, modules that work out of the box
  • 10/100 MBit Ethernet plus Wifi
  • Clean pin layout with every pin truly usable, more usable pins (27) than the single ESP32-WROVER
  • Truly asynchronous Flash writes, since the file system is on an additional Flash chip (ESP32-WROVER Flash writes always pause the system, by design)
  • Accurate ADC provided by the LPC coprocessor (ESP32 ADC is non-linear)
  • 4 MB of Flash for file system / user program. With low.js in generic ESP32-WROVER modules, only 1 MB is available for the file system / user program.
  • Wireless low.js updates, no need to connect the board to the PC.

Plans:

We have already seen its virtues, advantages and disadvantages. Here are the plans available for this tool.

 

basic low.js 

Free of charge for commercial and hobby use.

  • API Node.JS, JavaScript ES 6
  • Internet-enabled applications
  • Scalable and asynchronous programming
  • npm modules: access the world’s largest software library
  • Custom firmware support for mass application flashing
  • https://www.neonious-iot.com/lowjs/examples/getting-started.html

low.js Professional

15 per plate, volume licenses starting at $ 1

  • Integrated web-based IDE with full-featured debugger for rapid prototyping
  • Add low-level C / C++ code with the native low.js API
  • Wirelessly update low.js and custom firmware

 

Code example:

The following is a small code where we will make a led blink using low.js:

let gpio = require(‘gpio’);

gpio.pins[pinnumber].setType(gpio.OUTPUT);

 

let val = 0, dir = 0;

setInterval(() => {

    val += dir ? 0.03 : -0.03;

    if(val < 0) { val = 0; dir = 1; }

    if(val > 1) { val = 1; dir = 0; }

 

    gpio.pins[pinnumber].setValue(val);

}, 30);

Conclusions

Undoubtedly, we have a powerful tool which we can take into account when carrying out a project or even specialize in it. Thanks to its versatility and ease of code we can achieve great results in a short time.

Comment what you think about this way of programming microprocessors. You can find us at Twitter or LinkedIn to talk and share on the subject. We have a Telegram Channel, which we invite you to join by clicking on the link above, where we publish great content of value that will be of great help to you.

If you are taking your first steps in this immense world of electronics and low-level programming, in our Youtube Channel we have an Arduino Course for Beginners which you will love and will get you started as a programmer.

Leave a Reply