This is a project made for the Assignment3 of Internet of Things class at the Sapienza University of Rome.
This work is an extension of those already presented in:
For this reason, I strongly recommend you to see first the previous tutorials and then navigate through these new topics.
IntroductionIn this assignment, I've created a cloud-based IoT system that collects information from a set of virtual environmental sensors based on RIOT-OS using LoRaWAN, The Things Network, and MQTT protocols.
For the connection with LoRaWAN, I used the real hardware available on FIT/IoT-AB and TTN, while MQTT is managed by the cloud-based backend of Google IoT Core.
The following sections are a hands-on tutorial on how to set up and run the system.
TECHNOLOGY USED: Python, C, RIOT-OS, MQTT, LoRaWAN.
As you can see in the picture above our purpose is to make a connection between the RIOT based devices and LoRaWAN hardware connected to The Things Network, and then between the latter and the Google MQTT broker. To do this, I've created a simple TTN-Google Transparent Bridge in Python.
Before starting
It is good to note that, compared to the previous tutorials, I made two important changes in the structure to make the system more professional and closer to reality.
- I changed the payload abandoning the old structure (deviceId; value; date) for a single JSON object that sends data for all sensors. I also added the Wind Intensity sensor for a total of 5 sensors:
const payload = {
deviceId: "stationId",
temperatures: getRndInteger (-50, 50),
humidity: getRndInteger (0, 100),
wind_direction: getRndInteger (0, 360),
wind_intensity: getRndInteger (0, 100),
rain_height: getRndInteger (0, 50),
date: parseInt (Date.now () / 1000)
}
- I did a restyling of the dashboard by adding two charts for each sensor, one collects the data that arrives showing the trend, the other shows the progressive average. We will see better this aspect in the System in action section.
First of all we have to create and set up our application on the TTN platform. At the end of these simple steps, you will have an application with one device configured.
- Create an account on https://www.thethingsnetwork.org
- Add an Application following this Official TTN Guide
- Register a device in your TTN application by following this Official TTN Guide. Keep the default Other-The-Air Activation (OTAA) procedure.
Ok, this is all we need to start the experiment with LoRaWAN on IoT LAB.
Setup the RIOT-OS stationIn this section, we explore what I did to realize the RIOT-OS station. The code of my application is an extension of the one in the main repository of RIOT-OS, available here: RIOT pkg_semtech-loramac - GitHub
First I've created the function to generate the periodic random values to send to the dashboard.
As you can see, we create a JSON payload with random values and the id. Note that, unlike the previous tutorial, we don't send the date here because, unfortunately, the LoRa node on IoT-LAB does not give us the correct date. For this reason, we will add the date only when the payload will arrive on the transparent bridge.
Now to send the values we have to add a new command that will be executed later in the shell:
Well, we added the ttn_pub to the shell commands. This command is very easy, it starts an infinite loop that sends a new random payload every 8 seconds:
As a parameter, we have to give it an identifier that will be added to the payload.
This code is available on my GitHub repository (at the end of the page).
Our RIOT station is configured, we will see in the next section how to compile it on IoT-LAB.
Setup the LoRa node on IoT-LABIn this section, we will see how to submit this application with one LoRa node on IoT-LAB, flash a RIOT firmware for LoRaWAN and connect the node to our application in the TTN network.
- Connect to the Saclay site host:
ssh <login>@saclay.iot-lab.info
- Start an experiment with 1 node called riot_ttn
iotlab-auth -u <login>
iotlab-experiment submit -n riot_ttn -d 60 -l 1,archi=st-lrwan1:sx1276+site=saclay
- Remember the experiment identifier returned by the last command. It’ll be used in the commands shown below, <exp_id>. The requested experiment duration is 60 minutes. Wait a moment until the experiment is launched (state is Running) and get the nodes list. For the next steps of this tutorial we suppose that you obtained st-lrwan1-1.saclay.iot-lab.info
iotlab-experiment get -i <exp_id> -s
iotlab-experiment get -i <exp_id> -r
- Get the code of RIOT and of my applciation from GitHub. Note that, unlike the official LoRa node tutorial on IoT-LAB, here we clone the repository directly from the master branch, otherwise we can't compile our code:
git clone https://github.com/RIOT-OS/RIOT.git
cd RIOT/tests/
git clone https://github.com/valecor95/LORA_devices.git
- Important: RIOT doesn’t support the arm gcc version installed by default on the SSH frontend, e.g. 4.9. So we provide arm gcc 7.2 in /opt/gcc-arm-none-eabi-7-2018-q2-update. Use the following command to use gcc 7.2 by default:
export PATH=/opt/gcc-arm-none-eabi-7-2018-q2-update/bin:$PATH
- Build my LoRaWAN application:
cd LORA_devices/
make clean all
- Use the CLI-Tools to flash the ST LoRa node with the LoRaWAN firmware that you have just built and the access the RIOT shell via netcat:
iotlab-node --update tests/LORA_devices/bin/b-l072z-lrwan1/tests_LORA_devices.elf -l saclay,st-lrwan1,1
nc st-lrwan1-1 20000
- Now find the Device EUI, Application EUI and Application key information in the Overview tab of your device on the TTN web console. Then set them to the RIOT firmware (replace the values with yours), then set the datarate and join the device on ttn:
loramac set deveui 0000000000000000
loramac set appeui 0000000000000000
loramac set appkey 00000000000000000000000000000000
loramac set dr 5
loramac join otaa
- Now you are ready to run the periodic sending of data:
ttn_pub <device-id>
NOTE: the complete tutorial to use LoRaWAN node in IoT-LAB is available here:
Now we are able to send the values from the RIOT station to The Things Network via LoRaWAN. But this is only the first part of the system, in the next section we will see how to forward the data from TTN to the Google MQTT broker.
The following topic is very important fo our system because it allows to forward the messages from The Things Network to the Google Platform and then to the web Dashboard. To do this we will build a transparent bridge from scratch.
The main idea is to combine a TTN client with a Google MQTT client using a python script. For this reason, the first thing to do is following these two guides that are those where we will base our code:
SetupTTN client
I'll show you only the TTN client that is the core of the bridge. The Google client (and also the following code) is available on my GitHub repository and it is the same of the previous tutorial and the above guide.
Ok, we start connecting our client to the things network (line 51-54), then we fill the fields with our Google data (line 57-66), we call the function google.get_client to build the client and to establish a connection with Google (line 74). Finally, we get into an infinite loop to receive the messages from the RIOT station.
The function depicted in the above picture is the one responsible for the forwarding of messages. First of all, it converts the payload from base64 to ASCII (line 21-22). This because TTN accepts only base64 payloads. Then convert the ASCII string to JSON, add the correct date, and reconvert the payload to string (line 25-29), because Google accepts only string messages. Finally it publishes the payload on the Google MQTT broker (line 32-33).
Run the bridge
- Open the Bride folder:
cd TTN_bridge/
- Install all the modules needed:
pip3 install
- Start the bridge (This procedure has to be done before to launch the RIOT devices):
python3 TTNclient.py
If everything goes in the right way, you will have a view like this:
- Run the RIOT station:
- Run old stations and see the value on the dashboard:
Comments