Hi, unfortunately for you, this project has been composed during a set of assignments. Therefore, to better understand this article, I highly suggest you read my previous ones about the first and the second part at these links:
- Environment station using google cloud IoT and MQTT
- Environment station using RIOT / Google IoT / MQTT
The goal this time is to replace the MQTT protocol and the short-range wireless medium with LoRaWAN and TheThingsNetwork. Indeed, this time I had to develop a new RIOT-OS application that can be executed on the B-L072Z-LRWAN1 LoRa kit, using TheThingsNetwork to interconnect the sensor devices with the cloud infrastructure via the MQTT protocol.
Here the demonstration video:
Structure overview- 2 (or more) B-L072Z-LRWAN1 LoRa kit running on the IoT-LAB testbed
- The Things Network infrastructure
- A simple gateway (or transparent bridge), implemented by Python, to collect data from the TTN infrastructure and send them to the Google cloud platform;
- The Google cloud platform, providing the Cloud IoT core and Pub/Sub API, used to manage the connection, the devices and the messages exchanged with the web application;
- The web application, built using nodejs + express and some other related frameworks;
- The Mongodb NoSQL database program for the storage;
Note: the code provided on my github is made to work with my credentials, on my personal google account. I did not upload the keys, If you want to run it, you need to make your own setup.Introduction to IoT-LAB
IoT-LAB is an open testbed which provides a very large scale infrastructure facility suitable for testing small wireless sensor devices and heterogeneous communicating objects.
It features over 1500 wireless sensor nodes spread across six different sites in France. Nodes are either fixed or mobile and can be allocated in various topologies throughout all sites. A variety of wireless sensors are available, with different processor architectures and different wireless chips.
Since we do not have LoRa compatible hardware, using a testbed is the best option to make tests on real hardware.
LoRaWAN technologyThe LoRaWAN® specification is a Low Power, Wide Area (LPWA) networking protocol designed to wirelessly connect battery operated ‘things’ to the internet in regional, national or global networks, and targets key Internet of Things (IoT) requirements such as bi-directional communication, end-to-end security, mobility and localization services. It defines the communication protocol and system architecture for the network while the LoRa physical layer enables the long-range communication link.
The nodes in a LoRaWAN® network are asynchronous and communicate when they have data ready to send whether event-driven or scheduled, avoiding the battery consumption typical of synchronous networks. Moreover, to optimize a variety of end application profiles, LoRaWAN® utilizes different device classes. The device classes trade off network downlink communication latency versus battery lifetime.
LoRaWAN® utilizes two layers of security: one for the network and one for the application. The network security ensures the authenticity of the node in the network, while the application security layer ensures that the network operator does not have access to the end user’s application data. The AES-128 encryption is used with the key exchange utilizing an IEEE EUI64 identifier.
TheThingsNetwork overview and setupThe Things Network is a contributor member of the LoRa Alliance®, which is a non-profit association of more than 500 member companies, committed to enabling large scale deployment of Low Power Wide Area Networks (LPWAN) IoT through the development and promotion of the LoRaWAN® open standard.
Gateways form the bridge between devices and The Things Network, while the backend systems of The Things Network are responsible for routing Internet of Things data between devices and applications. Indeed, Non-IP protocols such as LoRaWAN®, require some form of routing and processing before messages can be delivered to an application.
TTN and Bridge setupThe Things Network web interface makes these steps really easy:
- First, go to the https://www.thethingsnetwork.org/ and create an account;
- Go to the console and add a new application to your TTN account, if you need more information use this link https://www.thethingsnetwork.org/docs/applications/add.html
- Now, register in your TTN application a new device for each board that you are going to use. Keep the default Other-The-Air Activation (OTAA) procedure. The only thing you must set is the Device EUI, let the application generate the other parameters for you. Take a look here if you need help: https://www.thethingsnetwork.org/docs/devices/registration.html
The first step is done! It is time to set up the gateway. I will assume that you already read the instructions in the "Cloud platform setup" section of the first article, then:
- Also this time, the gateway is recognized by the Google cloud platform as a simple device, so you need to add it to the set of devices previously created. Here a refresh on how to do it: https://cloud.google.com/iot/docs/how-tos/devices?authuser=1;
- Put a copy of the root.pem certificate (previously generated) in the devices_RIOT/gateway folder (or simply change all the paths);
- TTN uses the AES-128 symmetric encryption, which means this time you need to store a simple string, for the symmetric key. You could simply save it as plaintext in a variable, so that anyone that looks at your code can read it! Let's do something a little more secure. Create a ttn_password.txt file inside the devices_LoRa/gateway folder, then copy and paste the password from the TTN console, as shown in the picture above.
- Congratulation, you found the key, now, as we have done so many times, let's change the setup for Google, on top of the gateway_LoRa.py file:
# SETUP
project_id = 'your_project_ID'
registry_id = 'your_register_ID'
cloud_region = 'your_region'
device_id = 'your_device_ID'
sub_topic = 'your_topic'
- Finally, run it by:
python gateway_LoRa.py
First of all, you must set up your ssh access to the IoT-LAB servers. These steps are very simple, and the IoT-LAB tutorial is well done, so I directly leave you a link to the documentation. https://www.iot-lab.info/tutorials/ssh-access/.
Now, we are ready for the setup:
- First, establish the ssh connection to the Saclay site host:
my_computer$ ssh <login>@saclay.iot-lab.info
<login>
is your account ID, which can be found on the profile page
- Once you are connected, clone the RIOT Github repository in the main folder:
<login>@saclay:~$ git clone https://github.com/RIOT-OS/RIOT.git
- Make and switch to a new directory (for example "mycode"):
<login>@saclay:~$ mkdir <dir_name>
<login>@saclay:~$ cd <dir_name>
- It is time to upload the code. Open a new terminal, then go to the devices_LoRa/device and "secure copy" all the files:
my_computer$ scp main.c
<login>@saclay.iot-lab.info:<name_directory>
my_computer$ scp Makefile <login>@saclay.iot-lab.info:<name_directory>
my_computer$ scp Makefile.ci <login>@saclay.iot-lab.info:<name_directory>
- Since RIOT does not support the arm GCC version installed by default on the SSH frontend, we need to provide a new version:
<login>@saclay:~/mycode$ export PATH=/opt/gcc-arm-none-eabi-7-2018-q2-update/bin:$PATH
- Then verify that you have the right GCC version:
<login>@saclay:~/mycode$ arm-none-eabi-gcc --version
- Let's compile, run:
<login>@saclay:~/mycode$ make clean all
- Now we have the Executable and Linkable Format (.elf) of the program, it is time to start a new experiment on the IoT-LAB testbed platform. Login and submit the experiment:
<login>@saclay:~/mycode$ iotlab-auth -u <login>
<login>@saclay:~/mycode$ iotlab-experiment submit -n <experiment_name> -d <time> -l <number_of_nodes>, archi=st-lrwan1:sx1276+site=saclay
I know, we all wish to copy and paste all these commands, avoiding to type them. However, in this case, remember to set the experiment name, time and number of nodes.
- The previous step will return an id, take care, you must store it, or if you lose it, you will need to restart everything!! Or maybe not, you are lucky, you can always check it on the site, under "My experiments". However, it is not the unique id that you need, there is a new one, the number/numbers of the node/nodes associated with your experiment. To find it, simply run:
<login>@saclay:~/mycode$ iotlab-experiment get -i <exp_id> -r
- Perfect, at this point, we have the code compiled and an experiment running. Let's flash the executable file on the B-L072Z-LRWAN1 LoRa kit:
<login>@saclay:~/mycode$ iotlab-node --update bin/b-l072z-lrwan1/device_LORA.elf -l saclay,st-lrwan1,<node_number>
Remember to put the right node number!
The nodes are running, now you can access each of them, one for each terminal window, using Netcat:
<login>@saclay:~/mycode$ nc st-lrwan1-<node_number> 20000
If, for any reason, the device has any type of issue, go to your IoT-lab page, find the experiment and the node, then restart it, as shown in the picture below.
Now, go to "Applications > appID > Devices > deviceID" on the TTN web console and take the Device EUI, Application EUI, and Application key information. Therefore, on the device running, set them to the RIOT firmware:
> loramac set deveui your_EUI
> loramac set appeui your_EUI
> loramac set appkey your_key
To avoid issues set also the datarate, IoT-LAB advices a value of 5 on its site:
> loramac set dr 5
Finally, join the network:
> loramac join otaa
Now, you can use the same command we have used in the previous assignment to start the autonomous sending od the messages:
> start <dev_name> <interval> <telemetry1> ... <telemetry5>
The old dashboard was quite simple, so I decided to make some changes and add new features.
First of all, I decided to restructure the database, which now has one different schema for each type of telemetry, and a new schema for the devices. So, each device has a set of references to the telemetries, which uses the deviceId as foreign key. Decreasing a lot the retrieval time and increasing the general scalability of the system.
It can now recognize new devices requiring the connection and directly add them. Furthermore, any time there is a new message, not only the telemetry value but also the identity of the device is shown on the home page.
In conclusion, to give also a pleasant visual interface, I have added some colorful graphs to show the last hour's values, while the old values are shown in a list as before.
The application is now able to retrieve data from the virtual devices, the RIOT-OS devices using the MQTT-SN protocol, and this new implementation with LoRaWAN®. Moreover, with the new dashboard, and the new colorful graphs, anyone can appreciate how beautiful it can become collecting data with the IoT 'things'.
some other useful links:
Comments