Espruino on RAK8212: Uploading cryptographic material to Quectel BG96

Some IoT platforms like AWS IoT require you to authenicate with X.509 client certificates when using MQTT as communication protocol for your devices.

The Quectel BG96 module on the RAK8212 already has an embedded MQTT stack, so there is no need for using a MQTT library on the device’s MCU.

The BG96 module supports client and server side authentication using X.509 certificates. The following files must be provided:

  • The MQTT client’s client certificate (e.g. 3a34634a38-certificate.pem.crt on AWS IoT)
  • The private key of the client (e.g. 3a34634a38-private.pem.key)
  • The trusted root CA certificates

The challenge is: How can these files be transfered to the BG96 module’s file system?

Having Espruino installed on the RAK8212, this is not a complicated task. Check my github account at

https://github.com/wklenk/rak8212-espruino-nb-iot

for the JavaScript file upload-ssl-certs-to-bg96.js

All three files have to be provided in PEM format. As this format is in ASCII, you can just cut and paste the contents to the JavaScript source code.

Then transfer the code to the device (using the Espruino IDE) and call function

uploadCertificates();

The files will be saved to the device as cert.pem, key.pem and cacert.pem and can be used in a later step to configure the SSL connection options for the embedded MQTT stack.

 ____                 _
|  __|___ ___ ___ _ _|_|___ ___
|  __|_ -| . |  _| | | |   | . |
|____|___|  _|_| |___|_|_|_|___|
         |_| espruino.com
 1v99 (c) 2018 G.Williams
Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
http://espruino.com/Donate
>
>uploadCertificates();
Connecting Cellular Modem ...
=undefined
Cellular Modem connected.
Files in file system: +QFLST: "cacert.pem",1187
+QFLST: "cert.pem",1224
+QFLST: "key.pem",1679
+QFUPL line: +QFUPL: 1224,380d Uploaded cert.pem
+QFUPL line: +QFUPL: 345,1203 Uploaded key.pem
+QFUPL line: +QFUPL: 1187,2d19 Uploaded cacert.pem
Successfully uploaded SSL certificates to BG96 module.
>

Espruino on RAK8212: Accessing the NB-IoT (NB1) modem

This blog post ist about how to communicate with the NB-IoT (NB1) modem on the Quectel BG96 module, and play around with some interesting AT commands, revealing information about the NB-IoT radio network and the provider.

The Nordic nRF52832 microcontroller on the RAK8212 board only has one hardware UART, which is many cases (like this case) is used to create a serial connection with components on the BG96 module. For this reason, you should not interface with the RAK8212 board using a serial connection over the USB cable, because if you do, your serial connection will go away as soon as the nRF52’s (single) UART is switched to establish this board-internal serial communication. However, the Espruino firmware is able to emulate a serial port over Bluetooth Low Enery (BLE) by software. Either your notebook or PC is already equipped with BLE communication, our you can get yourself a small BLE dongle for the USB port.

Please check the section “Requirements” on this Espruino page for more information about BLE:
https://www.espruino.com/Quick+Start+BLE

I am using the Native Espruino IDE.

You can find some initial code for modem testing here:
https://www.espruino.com/RAK8212

We will use a slightly adapted code:

/* Example how to interface the modem using the "iTracker" and "AT" module. */

var at;
console.log("Connecting Cellular Modem ...");
require("iTracker").setCellOn(true, function (usart) {
  console.log("Cellular Modem connected.");
  at = require("AT").connect(usart);

  sendAtCommand("AT+GMR");
});

function sendAtCommand(command) {
  var data = "";
  at.cmd(command + "\r\n", 1000, function cb(d) {
    if (d === undefined || d == "ERROR") {
      console.log("Error:", d);
    } else if (d == "OK") {
      console.log(data);
    }
    else {
      data += (data ? "\n" : "") + d;
      return cb;
    }
  });
}

In the Espruino IDE, just connect to your RAK8212 module via Bluetooth LE, copy and paste this code to the right side of the IDE, and press the “Send to Espruino” button to transfer this JavaScript code to the RAM of the module.

What does this code do?

  • It internally “wires up” the nRF52’s UART with the UART of the BG96 module. For this, it uses a method of an Espruino module named “iTracker”.
  • It loads a utility module named “AT” that deals with non-blocking sending and receiving AT commands to modem-like devices over a serial line and tells this module which serial interface to use (“Serial1” on RAK8212).
  • It sends the “AT+GMR” command using the AT module and asynchronously collects the answer from the modem.

When uploading, the code will start to execute immediately and output something like

 ____                 _
|  __|___ ___ ___ _ _|_|___ ___
|  __|_ -| . |  _| | | |   | . |
|____|___|  _|_| |___|_|_|_|___|
         |_| espruino.com
1v99 (c) 2018 G.Williams
Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
http://espruino.com/Donate
>Connecting Cellular Modem ...
Cellular Modem connected.
AT+GMR
BG96MAR02A05M1G
>

The AT command sent via the nRF52’s UART to the modem was “AT+GMR”.
The firmware version returned was BG96MAR02A05M1G.

A full list of AT commands of the Quectel BG96 module is publicly available. Just search for it on Quectel’s website.

One of the key advantages of Espruino is that we can use the JavaScript interpreter on the device interactively, so now let’s play around and try out some other AT commands:

Let’s use the JavaScript function sendAtCommand() to send some further AT commands:

Display Product Identification Information

>sendAtCommand('ATI');
=undefined
ATI
Quectel
BG96
Revision: BG96MAR02A05M1G

Get IMEI

>sendAtCommand('AT+GSN');
=undefined
AT+GSN
8664250305XXXXX

Creating a NB-IoT (NB1) connection

If you have a SIM card that allows to create NB-IoT (NB1) connections, the following sequence of AT commands can be used to check if you actually can use NB1 communication:

This example should work in Germany for Vodafone (values for 1NCE (Germany) in comment):

>sendAtCommand('AT+QCFG="band",0,0,80000,1'); // LTE Band 20 (1NCE: 80 for Band 8)
>sendAtCommand('AT+QCFG="nwscanmode",3,1');
>sendAtCommand('AT+QCFG="nwscanseq",030102,1');
>sendAtCommand('AT+QCFG="iotopmode",1,1');
>sendAtCommand('AT+QCFG="servicedomain",1,1');
>sendAtCommand('AT+CGDCONT=1,"IP","vgesace.nb.iot"'); // "iot.1nce.net" for 1NCE
>sendAtCommand('AT+CFUN=1');
>sendAtCommand('AT+COPS=1,2,"26202",9'); // 26201 for 1NCE

It may take some minutes now for the modem to manually register at the network provider.
The modem LED should flash on-off-off-off periodically to indicate network search

With AT+CEREG?, you can check the current network registration status:

>sendAtCommand('AT+CEREG=2');
=undefined
AT+CEREG=2
>sendAtCommand('AT+CEREG?');
=undefined
AT+CEREG?
+CEREG: 2,5,"BB12","98","118F666",9

Status code 5 means that the modem has registered itself to the network in “roaming” modus. Check the specification of BG96 AT Commands for details about other status codes.
Two-byte tracking area code is “BB12”,
Four-byte E-UTRAN cell ID is “118F666”,
Access technology is LTE Cat NB1 (9).

Query some network information

>sendAtCommand('AT+QNWINFO');
=undefined
AT+QNWINFO
+QNWINFO: "CAT-NB1","26202","LTE BAND 20",6290

So it acutally seems we have a NB1 connection using LTE Band 20.

Query the name of the registered network

>sendAtCommand('AT+QSPN');
=undefined
AT+QSPN
+QSPN: "Vodafone.de","Vodafone","DATA ONLY",0,"26202"

Query signal strength

>sendAtCommand('AT+QCSQ');

=undefined
AT+QCSQ
+QCSQ: "CAT-NB1",80,-85,134,-6

Espruino on RAK8212: Getting started

The RAK8212 is a versatile developer board ideally suited for prototyping IoT use-cases. LTE NB-IoT is one option to send telemetry data to a IoT platform or receive configuration data and firmware updates over the air.

iTracker-BG96-Remote-Positioning-Sensor-Node-Module-NBIoT-GPS-Tracker-Module-BLE-Bluetooth-5-0-GPRS

The board already includes a lot of sensors to play around with and cover a lot of typical IoT use-cases:

The NB-IoT connectivity is provided by the Quectel BG96 module.

Espruino is an open-source JavaScript interpreter for various microcontrollers (including the RAK8212) created 2012 by Gordon Williams. As JavaScript is used for programming the IoT use-cases and the applications can easily be pushed to the device using a Web IDE, Espruino is an ideal companion for the RAK8212.

In the following sections I try to help you (the reader) to get started with setting up Espruino on a RAK8212 module on a Windows PC.

Wiring up the J-Link Adapter

For initial flashing of the Espruino firmware you need a J-Link Adapter. This adapter has to support the Serial Wire Debug (SWD) interface.
Check the documentation of your J-Link adapter for the SWD connector pinout.
For Segger J-Link adapters, one source of information could be here: https://www.segger.com/products/debug-probes/j-link/technology/interface-description/

Connect the connector pins of the J-Link adapter with the related pin headers on the RAK8212 adapter board.

Connector SWD pinout | RAK8212 header pin label
VTref                | VDD
SWDIO                | SWDIO
SWCLK                | SWCLK
RESET                | RESET
GND                  | GND

When finished, attach J-Link adapter to the Windows PC using the USB cable.

Don’t worry, you don’t need to have this pricey J-Link adapter shown in the picture. A J-Link Edu Mini for about €20 will do the job, too.

jlink-wired-up

Download the Espruino firmware

You can download the Espruino firmware for the RAK8212 module on Espruino’s download page.

Under “Find a binary” choose the board “iTracker RAK8212”.

Download cutting edge build espruino_1v99_RAK8212.hex
Make sure to “save the link as” a file, and not get the firmware displayed in the browser and saving it from the browser.

Flash the Espruino firmware

Download, install and start Nordic nRFgo Studio on the Windows PC.

Then we select “nRF5x programing” in the Device Manager panel, and the following UI will be shown in front of us:

nrfgo_flash1

Press button “Erase all” to erase the firmware which had been programmed into the RAK8212 module before.

Select notepad tab “Program Application”, locate the file espruino_1v99_RAK8212.hex and press button “Program”. Flashing the firmware may take some minutes.

nrfgo_flash2

In the “Log” section on the bottom of the nRFgo Studio application, there should now be a statement like “Application espruino_1v99_RAK8212.hex programmed successfully.”

Set up serial terminal session

As you have plugged in the USB connector cable of the RAK8212 adapter board into your PC, a COM port should have been assigned to the serial interface. You can find out the assigned COM port using the Windows Device Manager. In my case, the COM port is COM19.

rak8212_adapter_board_com_port

Setup a serial connection to this COM port using the terminal application “putty”.
Enter the COM port name as “Serial line”, 9600 as “Speed” and “Serial” as “Connection type”.
Press the “Open” button to start the serial terminal session.

putty_setup

After pressing the RESET button the the RAK8212 adapter board, you should get an output in the terminal as follows:

espruino_terminal_start_banner

You now no longer need the J-Link adapter and can remove it from the RAK8212 adapter board.

Set up Web IDE

Download the Espruino Web IDE for the Chrome browser following the instructions here: https://www.espruino.com/Web+IDE

Run Espruino Web IDE from Chrome’s home screen (or the App Launcher).
Click the orange Connect/Disconnect icon in the Top Left.

You should be asked for the right COM port to use now. If everything goes well, the Web IDE will connect to your RAK8212.

espruino_web_ide_1
You can now start to play with some JavaScript examples and send them down pressing the button “Send to Espruino”.

espruino_web_ide_2

Now have fun with trying out some example programs and get familar with JavaScript programming in Espruino!