A Web-Based IoT Solution For Monitoring Data Using MQTT Protocol
A Web-Based IoT Solution For Monitoring Data Using MQTT Protocol
Abstract —In this paper, a web-based Internet of things mentioned area different improvements are achieved using
solution aimed for monitoring, tracking and analyzing data in enhanced tools for real-time monitoring and tracking, decision-
agriculture area is proposed. The purpose of the presented making support, automatization of processes, analyzing data
solution is to facilitate monitoring of different manufacturing and reporting.
process in mentioned area using IoT technologies. A real time
data is achieved using broker-based publishing/subscribing
Message Queue Telemetry Transport protocol which is briefly II. BACKGROUND
described. Using this protocol limitations resulting from To understand the need for this type of IoT implementation,
constrained networks in rural areas are avoided. Collected data a short brief of the drying process is explained. Drying process,
from sensors is shown and stored in web part of the information as the one of the most important postharvest treatment, ensures
system. An architecture of the web application is described as a storage durability, reduces the spoilage and increases the shelf
client-server three-tier architecture in which the graphical user life of agricultural products. To ensure the quality of the final
interface (presentation layer), application functions and logic product, the temperature and moisture factors must be
(application layer) and computer data storage (database layer) monitored during the drying process. If the value of
are developed and maintained as independent modules, on
temperature and moisture offset is greater than tolerance
separate platforms. This type of implementation is developed by
the manufacturers need for monitoring and tracking data. This
setpoint, manufacturers must take additional actions. Although
architecture is based on future concrete implementation, at least these actions are semi-automated using gas or oil burning
in segments, in several agricultural facilities in Virovitica- stoves, most manufacturers are using wood as fuel. Cost-
Podravina County. effectiveness analysis shows that price of drying process using
wood as fuel is 70% lower than using oil or gas. Today, almost
Keywords—Internet of things; monitoring; MQTT; Raspberry Pi; 80% of small manufacturers are using wood as fuel. One of the
sensors; three-tier architecture; web application main drawbacks of using wood burning stoves is an obligation
for temperature surveillance 24 hours a day, seven days a
I. INTRODUCTION week. Temperature and moisture sensors are already installed
in facilities and the values are shown on the control board
Internet of things (IoT) is no longer future tech trend installed at the front side of the facility. To successfully finish
because of an everyday growing number of objects connected the process, manufacturers must manually check values within
to the Internet worldwide. IoT can be defined as a network of a period of the time which is most often a problem, especially
objects which are equipped with sensors and network access during the night. This paper proposes distance monitoring
modules [1]. IoT object can be any device, vehicle or building system for temperature/moisture surveillance over the web-
from everyday life. With appropriate electronics and software, based graphical user interface. Already installed sensors are
these objects are able to collect and share data. According to connected to devices (e.g. Arduino Uno, Raspberry Pi) whose
[2] Gartner says that 6.4 Billion connected objects will be in main task is to send data (i.e. temperature and moisture values)
use in 2016., 30% more than in 2015. That number will grow to a central server over publish/subscribe messaging protocol
up to 20 billion by 2020. IoT represents the next evolution of MQTT. Central server has MQTT broker role, and his main
the Internet and extends its ability to gather, analyze, and task is to multicast data to subscribed devices. Also, during the
distribute data which can be turned into information and real-time monitoring, collected data is sent to web server and
knowledge. Furthermore, IoT has made Internet sensory SQL server where the data is stored for the purpose of the
(temperature, pressure, vibration, light, moisture, stress) future analysis and reporting.
thereby achieving improved monitoring, analyzing and
tracking systems [3]. IoT is present in almost every human
area: society, tourism, education, supply chain management, III. MESSAGE QUEUING TELEMETRY TRANSPORT
military, healthcare, industry and many others. This paper is a The main characteristic of IoT devices is the machine-2-
proposal for IoT implementation in the agriculture area, machine communication which is in this paper presented as a
focused on drying of agricultural products, especially tobacco, communication between multiple sensor devices and a single
chamomile, wheat, fruits or even lumber. Using IoT in data collection device. These sensor devices are most often
249
978-1-5090-3720-9/16/$31.00 ©2016 IEEE
installed in isolated area where connection to the Internet is to distinguish data sent by the publisher, a topic string is used.
limited. For example, most of the facilities are built in rural For example, a client can publish temperature and humidity
areas where connection to the Internet is realized over slow values using different string for each value (e.g. temp/25 or
DSL connection or cellular network. In such environment, hum/40). On the other side, if a MQTT client wants receive the
unreliability which reflects high packet loss rate is one of the data, he must subscribe to the specific topic. To make a MQTT
main disadvantages [4]. There are several protocols proposed client from a device, an MQTT library must be installed and
for M2M/IoT communication with a focus on mentioned connection to an MQTT broker must be established over any
constrained environments. Most frequently adopted protocols kind of network. For example, MQTT client can be a small
are MQTT (Message Queue Telemetry Transport) and CoAP computer (Arduino or Raspberry Pi) connected to a wireless
(Constrained Application Protocol). IoT applications can be network with a library strapped to the minimum or a typical
simplified in the way of the error handling which can be done computer running a graphical program. The client
using these protocols [4]. implementation of the MQTT protocol is simplified and very
straightforward [5].
MQTT is a machine-to-machine (M2M)/Internet of Things
connectivity protocol for use on top of the TCP/IP protocol Temperature Web
stack which was designed as an extremely lightweight broker- sensor application
availability, and negotiated delivery guarantees [9]. In the hub MQTT Client
message Subscription on topic
temperature
message
MQTT Client
and spoke model of Message Oriented Middleware messaging (publisher) (subsriber)
needs to be send. On the other side, every device who had Fig. 1. Message queuing telemetry transport protocol model
previously registered its interests with a server will keep
receiving messages until subscription is canceled (Fig. 1). Client libraries can simplify the process of writing MQTT
Using this architecture, publishers and subscribers do not need client applications. MQTT libraries are available for different
to know for each other which is one of the major advantages of types of programming languages and platforms, for example,
this protocol. For example, temperature sensor devices need JavaScript, PHP, C, C++, Android, iOS etc. For the initial
not to know who are the clients that are subscribed for implementation, Python library will be used.
receiving data and conversely [13]. So, the publishers and
subscribers do not need to participate in the communication at B. MQTT Broker
the same time and do not need to be familiar with each other An MQTT broker is a central device in mentioned hub and
[16]. It is intended for devices with limited power and memory spoke model. The main responsibilities of an MQTT broker are
capabilities, where the network is expensive, has low handling communication between MQTT clients and
bandwidth or is unreliable. One of the key requirements of an distributing messages between them [4]. A broker can handle
Internet of Things concept is low network bandwidth used to up to thousands connected MQTT client at the same time.
send data and minimal device resource requirements. While When the message is received, broker must find all the clients
attempting to ensure reliability and delivery, MQTT has met that have a subscription to the received topic [5]. In presented
these requirements [12]. This protocol has been applied in a application, it is responsible for receiving messages from the
variety of embedded systems. For example, hospitals use this sensors connected to the device with implemented MQTT
protocol to communicate with pacemakers, oil and gas client library and forwarding it to the designated mobile device.
companies use it to monitor oil pipeline thousands of miles There are plenty other tasks and responsibilities that are
away. Facebook uses this protocol for messaging applications handled by the broker. First of all, there is authentication and
[17]. Fig. 1 shows proposed model of MQTT protocol which authorization of the clients for the security purposes. A client
runs on TCP/IP connection. can send username and password within connect message that
will be checked by the broker. On the broker side topic
A. MQTT Client permission is implemented in order to restrict client to publish
Any IoT object can be MQTT client that sends or receives or subscribe. Furthermore, for encrypted communication
telemetry data. This could be any device from a between the broker and the clients, TLS (Transport Layer
microcontroller up to a server. The type of the MQTT client Security) and SSL (Secure Sockets Layer) encryption is used,
(subscriber or publisher) depends on its role in the system. It the same security protocol that can be used by the HTTP
can produce or collect telemetry data. In both cases, MQTT protocol [14]. It is also possible to implement custom
client should first connect to a messaging server using a authentication or authorization logic into system [5]. There are
particular type of message explained in the following chapter. several message brokers that implement MQTT protocol like
After the connection is successfully established, a client must Mosquito - open source MQTT v3.1/v3.1.1. broker [18] and
declare himself whether he is a subscriber or publisher. In order HiveMQ – an enterprise MQTT broker [5].
250
C. MQTT over WebSockets As it is shown in Fig 1., the publisher must send a
Using MQTT over WebSockets every browser can be PUBLISH packet with QoS = 0 and receiver accepts ownership
MQTT device. Due the publish/subscribe pattern of MQTT a of the message when it receives the PUBLISH packet.
real-time communication between end device (e.g. temperature When using QoS level 1, it is guaranteed that a message
sensor) and monitor device (e.g. a web application) is achieved. will be delivered at least once to the receiver. The publisher
Using QoS 1/2 message will arrive on the client or broker at sends the message and checks the delivery status using
least once/exactly once. The broker will queue all messages PUBACK message. If PUBACK message is lost, the broker
which client misses when it’s not connected. Messages which will send the same message again, until PUBACK message is
are retained on the server are delivered when a client received.
subscribes to one of the topics instantly. Paho.js [19]
implements this behavior and will be used in initial testing. PUBLISHER BROKER SUBSCRIBER
PUBLISH
TABLE I. MQTT MESSAGE
Bit 7 6 5 4 3 2 1 0
PUBACK
Byte 1 Message type DUP QoS RETAIN
DELETE
Byte 2 Remaining length MESSAGE
Byte 1 contains the Message type which is represented as a Fig. 3. At least once delivery (MQTT QoS =1)
4-bit unsigned value. There is 14 message type specified in 3.1
version of MQTT protocol. CONNECT (client request to In QoS Level 2, also called “assured delivery” and shown
connect to server), PUBLISH (publish message), PUBACK in Fig 3., the messages are delivered exactly once using a 4-
(publish acknowledgment), SUBSCRIBE (client subscribe way handshake. Using QoS level 2 it is possible to have
request) are some of the message types. A detailed description relatively longer end-to-end delay, but there is no messages
of the message types can be found in protocol specifications loss.
[10] and [11]. DUP flag is set when the client the client or
server attempts to re-deliver a PUBLISH, PUBREL, PUBLISHER BROKER
SUBSCRIBE or UNSUBSCRIBE message. This applies to
messages where the value of QoS is greater than zero (0), and STORE
an acknowledgment is required. QoS flag indicates the level of MESSAGE
assurance for delivery of a PUBLISH message. The QoS levels PUBLISH [QoS = 2]
will restore last retained message with corresponding QoS for PUBLISH
specific topic. Each client that subscribes to this topic pattern PUBREC
will receive the retained message immediately. The broker will
save only one retained message per topic [5]. Remaining length PUBREL
251
On the presentation layer, functionality of HTML-based Ajax request sent by the client is received from the application
graphical user interface has been extended with custom layer via PHP scripts. There are three types of PHP scripts:
JavaScript/jQuery functions and several bootstrap friendly 1) PHP script that handles user’s actions and receives
jQuery plugins. Using AJAX for communication with Apache data from MQTT broker – used for inserting, updating
web server, features of a single page application are achieved. and deleting data from data server
When a user action generates a client call to retrieve new data, 2) A PHP script that prepares data – after a request for
the server only returns view fragments. This means that is data is received, this type of script is used for
possible to update parts of the application, without reloading connecting and retrieving data from MySQL server
entire viewport. At the side of design, the bootstrap responsive which is then encoded in JSON format and sent back
framework is used. Data received on the MQTT Broker from to client
publishers is automatically forwarded to MQTT client devices 3) configuration PHP – used for manipulating with
where the gauges and diagrams are updated with fresh data session storage, database connection, and other
(Fig.5.). A central monitor device, also MQTT client, has a application configuration variables.
task to send data to the Web server in application layer where
data is validated and prepared for inserting into SQL tables. On the database layer, there are several MySQL database
servers where data is stored and retrieved. Data in this tier is
There is one central monitor device in system. Saved data can
be used to create different types of reports and analyze, which kept independent of application servers. There is the database
can be shown on other end-user devices in presentation layer. used for configuration storage, data storage and domain with
There are two types of ajax communication (both include user storage.
AJAX request and response) between presentation layer and
application layer: saving data (central monitor device) and V. IMPLEMENTATION DETAILS
reading data (all devices in presentation layer). So, data A prototype of proposed system is implemented in a
preview on graphical user interface of devices in presentation tobacco drying kiln type SD – 78/2 (7270mm x 3150mm x
layer can be directly from MQTT broker, or requested from 2850mm) with two chambers (Fig. 6) and integrated wood
server with several filters applied (date, time, facility id, burning stove TD – 80 (Fig. 7).
process id).
MySQL
server
Database layer
result
SQL
SQL
PHP
PHP json
PHP config PHP action
creator On the front side of the facility, the control board is
attached to the kiln. On the control board, there are several
switches used for managing and monitoring drying process. As
mentioned, for initial test RaspberryPi model 3 is used. Main
XMLHttpResponse
XMLHttpRequest
AJAX response
AJAX request
JSON
JavaScript,
Jquery,
CSS and humidity values are shown on the board.
CENTRAL MONITOR
DEVICE
MQTT
Broker
252
protocol limitations resulting from constrained networks in
rural areas are avoided. In proposed model the client producing
data (publisher) sends data to the server which forwards data to
end user devices. Also, the architecture of web-based
application aimed at showing results collected from sensors is
presented. Architecture is described as client-server three-tier
architecture with three separate layers – presentation,
application and database layer.
REFERENCES
253