MQTT and Campus-wide heterogeneous Wireless Sensor Network



This project was done by T. Pratik. His project report can be found here.

Goal

Build a wireless sensor network (WSN) for environment monitoring with different hardwares that can communicate with each other using MQTT protocol.

Getting started

For this project, we used the following hardware and software

Hardware Software
Raspberry Pi Model 3B(built-in WiFi) – 1 no Arduino IDE
NodeMCU (built-in WIFi ESP-12e) – 2 nos Waspmote IDE
Arduino Nano with WiFi(ESP8266) – 2 nos Python
Waspmotes with XBee S1(IEEE-802.15.4) – 2 nos XCTU
WiSense buit-in CC2520(ZigBee,IEEE-802.15.4) – 2 nos Mosquitto

A quick introduction of MQTT

MQTT (MQ Telemetry Transport or Message Queue Telemetry Transport) is a publish-subscribe-based lightweight messaging protocol for use on top of the TCP/IP protocol

MQTT

Fig 1: MQTT communication




MQTT defines methods (sometimes referred to as verbs) to indicate the desired action to be performed on the identified resource. What this resource represents, whether pre-existing data or data that is generated dynamically, depends on the implementation of the server. Often, the resource corresponds to a file or the output of an executable residing on the server

MQTT is a device to device communication protocol using publish-subscribe model, consists of two main parts Broker and Clients. The Broker hosts an MQTT server and a Client may be a sensor or an actuator .

A client containing data publishes to the broker under a topic name, and a client interested in the data subscribes to the same topic name to get the data.

Raspberry Pi as MQTT broker (server)

A raspberry-Pi(Rpi) is a microprocessor embedded prototyping device. It has many versions, in this project we have used Rpi-3 which is powered by a 1.2GHz ARM Cortex A5 processor, has 1GB RAM, has inbuilt Bluetooth & WiFi, 4 USB ports and it can run a variety of operating systems including Windows and Linux. Hence, it can be considered as a small computer. We installed Raspbian Jessie, which is a flavour of Linux.

A Raspberry-pi was made the broker by installing mosquitto broker, which is a open source MQTT broker mainly used for research purpose. The broker always runs in the background and is started automatically on startup.

Raspberry Pi broker

Fig 2: The Raspberry-Pi which runs the Broker

Waspmotes as MQTT client

As mentioned earlier the waspmotes were sending data to the coordinator, the coordinator was attached to the Raspberry-pi and through serial communication, the data sent by the nodes were read.

A python MQTT-forwarder script was written, which read the incoming packets and forwarded them to the broker under respective topic names.

To transmit the data from the Waspmotes, first we need to run the waspmoteXBeeconfig.pde script. Once the configuration is done WaspMoteXbeedatasending.pde need to be executed in the Waspmotes. Waspmote codes were jointly written by T. Pratik and G. K. Sriharsha.

NOTE:

Arduino nano + ESP8266 as MQTT client

For implementing MQTT with Arduino we chose Arduino Nano due to its small size and less power consumption, in comparsion to Arduino UNO. For connectivity we attached ESP8266 to them for WiFi capability. Using PubSubClient library which is a light weight MQTT library for Arduino, the Arduinos were able to directly publish their data to the broker in SenML format, and the were able to publish their data from anywhere within the campus wherever IITMandiWiFi was available, and a node kept at IITMandi South campus can also publish data to the broker kept at IITMandi North campus.

Nano with ESP

Fig 3: Arduino-Nanos with ESP8266 implementing MQTT




Additional instructions related to this code:

		#include <WiFiEsp.h>
		#include <WiFiEspClient.h>
		#include <WiFiEspUdp.h>
		#include "SoftwareSerial.h"
		#include <PubSubClient.h>
		#include "LowPower.h"
		#include<ArduinoJson.h>
		#include"DHT.h"
		

The first four libraries enables the nano to establish a WiFi connection through ESP. SofwareSerial.h is required for the connection between nano and ESP board. PubsubClient.h is for MQTT. LowPower is for sleep mode. ArduinoJson.h for creating as well as parsing data in JSON format.

client.publish(“IITMandi/COMLab/Nano01/temperature”,pl,true)

NOTE: that donot connect ESP8266 directly to the Arduino nanos as they operate on different voltage levels (3.3V vs 5V). To avoid any damage either use voltage regular circuit or a breakout board.

NODE-MCU as MQTT client

Just to add a variety ,two NODE-MCUs were also programmed to publish their data to the broker. They can also publish their data from anywhere within thw campus wherever IITMandiWiFi is available.

NodeMCU

Fig 4: NodeMCU implementing MQTT




The code can be found here. The code is similar to the one that is written for Arduino nanos with external ESP boards. However, the implementation of sleep mode is different as the NodeMCU is directly connected to the ESP8266 and therefore implement ESP.deepSleep( ) function included in

#include<ESP8266WiFi.h>

Arduino nano + Xbee S2 as MQTT client

An arduino Nano along with an XBee S2 radio running 802.15.4 firmware, was programmed with proper configuration to add it to the network of Waspmotes, since it also sent sensor data to the same coordinator to which Waspmote node were sending, it can be said that the Arduino is also now a part of the Waspmote network, hence we achieved interoperability with devices manufactured from multiple manufacturers.

First, the XBee radio was configured with XCTU to set the Channel number, Network PanID ,API mode and Endpoint point, then it was directly attached to the arduino for serial communication using Arduino XBee library.

XCTU

Fig 5: An XCTU interface

Configuring external XBee module :

WiSense nodes as MQTT client

AS there was no MQTT library for the WiSense nodes, similar to Waspmotes, a forwarder script was used to post the data received from WiSense nodes.

WiSense Coordinator WiSense node

Fig 6: WiSense coordinator and sensor node

Implementing sleep modes

NOTE: The Arduinos along with ESP8266 radios, donot save much power mainly due to the power LED which is always on .So in order to take full advantage of sleep mode, it is advisable to get rid of them by desoldering.

Integration, Experimental evaluation and Result

The network consists of a central broker(Raspberry-Pi) and 8 clients (final network didn't haveS Arduino nano + Xbee client).

2 Waspmotes and WiSense nodes transmit data to their respective coordinator which later get published in to the broker with the help of a forwarder script. 2 Arduino Nano with ESP-8266 WiFi radios and 2 Node-MCUs publish directly to the broker.

Each of their payload consists of SenML-JSON format, as following :

{*‘bn’:‘location/name of node’;‘e’:[‘n’:‘parameter name’,‘u’ :‘Parameter unit’,‘v’:‘value’]}

from which topic name can be extracted as : String(value(bn) + value(parameter))

NOTE: Currently substitute IITMandi/COMLab in place of location

By installing MQTT client software mosquitto (we tested with paho mqtt client), connecting to the Broker and subscribing to this topic will give you the live readings of the sensors.

MQTT terminal display

Fig 7: MQTT client displaying data from all sensor nodes



The above readings are of a client which has subscribed to every topic on the broker. This is done using IITMandi/COMlab/# .

How to subscribe to the MQTT broker?

Once you have installed mosquitto client, you can type the following command to subscribe to the broker

$ mosquitto_sub -h <host ip> -t <topic id>/ wildcards+

where valid wild cards are #, +

Remotely Enabling/Disabling sensors

The code can be found here. Using the ‘‘client.subscribe(‘‘settings“)” function this code allows the nodeMCUs receive setting related data under the ‘‘settings" topic.

NOTE: To prevent loss of packets due to sleep modes, sleep mode is not implemented in this code.

Visualization

The aim was to take live sensor readings and plot them graphically as and when the are updated.

Offline plot

A local liveplot was created using matplotlib, which can be used within the IITMandi campus to view live updates, which uses subgraphs and the common attributes from different sensors are plotted on the same subgaph graph. For Example: Temperature readings from sensor node-1 and node-2 are plotted on the same subgraph with different colours to make comparison easier.

Online plot

In the first, whenever a new row of data was written into the .csv file, it was sent to thingspeak channel(a cloud service), where graphs were plotted automatically.

100% 60%

Fig 8: Offline and Online plot

Python codes and instructions for plotting

NOTE: In some of the scripts, you need to uncomment it to upload the data to thingspeak server.

Additional data manipulation scripts

Conclusion and future work

We have build a heterogeneous WSN where communication between sensor to coordinator or coordinator to sensor is done through the MQTT protocol. This framework not only allows us to receive data from sensor nodes communicating through different radio modules, but also can be used to change the setting sensor nodes by issuing a command under the appropriate topic name. In future, we intend to address the following:

References