This project is intended to be the logical continue of google-cloud-based-iot-system tutorial, as study project of IoT course at Sapienza - University of Rome.In this section we are going to change the node.js device with a virtual randomized device running on RIOT-OS, this device will interact with Google Cloud through MQTT and MQTT-SN protocol.Our purpose is build an MQTT-SN broker, in this case RSMB, where the RIOT-OS applications are publishing their values over UDP/IPv6, and then an MQTT-SN/MQTT transparent bridge. The goal of this bridge is to subscribe to a predefined set of topics on RSMB and forward all messages received to the same topic on the MQTT broker. Google Cloud does not support direct connection with RSMB so we need to develop another bridge that forwards data from RSMB to the Google MQTT broker.
DEPENDENCIESIn this project there are few dependencies needed, this can be easly obtained following the official guide linked here:
IMPLEMENTATIONRIOT
virtual
devices
We are going to use emCute (MQTT-SN) module in RIOT as base for our application.To set up the environment just follow the istruction on emCute repository.The goal of this device is simply generate random values and then publish on a specific topic.
In those picture we can see our primary function that is divided in two parts, we basically get the topic and device id from arguments, then generate a value related to the device and finally publish the data.As you can see to start the polling publish we use a basic shell implementation given from the emCute base
To run the device follow this steps:
//SETUP NETWORKING AND INTERFACE
sudo {PATH-TO-RIOT}/dist/tools/tapsetup/tapsetup -c 2
sudo ip a a fec0:affe::1/64 dev tapbr0
//RUN DEVICE
cd {PATH-TO-RIOT-CLIENTS}
make all term PORT=tap0 BOARD=NATIVE
//FROM RIOT APPLICATION SHELL
ifconfig 5 add fec0:affe::99
con fec0:affe::1 1885
sub <topic_name>
polling_pub devices random 20
//USAGE => polling_pub <topic> <device-id/random> <polling time>
//N.B. using random as device the process will be totally randomized
MQTT-SN/MQTT bridge
Now that the RIOT device is up we need to establish a connection to the MQTT broker, Google IoT does not support directly mosquitto as AWS does but we can bypass this problem through a middle client that receives from the device (MQTT-SN) sending to Google (MQTT).I've used the very usefull python client provided by mosquitto, this client use our custom class "MQTTbridgeGoogle" to implements the MQTT connection while the connection to MQTT-SN is given. In main function we simply connect on a generic "linh" client over port 1885, subscribe on devices and remain connected.
In MQTTbridgeGoogle.py the process is pretty similar of the one explained in the first tutorial in Node Virtual Device. First of all we use jwt to sign in a token and finally define a method get_client that will be used in MQTTSNclient.py to connect with google.
For more details on MQTTbridgeGoogle follow this guide and be sure to download the specific root.pem necessary to ensure TLS connection.To run this MQTTSN-MQTT bridge follow those steps:
cd {PATH-TO-MQTTSclient}
pip install jwt
pip install paho.mqtt
python MQTTSNclient.py
Comments