This project is related to Clever Weather, so it's strongly recommended to check that first. The implementation part takes for granted that you already have an Azure IoT hub, at least a device and a web app to visualize data.
This IoT app allows you to get values from sensors and send them to your Azure IoT hub using MQTT and MQTT-SN protocols. To do so there's a RIOT-OS app that creates random values and sends them to a RSMB (Really Small Message Broker) using the MQTT-SN protocol. A python gateway subscribe to the broker through MQTT protocol and directly sends the values to the IoT Hub. Finally the nodejs app allows you to visualize data in local.
ComponentsRIOT-OS app
A simple RIOT-OS app will be used to randomly create values and publish them to a MQTT-SN channel. In general many clients can communicate using MQTT-SN protocol (even python ones), but a C app was the most suitable implementation for this assignment.
Mosquitto RSMB
This ready-to-use broker can be accessed by both MQTT and MQTT-SN protocols.
PythonGateway
Thanks to the gateway the RSMB can communicate with the IoT hub, which is used like a MQTT broker. I have realized it in python because it directly implements the MQTT protocol, but you could choose another language.
Azure IoT Hub
You can use Azure simply accessing to Azure website using your microsoft credentials or creating one. Unfortunately Azure is free only for a limited time, unless you are a student, but in this case you have less functionalities. Azure has many services, but I will focus on the IoT hub. The IoT hub will be used as a MQTT broker, but it's not complete and it doesn't support all the standard behaviours.
Nodejs
Nodejs is used to run an application that lets you visualize the data in local. You have to install also the package @azure/event-hubs.
Implementation- RSMB
Really Small Messsage Broker can be easily installed cloning the repository and then compile it.
git clone https://github.com/eclipse/mosquitto.rsmb.git
cd mosquitto.rsmb/rsmb/src
make
The broker is almost ready to use, you just have to add the configuration file config.conf
in mosquitto.rsmb/rsmb/src
directory.
# add some debug
outputtrace_output protocol
# listen for MQTT-SN traffic on UDP port 1885
listener 1885 INADDR_ANY mqtts
ipv6 true
# listen to MQTT connections on tcp port 1886
listener 1886 INADDR_ANY
ipv6 true
In my case, the configuration file specifies that the broker has a port for MQTT-SN traffic (1885), a port for MQTT traffic (1886) and ipv6 is enabled. You will use the port numbers specified here to connect to the broker. At this point you can start the broker.
cd mosquitto.rsmb/rsmb/src
./broker_mqtts config.conf
This image shows the broker when it is started and when RIOT app and the gateway connect to it. In the last lines you can see that when the app publishes a message, it is sent to PythonGateway which is subscribed to the same topic.
- RIOT-OS
The first thing to do is set up the environment installing all the requirements described here and then you can clone the repository:
git clone https://github.com/RIOT-OS
To use RIOT as native you have to setup tap and tapbr devices:
./RIOTDIR/dist/tools/tapsetup/tapsetup
ifconfig | grep tap
Whit this command you have created 2 devices tap0 and tap1 and a tapbridge tapbr0. You can use grep
to check if they have been correctly created.
Now you can assign a site-global prefix to tapbr0 opening a new terminal and typing:
sudo ip a a fec0:affe::1/64 dev tapbr0
I have created my RIOT app starting by one of the already existing examples of the RIOT-OS folder, emcute_mqttsn. So you can go to RIOT/examples/emcute_mqttsn
directory and check for it. In the main.c
file I have added a function similar to the existing cmd_pub that generates random values and publish them every 5 seconds. Then I have added it to the list of functionalities of the main, so you can see it when you type help
.
You can execute it from native using device tap0:
make all term PORT=tap0 BOARD=native
or from device tap1:
make all term PORT=tap1 BOARD=native
And then assign a site-global address to RIOT native instance with the same prefix as before:
ifconfig 5 add fec0:affe::99
You can see all the functionalities of the RIOT app typing help
. The first thing to do is connect to the broker using the same address specified before and the port for MQTT-SN traffic:
con fec0:affe::1 1885
Now you can start publishing messages simply typing
publisher <topic-name>
I have used a device as topic name, so when the messages arrive at the IoT hub, it will look like that device published them.
The image shows what you see when the app is connected to the broker and you type help
. You can notice the publisher
command, the one that I have added, that allows you to start publishing messages on a topic of your choice (in this case "foggia_2").
- Python Gateway
Python gateway uses paho-mqtt library to connect to the RSMB and MQTT direct implementation to connect to the IoT hub. It subscribes to a topic on RSMB and, when a message arrives, it sends it to the hub. You could use also MQTT-SN to connect to the RSMB. At the moment my implementation subscribes only to two topics, which are also two devices of the IoT hub, so it looks like the message has been sent by that device. You will need a connection string for every device.
- Nodejs
You can run the web app simply going into the folder and typing in the command line:
npm start
If the gateway is connected, you will see the incoming messages in the command line:
FInally you can visualize the data typing http://localhost:3000
/
in a browser.
Communicate with your IoT hub using MQTT protocol
Comments
Please log in or sign up to comment.