If you followed the first part of my project, you will now have an MQTT broker running on a cloud server, a MySQL local database storing sensors info and some other scripts (one connecting DB to broker and another simulating some king of sensor). In the second part, we've seen how to replace the simple Python sensor with a RIOT-OS based one, using MQTT-SN and a transparent gateway to feed the broker. Today we are going to replace again the sensors with new ones, which will run on LoRaWAN devices through IoT-LAB and will communicate using The Things Network.
Let me introduce the technologies used in this part of the project:
IoT-LAB is a service which provides a very large scale infrastructure facility suitable for testing small wireless sensor devices and heterogeneous communicating objects.
LoRaWAN is a set of protocols built on top of the LoRa radio technology. It allows nodes to send messages on long distances and with potentially very low power consumption.
The Things Network provides a set of open tools and a global, open network to build an IoT application at low cost, featuring maximum security and ready to scale.
Here we have the main architecture of the project, with this new component
First of all you will need an IoT-LAB and a The Things Network account in order to emulate the sensors and make them communicate. You can do it clicking here and here. Once you have done, you will need to configure SSH access on the first service (here you can find a simple tutorial helping you to complete this task).
In order to make the transparent bridge working, you will need a specific python module, more than the ones of the previous parts (Paho and PyMySQL). You can install it typing this command on your terminal:
$ pip install ttn
The Things Network application and deviceBefore start running the new sensors you will need to create an application and a device on your TTN account. So, enter the console and click on Applications, then on Addapplication. Then follow the steps:
1. Choose an application id. Remember it because it will be needed next
2. Click on "Add application", then in Devices section and click on Register device
3. Choose a device id, click on "generate" button for Device EUI, then on register
Keep the device page open because you will need some information from here, in the next steps.
Message payloads are hexadecimal encoded, so you'll need to configure a decoder. This can be done in the Payload format tab in the application overview page. Copy this function and save the changes:
function Decoder(bytes, port) {
var result = "";
for(var byte in bytes) {
result += String.fromCharCode(bytes[byte]);
}
return { "string": result };
}
Now you will need to link the TTN device with your already created broker on the cloud server. The gateway is provided in my repository, so download it and go into riot_lora_station/transparent_bridge/. Here you'll find the python script. Open the config file and fill it with the correct informations.
- MQTT_TTN_USER: is the TTN ID of the previously created application
- MQTT_TTN_PASS: is your application access key (you can find it at the end of the application summary page on TTN website)
- MQTT_CLOUD_IP: is the IP address of the cloud broker
At this point you can run the script typing
$ python ./transparent_bridge.py
If you have followed the second part of my project, you probably have already downloaded the last RIOT-OS release. If this is not the case, you can do it clicking the link. Enter the directory and add the version 7.3 of arm gcc to the path, through this command:
$ export PATH=/opt/gcc-arm-none-eabi-7-2018-q2-update/bin:$PATH
You can check if all it's ok typing:
$ arm-none-eabi-gcc --version
Now you'll need to enter the repository directory again, but this time follow the riot_lora_station/station path. Then follow the steps:
1. Edit the Makefile and set your RIOT path
RIOTBASE ?= $(CURDIR)/<YOUR-PATH-FROM-CURRENT-DIR>
2. Compile the application
$ make clean all
3. Copy the elf file on your IoT-LAB home
$ scp bin/b-l072z-lrwan1/riot_lora.elf <USERNAME>@saclay.iot-lab.info:riot_lora.elf
The file is ready to be flashed.
IoT-LABNow you will need to create a new experiment on IoT-LAB, with two nodes and flash the file on both of them. Then you'll have to configure the connection and run the station.
1. Log into IoT-LAB through SSH
$ ssh <USERNAME>@saclay.iot-lab.info
2. Ask for authorization and run a new experiment
$ iotlab-auth -u <USERNAME>
$ iotlab-experiment submit -n <NAME-OF-EXPERIMENT> -d 60 -l 2,archi=st-lrwan1:sx1276+site=saclay
As you can see, we are executing it for 60 minutes on 2 nodes. The last command will return you an ID, you will need it in the next commands.
3. Wait until the experiment is fully running. You can look at this with:
$ iotlab-experiment get -i <EXPERIMENT-ID> -s
4. Note the node's ids. You can print them in this way:
$ iotlab-experiment get -i <EXPERIMENT-ID> -r
5. Flash the elf files
$ iotlab-node --update riot_lora.elf -l saclay,st-lrwan1,<DEVICE-ID>
You have to repeat this step for both the nodes you've created
6. Access the app shell
$ nc st-lrwan1-<DEVICE-ID> 20000
Note that the elf file will be flashed on two nodes, so you'll need to access them from two different terminals and repeat the following steps on both of them.
7. Connect the device to TTN. The shell allows you to configure connection parameters and join the network, through this commands. If you kept the device page open as I told you, the first three infos, will be very easy to retrive. Go back on the website and copy them. The dr parameter sets the datarate (it's 5 because the nodes are very close to the gateway).
> loramac set deveui <DEVICE-EUI>
> loramac set appeui <APP EUI>
> loramac set appkey <APP-KEY>
> loramac set dr 5
> loramac join otaa
8. Start transmission. Obviously the station ID should be different on the two IoT-LAB nodes, as they have to be unique.
> start <STATION-ID>
The stations will now be up. The stop command can be used to stop them.
Here you can find the other parts of my project:
Project developed as part my MSc degree in Engineering in Computer Science.
Useful links:
If you appreciated my work, please like or share it.
Comments