Hi, to better understand this article I suggest you to read the previous one about the first part at this link: Environment station using Google cloud IoT and MQTT
This time, building on-top of the cloud-based components developed in the previous article, I had to replace the virtual environmental stations with new ones built using the RIOT-OS and MQTT-SN protocol.
The introduction of this protocol requires also to add a new component, the transparent bridge, which allows the devices to subscribe to a predefined set of topics on the MQTT-SN broker and then forward all the messages received to the same topic on the MQTT broker that is controlled by the cloud-based backend.
To test the code, I have used the native emulator of RIOT-OS which can run my stations and generates random values over MQTT-SN that will arrive at the cloud via the MQTT. However, in this case, the code is also able to run on real hardware thanks to the RIOT-OS implementation.
Here the demonstration video:
Note: I have updated the repository on GitHub, now there is only one device and you must specify, after the name and interval, which telemetries it should send (so that we can run the same code on multiple terminals).Structure overview
Here the components:
- 2 (or more) devices implemented by RIOT-OS which are able to use either the native emulator to generate random values or real hardware;
- An MQTT-SN broker (mosquitto.rsmb in this case) to forward all the messages from the devices to the gateway;
- A simple gateway (or transparent bridge), implemented by Python, to collect data from the RIOT-OS devices and send them to the Google cloud platform;
- The Google cloud platform, providing the Cloud IoT core and Pub/Sub API, used to manage the connection, the devices and the messages exchanged with the web application;
- The web application, built using nodejs + express and some other related frameworks;
- The Mongodb NoSQL database program for the storage;
Note: the code provided on my github is made to work with my credentials, on my personal google account. I did not upload the keys, If you want to run it, you need to make your own setup.Introduction to RIOT-OS
RIOT is a small real-time multi-threading operating system for networked, memory-constrained systems with a focus on low-power wireless Internet of Things (IoT) devices. It is open-source software, released under the GNU Lesser General Public License (LGPL).
It is based on the following design principles: energy-efficiency, real-time capabilities, small memory footprint, modularity, and uniform API access, independent of the underlying hardware.
It supports multiple drivers, which allows the user to start out of the box. Moreover, the hardware-dependent code is reduced to a minimum and abstracted from the kernel itself.
These features, and the possibility to run on several platforms, including embedded devices as well as common PCs (with the "native board"), make RIOT-OS a great choice to build IoT systems.
Why MQTT-SN?MQTT-SN is designed to be as close as possible to MQTT, but it is adapted to the peculiarities of a wireless communication environment such as low bandwidth, high link failures, short message length, etc. It is also optimized for the implementation on low-cost, battery-operated devices with limited processing and storage resources.
However, MQTT-SN is not MQTT, therefore it needs a Transparent Gateway, which is a daemon, or small server, that accepts incoming MQTT-SN data over a number of transports and converts them into MQTT appropriate for connecting to an MQTT server.
Gateway setupAssuming that you have already completed the instructions in the "Cloud platform setup" section of the previous article, the following steps are required to make also this part work:
- The gateway is recognized by the Google cloud platform as a simple device, then you need to add it to the set of devices previously created. Here a refresh on how to do it: https://cloud.google.com/iot/docs/how-tos/devices?authuser=1;
- Put a copy of the root.pem certificate (previously generated) in the devices_RIOT/gateway folder;
- Open the gateway.py file and change the setup section on the top, to connect it with your profile:
# SETUP
project_id = 'your_project_ID'
registry_id = 'your_register_ID'
cloud_region = 'your_region'
device_id = 'your_device_ID'
sub_topic = 'your_topic'
- Then run:
$ python gateway_RIOT.py
In this section, I will show how to set up the project to make it run on the native board simulator, so that you can simply try it on your laptop without the needs of an external board.
- Since the code is based on the emcute_mqttsn example, also part of the setup is very similar. Therefore, to run the mosquitto RSMB (Really Small Message Broker), just follow the instructions on the "Setting up a broker" section at the following link: https://github.com/RIOT-OS/RIOT/tree/master/examples/emcute_mqttsn
- Since we are using the native board, we also need to set up the virtual network interfaces running these two commands inside the devices_RIOT folder:
$ sudo ./RIOT/dist/tools/tapsetup/tapsetup
$ sudo ip a a fec0:affe::1/64 dev tapbr0
- Pick one of the device folders and run it by:
$ BOARD=native make all term PORT=tap0
- At this point, we should have this situation, where on the left there is the device, on the top right the mosquitto broker and the bottom right the gateway
- To complete the setup, on the device terminal (already working!), run the following two commands:
> ifconfig 5 add fec0:affe::99
> con fec0:affe::1 1885
- Now, the device is connected to the broker and ready to use all the functions of the pub/sub paradigm. To see all the functionalities run the command
help
, otherwise, to directly begin the autonomous sending, just run:
> start <dev_name> <interval> <telemetry1> ... <telemetry5>
- Here there is the final situation
As we can see, the RIOT device is sending through the MQTT-SN broker the telemetries to the gateway, which will finally send them to the Google cloud platform.
ConclusionIn conclusion, the application is now able to retrieve data from both the virtual devices previously implemented and the new RIOT-OS implementation.
Some other useful links for the setup are:
For the next assignment:
Comments
Please log in or sign up to comment.