This project is the second part of an assignment for the course of Internet of Things at the Sapienza University of Rome. The aim is to build a Virtual Environmental Station based on Amazon Web Services (AWS). It is suggested to inspect the first part because in this guide it will be assumed that you already know the basics of the argument.
The system is made by two RIOT-OS native boards that generate random environmental data and send them to AWS through MQTT-SN (Mosquitto) and an MQTT transparent bridge. In the section Useful links, you can find the links to my GitHub repository that contains all the code that I used and to a YouTube presentation.
First of all, you need to clone this repository and set-up the RSMB (Really Small Message Broker):
> git clone https://github.com/eclipse/mosquitto.rsmb
Then, essentially you have to follow step by step the instructions provided in the README, so:
> cd mosquitto.rsmb/rsmb/src
And after that, you have to create in the same folder a new file named config.conf that contains:
# Uncomment this to show you packets being sent and received
# Normal MQTT listener
listener 1883 INADDR_ANY
# MQTT-SN listener
listener 1883 INADDR_ANY mqtts
Even if this is the sample configuration provided in the README, it is suggested to use port 1885 for the MQTT-SN listener and port 1886 for the MQTT listener as standard ports. With these simple steps, you have configured your MQTT-SN broker, so you can run it with:
> ./broker_mqttsn config.conf
Now the broker is ready for connections
RIOT is a small operating system for networked, memory-constrained systems with a focus on low-power wireless Internet of Things devices. It is open-source software, released under the GNU Lesser General Public License (Wikipedia). First of all, obviously you have to download and set-up RIOT-OS and all the needed components: since it is a quite long but simple process, simply follow the RIOT-OS Tutorial to have a step-by-step guide for downloading and to have some useful tutorials to become confident with RIOT-OS. After that, you can clone this repository to get all the basic tools for this project. Be careful, it is important that you pay attention to the path of every file is mentioned in this guide! You will work in the folder named emcute_mqttsn that in my case is located at home/RIOT/examples/emcute_mqttsn; if for some reason you have a different path, you have to modify the following line in the Makefile (it is obviously located in emcute_mqttsn folder):
RIOTBASE ?= $(CURDIR)/../..
At this point, you need to create a bridge, some tap interfaces (two in this case) and you need to configure the global addresses for the communications:
- in the Home folder, type in terminal:
> sudo ./RIOT/dist/tools/tapsetup/tapsetup -c 2You can check that everything has gone well, typing:
> ifconfig | grep tapIf you will have some troubles with the tap interfaces, consider that with:
> sudo ./RIOT/dist/tools/tapsetup/tapsetup -d
you can delete and set the interfaces again! (The oldest and most powerful solution...)
- assign a site-global prefix to tapbr0:
> sudo ip a a fec0:affe::1/64 dev tapbr0
On the other side, we have to set the communication in the RIOT board, so in the folder RIOT/examples/emcute_mqttsn:
> sudo make all term PORT=tap0
- then, in the RIOT board:
> ifconfig 5 add fec0:affe::99
This is for the first RIOT board, but since in this project it is expected to have two virtual stations, when you will run the second board you have to choose another tap, for example tap1, and you have to change the final part of the address, for example with 100 instead of 99.
Before going on, give a look to the code. For this project, it has been used for the most part the sample file available in the RIOT-OS repository with some adjustments where needed. This file essentially provides a native board with the basic functions and you can simply add any other command you need. In this case, it has been lightly adjusted the command cmd_pub and have been added the commands basic_pub and cmd_start, for having a more comfortable way to run the process.
The function random_values provides random environmental values generated in a very simple way; obviously, you need to change the topic on which you will publish your own data if you have one named differently.
- now you can start the process typing in the RIOT board:
> start fec0:affe::1 1885 station1
Note that 1885 is the port chosen for the MQTT-SN listener and station1 is the ID of one virtual environmental station. When you will run the second station you will use also port 1885, but of course ID stations. From this moment, the system starts to generate random values and sends them to the broker, every 10 seconds.
Lastly, you have to set up an MQTT transparent bridge since the direct communication between MQTT-SN and AWS is not supported. You can do this in several ways: in this project has been used a Python script in order to act both as a transparent bridge and to store data in DynamoDB, but notice that you can do it also simply with a .conf file even if in this guide we will not discuss that method. Since when you run the bridge you use some default utilities provided by Mosquitto, located at mosquitto.rsmb/rsmb/src/MQTTSClient/Python, it is recommended to not use Python3 because otherwise you will need to do some adjustments to those utilities. A great part of the code is the same as the previous part of the project; so essentially you have a function to set up the connection with AWS and some functions to publish and store data. If you need further information give again a look to the first part!
What is new is the Callback class: it allows through the function messageArrived to publish and store every message (payload) arrives. The function messageArrived deals with Mosquitto's utility so it is suggested to don't change the name of the arguments. Here you can maybe find some other useful information. You can now run the bridge taking care to place it in the correct folder: the bridge has to stay in the folder mosquitto.rsmb/rsmb/src/MQTTSClient/Python; also check that the MQTT-SN broker ran before is still working.
> python bridge.py
Now the transparent bridge will start to receive random payload from RIOT and to store them in the database. Notice that the environmental data will be displayed on the same web application (localhost) introduced in the first part!
The code is available in my Github repository and obviously if you want to use it you have to change some parameters like the endpoint and the path of the certificates. If everything has been done correctly, you can run the system in the following way (every step needs a separate terminal):
- (optional) Run the localhost page typing:
python3 manage.py runserverThe file is located in django_web folder
- Run the MQTT-SN Broker:
> ./broker_mqttsn config.conf
- Set-up the tap bridge and some interfaces (RIOT-OS section) and assign a site-global prefix
- Make, configure and run some RIOT boards with different IDs for each station (RIOT-OS section)
- Run the transparent bridge:
> python bridge.py
If everything worked properly you should see something like this: