You need a fast, simple and inexpensive way to prototype your IoT use cases?
This article describes how you can achieve this using
- The RAK8212 device with its built-in Quectel BG96 communication module
- Espruino: A JavaScript interpreter for microcontrollers
- Narrowband IoT (NB-IoT): A Low Power Wide Area Network (LPWAN) radio technology standard
- AWS IoT Core and Device Shadow Service (“Digital Twin“)
In this example, the RAK8212 device is used as a sensor that periodically sends temperature values to its digital twin in AWS IoT Core. For communication with AWS it uses the MQTT protocol over Narrowband IoT (NB-Iot). The state of a LED on the RAK8212 can be controlled by setting it on the digital twin in AWS IoT.
Setting up the thing in AWS Iot Core
Get yourself an account on AWS, navigate to “AWS IoT” and “Create a single thing”. Describing all the steps would go too far in this article, and AWS has good online documentation. So please follow the steps given in the AWS documentation.
In the end, you should have an IoT object (=thing), identified by a name, and a certificate for this object (e.g. 133c075337.cert.pem), a public key (e.g. 133c075337.public.key) and a private key (e.g. 133c075337.private.key). You need to download these three files and activate the certificate.
In the navigation menu of the AWS console, go to “Manage -> Things” and you will see all your things. Select the thing of your choice and, in the navigation menu of the thing, go to “interact”. Note the server address displayed as “Update your Thing Shadow using this Rest API Endpoint”. We will use this as the address of the MQTT server later.
Example Address: a136ivuau4uklv-ats.iot.eu-central-1.amazonaws.com
Also notice the MQTT topics below. They all contain the name of your thing:
Example MQTT Topic: $aws/things/klenk-iot-device/shadow/update
This shows that the name of my thing is “klenk-iot-device”.
Setting up the example code for RAK8212
The example code is hosted at github:
https://github.com/wklenk/rak8212-espruino-nb-iot/blob/master/data-logger-bg96-mqtt.js
Find the JavaScript object that holds the configuration for the NB-IoT radio network connection, e.g.
// NB1 connectivity settings for Vodafone Germany var connection_options = { band: "B20", apn: "vgesace.nb.iot", operator: "26202", debug: false // Print communication with BG96 module to console. };
Adapt this to fit to your NB-IoT network of choice.
Find the JavaScript object that holds the configuration for the MQTT connection to AWS IoT:
var mqtt_options = { // AWS IoT server: 'a136ivuau4uklv-ats.iot.eu-central-1.amazonaws.com', port: 8883, client_id: "klenk-iot-device" };
Adapt the MQTT server (same as address of API endpoint noted down above) and client_id (name of the thing).
Upload the cryptographical material to the BG96 module
There is still one step missing: As AWS IoT uses bidirectional authentication of the MQTT connection, you need to transfer the thing’s certificate, the private key and the file of trusted root CA certificates to the BG96 module.
This is described in depth in this blog post: Espruino on RAK8212: Uploading cryptographic material to Quectel BG96 https://wolfgangklenk.wordpress.com/2019/03/26/espruino-on-rak8212-uploading-cryptographic-material-to-quectel-bg96/
To code expects the files uploaded to have the following names:
- cert.pem – The certificate of the thing
- key.pem – The private key of the thing
- cacert.pem – The file holding the trusted root CA certificates
Starting the example code on the RAK8212
Using the Espruino IDE, connect to the RAK8212 via Bluetooth LE, send the JavaScript code to the RAK8212 device, and execute function onInit();
____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| 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 > >onInit(); Entering State Setup External Hardware =undefined External modules connected. BME280 wiring set up. Entering State Configure Modem Entering State Register To Network Entering State Open MQTT Network Entering State Connect To Server Entering State Get Current State Entering State Subscribe To Delta Updates Entering State Publish Telemetry Data Current temperature: 27.42 Entering State Sleep Entering State Publish Telemetry Data Current temperature: 27.52 Entering State Sleep Entering State Publish Telemetry Data Current temperature: 27.78 Entering State Sleep
The code is using a Finite State Machine, that transitions from state to state and can easily be extended (take care of memory limitations!). In case of a failure, the BG96 module is shut down, and the state machine is restarted.
The Digital Twin representation at AWS IoT
In the AWS IoT console’s navigation menu, go to “Manage -> Things” and you will see all your things. Select the thing of your choice and in the navigation menu of the thing, go to “shadow”.
Example Shadow State:
{ "desired": { "led": "on" }, "reported": { "temperature": "27.57", "led": "on" } }
The code on the RAK8212 device will periodically update the temperature value every 60 seconds, and will also report the current state of the (blue) LED on the RAK8212.
Switching on/off the LED using the Digital Twin
You can switch on/off the (blue) LED on the RAK8212 by changing the LED’s desired state in the Digital Twin representation.
On the AWS IoT console, the is a page called “MQTT Client” that can be used to subscribe to or to publish to a arbitrary MQTT topic.
For switching the LED on and off, we need to public the desired state to the topic $aws/things/klenk-iot-device/shadow/update
Send a JSON object like this:
{ "state" : { "desired" : { "led" : "off" } } }
The RAK8212 should output a line like these on the console and switch on/off the (blue) LED accordingly
+QMTRECV reports message on topic: "$aws/things/klenk-iot-device/shadow/update/delta" with payload: {"version":6981,"timestamp":1554561624, "state":{"led":"off"}, "metadata":{"led":{"timestamp":1554561624}}}"
hi Wolfgang, are you available to do contract work?
I concern this activities as hobby and just like to share my achievements with others. However, feel free to contact me via email.