In this post I'm going to present you how to setup the code to run the final product. Looking on my Github Page you can go to the LoRaWAN_Station folder and notice that we only have two important files here:
- TTNTBClient.py
- main.c
Here I'm going describe how set up two LoRaWAN stations with the purpose of sending data to Thingsboard simulating two Virtual Environmental Stations.
You may also be interested in visiting the Third Assignment page.
Requirements- pahp-mqtt @ https://pypi.org/project/paho-mqtt/
- Mosquitto @ https://mosquitto.org/
- TheThingsNetwork @ https://www.thethingsnetwork.org/
- IOT-LAB @ https://www.iot-lab.info/
Create an account on TheThingsNetwork, create an Application and then register two Devices.
In my case the application is called iotappan and the devices are called iotappan-dev-e and iotappan-dev-f and they use an OTAA activation method.In this tutorial I will use my devices, but you can substitute with yours.
It is also important for the system to properly work to adjust the payload format of the devices.
To do that go on the console > application > Payload Formats and simply add the following javascript code to the "decoder" section:
function Decoder(bytes, port) {
var result = "";
for (var byte in bytes) {
result += String.fromCharCode(bytes[byte]);
}
return {"string": result};
}
Create an account on IOT-LAB and configure your SSH access.Here we will operate in the IOT-LAB Saclay site. This tutorial will be based on a dedicated IOT-LAB Tutorial on how to set up a LoRaWAN connection using TheThingsNetwork and RIOT-OS with some little differences to make the system able to use two LoRaWAN nodes instead of one.
What is LoRaWANLoRaWAN: Long Range Wide Area Network is a communication protocol with high efficiency peculiarities. LoRaWAN is based on low power consumption and a wide communication range belonging to a technologies category named LPWAN - Low Power Wide Area Network, useful in terms of environmental sensors to use the less energy as possible.
In this tutorial we're going to book two ST B-L072Z-LRWAN1 boards on IOT-LAB and we're also programming them for our pupruses.
System ArchitectureThe system we're going to set up can be built in various steps using the specified platforms.
In few words, the LoRa boards from IOT-LAB are sending data through TheThingsNetwork.
On your local machin there will be a gateway that, connected to the network, will receive the wanted data and will forward them to ThingsBoard.
In my case, data will be visualized using a Web Application I've built in the first assignment tutorial.
The file TTNTBClient.py is responsible of the forwarding of the incoming data from TheThingsNetwork to ThingsBoard.
Once running, the client, by the paho module, subscribes to the TTN's broker at the topic +/devices/+/up using as username the name of the TTN Application and as password the Application Accees Key. Keep in mind that you will be your own credentials, different from mine.
Even if we have two boards sending data, our client will act as a single paho client: once subscribed to the related topic, the client will receive the incoming data from the boards as a json string. This string is then converted into a dictionary, so the sending board is identified and the payoload is then transmitted to the corresponding ThingsBoard device using MQTT protocol.
Remember to change the Thingsboard ACCESS_TOKENs with your TB devices as we saw in the first assignment tutorial.
The TTNTBClient can run on each machine with python, paho and an internet connection. Nothing more is required.
For simplicity we'll run the client on our local machine.
main.cThe main.c file is the core of our system. Or the base. Or the goal. In whatever way you want to call it, you have to know that is important. Here's why.
This file contains the instructions to generate the random values, simulating the environmental station, and also to connect to TheThingsNetwork.
After the compilation in RIOT-OS it will be generated a .elf file that is destined to be uploaded on the two different boards.
Let's set up all!As I've previously said, this will be similar to the one proposed by IOT-LAB.
1. Connect to the Saclay site host:
my_computer$ ssh <login>@saclay.iot-lab.info
2. Start an experiment with 2 nodes called riot_ttn
<login>@saclay:~$ iotlab-auth -u <login>
<login>@saclay:~$ iotlab-experiment submit -n riot_ttn -d 60 -l 2,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.
3. 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 and st-lrwan1-2.saclay.iot-lab.info.
<login>@saclay:~$ iotlab-experiment get -i <exp_id> -s
<login>@saclay:~$ iotlab-experiment get -i <exp_id> -r
4. Get the code of the 2019.01 release of RIOT from GitHub and also clone the project repository:
<login>@saclay:~$ git clone https://github.com/RIOT-OS/RIOT.git -b 2019.01-branch
<login>@saclay:~$ git clone https://github.com/PanK0/iot-project.git
<login>@saclay:~$ cd RIOT
Copy the main.c in the RIOT/tests/pkg_semtech-loramac folder:
<login>@saclay:~$ cp iot-project/LoRaWAN_Station/main.c RIOT/tests/pkg_semtech-loramac
It's IMPORTANT that the RIOT version is the 2019.01 to make sure that the main.c will be correctly compiled and will work.
5. 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-2017-q4-major
. Use the following command to use gcc 7.2 by default:
<login>@saclay:~/RIOT/$ export PATH=/opt/gcc-arm-none-eabi-7-2017-q4-major/bin:$PATH
Verify that you have the right gcc version:
<login>@saclay:~/RIOT/$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2017-q4-major) 7.2.1 20170904 (release) [ARM/embedded-7-branch revision 255204]
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
6. Build the LoRaWAN test application provided by RIOT.
<login>@saclay:~/RIOT/$ make -C tests/pkg_semtech-loramac clean all
7. Use the CLI-Tools to flash the ST LoRa node with the LoRaWAN firmware that you have just built. Here we use st-lrwan1-1 and st-lrwan1-2 but it may change in your case:
<login>@saclay:~/RIOT/$ iotlab-node --update tests/pkg_semtech-loramac/bin/b-l072z-lrwan1/tests_pkg_semtech-loramac.elf -l saclay,st-lrwan1,1
<login>@saclay:~/RIOT/$ iotlab-node --update tests/pkg_semtech-loramac/bin/b-l072z-lrwan1/tests_pkg_semtech-loramac.elf -l saclay,st-lrwan1,2
8. You can now access the RIOT shell running on your node using netcat:
<login>@saclay:~/RIOT/$ nc st-lrwan1-1 20000
[We'll operate now only on one board and then we'll setup the second one in the same way]
9. The shell provides the loramac
command to interact with the LoRaWAN stack running on the node:
> help
help
Command Description
---------------------------------------
loramac control the loramac stack
reboot Reboot the node
random_init initializes the PRNG
random_get returns 32 bit of pseudo randomness
> loramac
loramac
Usage: loramac <start|get|set|join|tx>
10. Now find the Device EUI, Application EUI and Application key information in the Overview
tab of the iotlab-node
device on the TTN web console.Then set them to the RIOT firmware (replace the values with yours):
> loramac set deveui 00000000000000
> loramac set appeui 00000000000000
> loramac set appkey 0000000000000000000000000000
You can also set a fast datarate, e.g. 5, corresponding to a bandwidth of 125kHz and a spreading factor of 7, since the nodes are very close to the gateway:
> loramac set dr 5
11. Now that the node has the required information set, it is time to join it to the network using the OTAA
> loramac join otaa
Join procedure succeeded!
On the TTN web console, go to the Data
tab of the iotlab-node
page. You should see the activation message received by the TTN backend (the item is clickable).
12. Open a secondterminal and connect again to the Saclay site via SSH. Then repeat the procedure from step (8) conneting to the second board (in my case named st-lrwan1-2) and setting it with the data of the second TTN device.
13. Open a thirdterminal on your localmachine and run the TTNTBClient.py with:
my_computer$ python TTNTBClient.py
Now the client is running and ready to receive data.
Start the connectionOn the first and on the second terminal start the sending data:
> loramac start
If everything has gone well, you should see something like this on both terminals:
Note that here are displayed my devices data.
Now the client running on your local machine will receive the payloads from the boards and will forward them to ThingsBoard.
In my case, data will be available on the Web Application.
Comments
Please log in or sign up to comment.