2 | How to develop an IoT device using RIOT-OS and connect it to Thingsboard using MQTT-SN

Francesco Colasante
6 min readApr 2, 2020

--

In this tutorial we will built on-top of the cloud-based components developed in the first part: How to setup an IoT system using ThingsBoard. If you have not already read it, I highly recommend you to do it.

Now we will replace the virtual environmental stations developed using Python with new ones built using the RIOT-OS and MQTT-SN protocol. You will use the native emulator of RIOT-OS to run your stations and generate values over MQTT-SN which need to arrive to the cloud via the MQTT. Afterward, you will take your first steps into the IOT-LAB ecosystem, to execute your RIOT-OS application on real devices.

IoT cloud-based system using Thingsboard, MQTT, RIOT-OS and IoT-LAB

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 tutorial and video.

git pull
git submodule update --init --recursive

I added 2 new important folders:

  • brokers : it contains all configurations of the broker and the gateway. In addition, there are 2 submodules to fetch directly source code of mosquitto.rsmb and paho MQTT-SN Transparent Gateway.
  • device/riot : contains the RIOT source folder for the IoT device and a small script set_network.sh to easily configure network interfaces on native deployment.

RIOT-OS

RIOT is a real-time multi-threading operating system which supports a range of devices which are typically found in the IoT. It is based on the following design principles:

  • energy-efficiency and real-time capabilities;
  • small memory footprint and modularity;
  • uniform API access: independent of the underlying hardware (POSIX).

In order to have an overview I suggest you to check the official RIOT references: Getting Started, GitHub repository, documentation. Follow the Getting Started guide and take note of your RIOT base folder.

Thingsboard

IoT devices are no longer directly connected to Thingsboard MQTT, hence we need to change our default topic. Now, we will publish telemetry over:

Topic: v1/gateway/telemetry

In addition, the payload is different from before:

{
"Device A": [
{
"ts": 1483228800000,
"values": {
"temperature": 42,
"humidity": 80
}
}
],
"Device B": [
{
"ts": 1483228800000,
"values": {
"temperature": 42,
"humidity": 80
}
}
]
}

Check this link for the complete Thingsboard MQTT Gateway API Reference.

Therefore we add a new device on Thingsboard to sure to select the is Gateway checkbox

Thingsboard gateway configuration

Following that, copy the ACCESS TOKEN of Tutorial Gateway and paste into brokers/conf/bridge_gateway_[prod|dev].conf at the row remote_username ; in addition, if you have configured Thingsboard on cloud, change also address with your PUBLIC IP.

We do not have to configure anything else on Thingsboard, we will leave everything as it was in the first tutorial.

RIOT Firmware

My RIOT firmware is based on the official example emcute_mqttsn. I added some module on the Makefile as follow:

DRIVER ?= lps331ap        #to use directly sensors
USEMODULE += $(DRIVER)
USEMODULE += saul_default #sensors API
custom shell commands on riot

Check on GitHub repository for more details. It is important to set #define SENSOR 0 when you compile on native board and 1 when you run on real iotlab-m3 devices. Remember to disable manually the lps331ap module on Makefile .

N.B.: I created a separated thread using the riot API thread_create in the function cmd_pub_data in order to continuously publish data.

MQTT-SN

MQTT-SN is a publish/subscribe messaging protocol for wireless sensor networks (WSN), with the aim of extending the MQTT protocol beyond the reach of TCP/IP infrastructure for Sensor and Actuator solutions.

MQTT-SN Architecture

A very basic MQTT-SN gateway is rsmb available as a submodule in my repository and it is installed by default on iotlab-a8 devices on IoT-LAB.

In my configuration, I decided to use paho MQTT-SN Transparent Gateway which works both as a MQTT-SN broker and as a MQTT-SN transparent gateway.

Hence, I modified the default gateway.conf as follow:

BrokerName=localhost
BrokerPortNo=1884
GatewayUDP6Bind=fec0:affe::1/64
GatewayUDP6Port=1885

Now, install it and copy my custom configuration (assuming that you have fetched the submodule)

cd brokers/MQTTSN-Gateway
./custom_install.sh
mv gateway.conf gateway.conf.bkp
cp ../conf/MQTTSN-Gateway.conf gateway.conf

This configuration is scalable and “in addition” I did not have to write a line of code. At the next stage, you can run mosquitto as bridge which connects paho mqtt-sn gateway to the remote MQTT broker on Thingsboard.

mosquitto -c brokers/conf/bridge_gateway_prod.conf

Open another terminal and run the paho transparent gateway:

cd brokers/MQTTSN-Gateway
./MQTT-SNGateway

Following that you can finally compile the RIOT firmware (check your RIOTBASE on Makefile) and then run it.

cd device/riot/sensors_mqttsn
./start_network.sh
PORT=tap0 make clean all term

Now you will have a scenario like this:

RIOT native, MQTTSN trasparent bridge and mosquitto

You can interact with the RIOT shell in order to connect to the MQTT-SN broker and push data.

ifconfig 5 add fec0:affe::99
con fec0:affe::1 1885
set_device "Device Piano"
pub v1/gateway/telemetry "{ 'Device Piano': [ { 'ts': 1585744760000, 'values':{'humidity': 42 }}]}" 1
pub_telemetry
  • set_device <device_name> : is a custom command which I wrote to setup dynamically the device name
  • pub <topic> <payload> is a default command available emcute_mqttsn
  • pub_telemetry : is a custom command which I wrote to directly push random data (in case of native applications) to <device_name>
  • pub_data : is a custom command to continuously push data using another thread. (Not use it for now, it is only a stub to multi-thread programming)
A RIOT execution
Multi-device dashboard
Device Dashboard

IoT-LAB

IoT-LAB provides a very large scale infrastructure suitable for testing small wireless sensor devices and heterogeneous communicating objects.

Watch my video to see how to run it locally and how to retrieve real sensors data on IoT-LAB

  1. Register to IoT-Lab.
  2. Configure SSH Access (link).
  3. Follow this tutorial

In order to build RIOT-firmware remotely you have access into your remote machine and then run:

ssh <username>@saclay.iot-lab.info
#mv to your src folder
source /opt/riot.source
BOARD=iotlab-m3 make all
iotlab-experiment submit -n riot_m3 -d 60 -l 1,archi=m3:at86rf231+site=saclay
iotlab-experiment get -i <exp_id> -r
iotlab-node --update <your_binary>.elf -l saclay,m3,<dev_id>
nc <dev_id> 20000

Check out my repository for a more complete guide about iot-lab. In the next tutorial I will go into more detail of the iot-lab network configuration. Therefore, we will replicate the same configuration on real iotlab devices.

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/

--

--

Francesco Colasante
Francesco Colasante

Written by Francesco Colasante

Sapienza University — Engineering in Computer Science

No responses yet