Getting started with the Onion Omega 2+ and Node.js

In this article I present the Onion Omega 2+ and show you how to get started with it using Node.js. Onion Omega2+ is a stamp-sized Linux computer (42.9 x 26.4mm) featuring a 580MHz MediaTek MT7688 processor (MIPS24KEc core), 128Mb DDR2 RAM (64Mb on Omega2), 32Mb Flash (16Mb on Omega2), WiFi b/g/n, USB 2.0, 1 SPI, 1 I2C, 1 I2S (for audio codec), 2 UARTs, 15 General purpose I/Os, 2 PWMs and a MicroSD slot (only on Omega2+). And you can get this tiny-but-awesome Linux machine for only US$ 9.00 (or US$ 7.50 for the Onion Omega2)!

Along with the Onion Omega2 and Omega2+, Onion released in July/2017 a new SMT version of Omega2, the Omega2S (and Omega2S+). These modules are designed to be soldered as SMT devices on PCBs and they include all features of Omega2/Omega2+ plus a third UART, an external SD interface, a PCIe interface and more GPIOs! These modules are intended for high volume commercial and industrial products.

Figure 1 – Onion Omega2+

The device uses Linux OpenWRT distribution (which comes pre-installed) and is very simple and easy to use. It can be programmed just like a desktop Linux computer and you can choose between compiled languages (such as C and C++) and scripted/VM-based languages such as Python, Node.js, Ruby, PHP or any other language you might want!

I chose to use Node.js as it is JavaScript-based and its event-driven model is designed to be very fast and efficient. Another reason to use Node.js is that learning JavaScript allow designing full-stack applications (both backend code running on server and frontend code running on client/browser).

For those who don’t know Node.js, it is a JavaScript interpreter (based on Chrome’s V8 engine) which can run code without a browser (JavaScript is mostly a browser-dependent language). But along with JavaScript (or simply js), Node.js includes several features suited for server-side applications such as file-system access, HTTP and HTTPS client/server, sockets, etc.

Support Boards

Onion provides several supporting boards that make it easier getting started with Omega2+:

  • Mini dock: provides basic functionality (a micro USB connector which provides power and a serial terminal connection through an onboard serial-to-USB converter chip, a 3.3V regulator, reset switch and a USB type A jack);
  • Expansion dock:  provides all mini dock functionalities plus an onboard RGB LED and an expansion header which can connect other expansion boards (such as an OLED display expansion, ethernet expansion, 16-channel PWM expansion, relay expansion, GPS expansion, etc);
  • Power dock: provides all expansion dock functionalities plus an onboard 3.7V LiPo battery controller which enables using an external LiPo battery to power the Omega2 board (battery is not included);
  • Breadboard dock: this is a very basic dock which enables connecting the Omega2+ to a breadboard. The dock only includes a 3.3V regulator and a micro USB connector for power (no USB terminal included);
Figure 2 – Breadboard dock
  • Arduino dock 2: this dock is a mix of an expansion dock with an Arduino board. It includes an ATMega328 AVR microcontroller, a power jack, a micro USB jack (for power only), a USB type A jack, an Arduino header, expansion header, a serial terminal header and an onboard level-shifter (for Omega-AVR communication). Note there is no serial-to-USB converter chip on this dock, so the only way to get Omega’s command prompt is through WiFi connection (either by using the terminal available at web console or by using SSH). Arduino dock enables using AVR microcontroller as a co-processor so it can deal with external signals (such as analog values) and make them accessible to Omega’s Linux OS (either by using a simple UART communication or I2C connection, where Arduino is a slave and Omega is master). Another interesting feature is that you can wirelessly program Arduino!
Figure 3 – Arduino dock (release 2)

Getting Started with Onion Omega2+

The first thing to know when starting with the Omega2 modules is that there is no screen and no GUI. Despite running Linux, the modules doesn’t have any kind of embedded display controller, therefore there are two ways to interact with Omega2:

  1. By using its embedded web frontend;
  2. By using a terminal, which can be a serial terminal (serial port or serial-to-USB), a web terminal (available at the web frontend) or a secure shell (SSH) session.

Anyway, getting started with Omega2 is pretty straight-forward: just attach it to a dock and use a USB cable to connect the dock (micro USB) to a powered USB port (on your computer or a wall adapter). The module LED will start blinking and after about a minute it will be steady on meaning boot is completed. From there you can connect to the module’s WiFi hotspot, log in, configure it to connect to your local WiFi access point and you are good to go! The guys at Onion wrote a very good and detailed tutorial on how to setup the module.

Once setup is complete, you can access Omega2’s web interface by browsing its local address at omega-XXXX.local (where XXXX are the last four digits of your Omega2 module MAC address, which is on a sticker on the module cover) as shown below.


Figure 4 – Onion Console

Onion Console includes eight web applications, some of them are related to GPIO and expansion boards (OLED, Relay and PWM). There is a web terminal application which allows controlling and running programs through Linux shell, a web editor with a file navigator, so you can edit files residing within the module file system, a settings panel which allows controlling several features, checking system status, upgrading its firmware, changing WiFi settings, etc. A Webcam application which allows streaming video from a USB camera is also included, but didn’t work when I tried to install it.

Figure 5 – Omega2 Status

Figure 6 – Omega2+ on Arduino dock with OLED expansion

Node.js

Now it is time to get our hands “dirty”! Let’s start by installing Node.js: the first step is opening an Omega2 shell session, I am using SSH, but you can use the web terminal or a serial terminal connected over USB:

$ssh omega-0347.local -l root
root@omega-0347.local's password:




BusyBox v1.26.2 () built-in shell (ash)


   ____       _             ____
  / __ \___  (_)__  ___    / __ \__ _  ___ ___ ____ _
/ /_/ / _ \/ / _ \/ _ \  / /_/ /  ' \/ -_) _ `/ _ `/
\____/_//_/_/\___/_//_/  \____/_/_/_/\__/\_, /\_,_/
W H A T  W I L L  Y O U  I N V E N T ? /___/
-----------------------------------------------------
   Ω-ware: 0.1.10 b160
-----------------------------------------------------
root@Omega-0347:~#

Default root password is onioneer, you can leave it as that, but I strongly suggest you to change it for a stronger password!

Once you have access to linux shell, you can use most shell commands, so let’s try and see if node is installed, node -v should return node’s version:

root@Omega-0347:~# node -v
-ash: node: not found

Since it is not installed, we are going to install it. Onion Omega2 has its own package management system, opkg, and it is used either for installing new software packages, upgrading them or removing something that isn’t useful anymore. Make sure your opkg database is up-to-date by running opkg update before installing node:

root@Omega-0347:~# opkg update
root@Omega-0347:~# opkg install nodejs
root@Omega-0347:~# node -v
v4.3.1
root@Omega-0347:~#

Note that by running node -v we now get node’s current version (4.3.1). Keep in mind this is not the latest version (latest is 8.4.0 and LTS is 6.11.2), but that should not be an issue for most users. Documentation for Node.js 4.3.1 can be found here. You can also install Node’s package manager (npm) which allows access to a huge open source library and automates some steps and package dependencies when programming in Node.js. We are not using npm right now, but you should install it anyway.

Our first example can work on any Onion Omega2, no matter you have a specific dock or expansion dock, as we are just going to blink the onboard LED. This LED is connected to GPIO44 and it is usually controlled by the system. There are two ways user can control it: by using sysfs, a pseudo file system which allows controlling devices through files, or by using GPIO tools such as fast-gpio.

Access through sysfs is mostly relies on Onion LED driver which is designed to offer some fancy pre-built features such as blinking, oneshot, Morse coding, etc. Onion provided a lot of information on their webpage about Omega LED.

We are going to use fast-gpio, a command-line tool which can directly read/write onto CPU’s GPIO registers. This is the fastest way to deal with GPIO without having to write your own C to directly work on GPIO registers. fast-gpio also overrides system control over Omega LED (which doesn’t happen when you use gpioctl, another command-line tool based on sysfs). Before using fast-gpio, we have to install its package root@Omega-0347:/# opkg install fast-gpio and you are good to go.

Prior to start working on our first program, it is a good idea doing some basic test to check if everything is fine:

root@Omega-0347:/# fast-gpio set 44 1
> Set GPIO44: 1
root@Omega-0347:/# fast-gpio set 44 0
> Set GPIO44: 0

If everything was fine, the first command will set GPIO44 to “1”, turning the LED off, while the second command will set GPIO44 to “0”, turning the LED on!

Now let’s start to work on our program. First of all, we have to choose an editor, you can either edit the program file on your computer or directly on Omega2. If you choose to go editing on Omega2, you can choose among vi, nano (both are included and available at command-line) or you can choose to go with the web editor. We are using the web editor from now on, because it is very easy to use and includes syntax coloring. So let’s create a file under /root/ named LEDBlinker.js:

Figure 7 – Editing LEDBlinker.js with the web editor

Complete listing is shown below and it is also available at my GitHub repository.

exec    = require('child_process').exec;
var temp = 0;
var state = 0;
console.log("Omega2 LED Blinker!");
function intervalFunc() {
   if (state === 0) {
   state = 1;
   exec('fast-gpio set 44 1');
   } else {
   state = 0;
   exec('fast-gpio set 44 0');
   }
   if (temp == 11) {
   clearInterval(timer);
   exec('fast-gpio set 44 0');
   }
   console.log('blink '+temp+' '+state);
   temp++;
}


var timer = setInterval(intervalFunc, 500);

Once you saved the file, you can run it with the command: node LEDBlinker.js:

root@Omega-0347:~# node LEDBlinker.js
Omega2 LED Blinker!
blink 0 1
blink 1 0
blink 2 1
blink 3 0
blink 4 1
blink 5 0
blink 6 1
blink 7 0
blink 8 1
blink 9 0
blink 10 1
blink 11 0

Our next example makes a slightly better use of Node.js, it is a simple web controlled LED. We are going to use the Breadboard dock and wire an external LED to GPIO18 (in series with a 560R current-limiting resistor), but you can modify the program to use the onboard amber LED if you want:

exec = require('child_process').exec;
os = require('os');
const http = require('http');
const hostname = os.networkInterfaces()['apcli0'][0].address;
const port = 3000;
const server = http.createServer((req, res) => {
  if (req.url === '/?LED=ON') exec('fast-gpio set 18 1');
  if (req.url === '/?LED=OFF') exec('fast-gpio set 18 0');
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.write('<h2>A simple LED control server <br>with Onion Omega2+</h2>');
  res.write('<p>LED state: <a href="?LED=ON"> <button>ON</button></a>');
  res.write('  <a href="?LED=OFF"><button>OFF</button></a></p>');
  res.end();
});
 
 
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Running this new example with node LEDServer.js will result in something as:

root@Omega-0347:~# node LEDServer.js
Server running at http://192.168.0.14:3000/

Now all you have to do is to open you browser and navigate to the address shown. Figure 8 shows the working example!

Figure 8 – Controlling an LED with a browser

That’s if for now! I hope you enjoyed this quick review and we will be back soon with more interesting examples and projects using the Onion Omega2! See you!

References

Leave a Reply