3 | How to connect an IoT device to Thingsboard using LoRaWAN, The ThingsNetwork and RIOT-OS
The purpose of this tutorial is to build a system that uses LoRaWAN to send sensors data from an IoT device without an Internet connection. We will build a RIOT-OS application that will be executed on the B-L072Z-LRWAN1 LoRa kit over IoT-LAB. We will use TheThingsNetwork to interconnect the sensor devices with the cloud infrastructure via the MQTT protocol. The element that we are going to setup are:
- a Python MQTT Transparent Bridge
- a Riot-OS firmware which retrieves real environment data
- The Things Network MQTT bridge
Architecture
The architecture of the project is shown in the image above. We have a device on IoT-Lab which runs a Riot-OS firmware. This device will send real sensor data to The Things Network using the LoRaWAN protocol. We will set up a Python MQTT Transparent Bridge which subscribes to +/devices/+/up
topic on The Things Network MQTT broker and publish modified data on v1/gateway/telemetry
topic on the Thingsboard MQTT broker.
What is LoRa?
LoRa is a Long-Range radio technology developed by Semtech. Here is a definition from Semtech’s LoRa FAQ:
LoRa devices offer compelling features for IoT applications including long-range, low power consumption and secure data transmission. The technology can be utilized by public, private or hybrid networks and provides greater range than Cellular networks. LoRa Technology can easily plug into existing infrastructure and enables low-cost battery-operated IoT applications.
The Things Network
The Things Network is a global, crowdsourced, open, free and decentralized internet of things network. The network allows for things to connect to the internet using little power and little data.
Prerequisites
First of all, you need to pull the update from my GitHub repository. If you have not already done it, I highly recommend you to check my previous tutorials and videos.
- How to setup an IoT system using ThingsBoard: blog-post, video, GitHub
- How to develop an IoT device using RIOT-OS and connect it to Thingsboard using MQTT-SN: blog-post, video, GitHub
In this tutorial, we will use 2 folders of my repository:
device/riot/sensors_lora
brokers/TTN_BT_Gateway
In this video you can quickly see what we are going to do:
Configure a TTN application
Before using the LoRa nodes on IoT-LAB, you need an account and an application with a device configured on TTN.
- If you do not already have a TTN account, create one from here.
2. Add a new application and register at least 2 devices in your TTN application by following this documentation. Keep the default Other-The-Air Activation (OTAA) procedure. The OTAA activation requires 3 informations:
- Device EUI: the device unique identifier is an 8 bytes array.
- Application EUI: the application unique identifier is an 8 bytes array.
- Application Key: the application key is a 16 bytes array.
3. Setup payload decoding through console > application > Payload Formats
and add the following JavaScript
code to the decoder section:
function Decoder(bytes, port) {I
let result = "";
for (let byte in bytes) {
result += String.fromCharCode(bytes[byte]);
}
return {"string": result};
}
Riot-OS firmware
I build a riot firmware which automatically connects to the LoRa Gateway and sends telemetry data by reading from the temperature and humidity sensors every PERIOD
.
The most important part is:
Before copying the source files to iot-lab, be sure to change the parameters for the OTAA activation.
In addition, you must also change the DEVICE_NAME
in the main.c
file.
Thingsboard
In this tutorial, you will have to make a few changes to the Thingsboard compared to the previous configuration. You just have to create a new gateway and take notes of the access token.
Now it is time to configure the transparent gateway and run it.
Transparent Gateway
I built a transparent gateway using the paho.mqtt
library. It is very simple, it connects to MQTT TTN broker and Thingsboard broker. The most important part is the callback on receiving messages from TTN. In this tutorial, I decided to write a transparent gateway from scratch and not configure mosquitto with 2 different connections because it is also necessary to modify the incoming packet.
TTN broker provides a complex packet and we only need to extract the payload_raw
field which is base64 encoded.
As we have seen in previous tutorials, when we use a Thingsboard gateway we need a particular payload. It is necessary to specify the timestamp of the telemetry data. I have not found the RTC driver on our device and the classic time library does not work so I decided to move the timestamp marking on the transparent gateway.
For this reason, the riot application sends the data with a placeholder <timestamp>
and the gateway retrieves the current timestamp and replaces it.
Configuration
Remember to replace the data entered in the mysecrets.example.py
file with your ones. Then:
mv mysecrets.example.py mysecrets.py
It is important that you do not share your private token and passwords.
In particular, you need to setup:
ACCESS_TOKEN_TB_GATEWAY = 'hwi5VuEHBYsqPvS6fAAA'
THINGSBOARD_IP = "64.227.26.128"
TTN_APPID = 'tutorialiot' TTN_KEY = 'ttn-accountv2.7SPdlSUMHyWyBwSR6x0Bxn5nuNWfXfPYaf1wD0aaaa'
The names are self-describing, with a little imagination you should understand what they refer to.
IoT-Lab setup
- Access to
saclay
:ssh <username>@saclay.iot-lab.info
- Clone RIOT repository (already done see Tutorial 2)
- Setup
gcc compiler
cd ~/RIOT
source /opt/riot.source
export PATH=/opt/gcc-arm-none-eabi-7-2017-q4-major/bin:$PATH
arm-none-eabi-gcc --version # to check whether is 7.x
mkdir ~/RIOT/examples/mylorawan
4. Copy RIOT firmware using scp
(from your pc to IoT-LAB).
cd device/riot
scp sensors_lora/* colasant@saclay.iot-lab.info:~/RIOT/examples/mylorawan
5. Compile your riot-firmware
make -C examples/mylorawan clean all
6. Launch new experiment (Keep note of your <dev_id>
):
iotlab-experiment submit -n riot_ttn -d 120 -l 1,archi=st-lrwan1:sx1276+site=saclay
iotlab-experiment get -r
7. Flash .elf
file and access to your device using netcat
iotlab-node --update lorawan.elf -l saclay,st-lrwan1,<dev_id>
nc st-lrwan1-<dev_id> 20000
Finally, we run our transparent gateway which acts as a forwarder
Dashboard
Now we can enjoy our data directly from the dashboard. Congratulations, you have been great!
This is a project for the Internet of Things course of the MSc Engineering in Computer Science at the Sapienza University of Rome.
Website: https://francescocolasante.it/