Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Smart home automation with MQTT

Andrei Cornel – Cristian, Tudor Gabriel, Madalina Arhip-Calin, Alexandru Zamfirescu


Department of Power Engineering Systems
Polyethnic University of Bucharest – Faculty of Power Engineering
Bucharest, Romania

Abstract – This paper aims to investigate the main


II. MQTT STRUCTURE
advantages brought by Message Queuing Telemetry
Transport (MQTT) protocol which is used by Internet of
Things (IoT) devices. In addition to the advantages
What makes MQTT a lightweight protocol is the
brought by the MQTT protocol in the smart grid use of publish – subscribe (pub/sub) architecture. In
environment we will also analyze the challenges and the pub/sub architecture messages are sent (published)
limitations that are introduced by it and were it should by a client to a server who forwards the message to
be adopted in the current smart grid. As a study case for another client. In order to assure that the message is
this paper the practical implementation of MQTT in a correctly forwarded by the server, the client that
smart home automation system will be presented. receives the message is required to be subscribed to
Index terms – IoT, MQTT, smart grid, smart home the topic where the message was published [3].
In short, a message is published by client to a
I. INTRODUCTION certain topic in a server and the message is forwarded
to one or more clients that are subscribed to the same
MQTT is lightweight protocol designed for power topic. The topic is represented by the place were data
constrained and low bandwidth devices used for is sent and retrieved by clients and its structured the
machine-to-machine (M2M) connectivity through IoT same way that folders are structured in an operating
environments that work over TCP/IP. MQTT was system. For reading data, a client needs to subscribe to
developed by Andy Stanford-Clark and Arlen Nipper the exact topic of published message, or it can use the
at IBM in 1999 and used as a proprietary protocol by characters “#, +” to multiple topics at one. These
IBM until 2010 when MQTT version 3.1 was released characters are named wild cards and are used for single
with royalty free license. This event was followed or multilevel topics. In MQTT servers are known as
closely by the standardization and inclusion of MQTT brokers and handles the data transmission between the
in OASIS, Organization for the Advancement of clients. The pub/sub provides one to many data
Structured Information Standards, and its release by distribution and assures that the messages are not
OASIS as version 3.1.1. Shortly after it was accepted mixed by decoupling messages transmitted and
by OASIS, MQTT became also an ISO standard in received by the clients [2,3].
2016, ISO/IEC 20922:2016 [1,2]. In order to assure that the message is sent to the
For IoT devices embedded throughout a smart broker MQTT assures three quality of service (QoS)
grid MQTT offers the ability to send and receive data levels [3]:
in near real time with a small footprint on the • QoS 0 - at most once delivery, the message it
communication network and on the device itself. is just delivered without a notification that it
Being a lightweight protocol, MQTT it is not only was received. Used only in wired connection
useful for power or bandwidth constrained devices, or by severely restricted devices;
but it means that can be also deployed on the current • QoS 1 - at least once delivery, for every
intelligent electronic devices (IED) without affecting message delivered a publish
their functionality. Moreover, because MQTT works acknowledgement notification is sent back the
over TCP/IP protocol, that is used by every system or sender, if the notification it is not received in a
equipment connected to the internet, it means that specified time the message will be sent again
MQTT can be implemented without any with a duplicate tag.
supplementary cost over a system with an internet • QoS 2 - exactly once delivery, it used above
connection. QoS 1 and it ensures that duplicate message is

978-1-7281-3349-2/19/$31.00 ©2019 IEEE


not delivered to the receiving client. Used only TCP/IP is too complicated to work with sensor
for critical applications. networks that are constrained both by bandwidth and
computational power. As an alternative MQTT-SN
can be used over UDP (User Datagram Protocol), that
like TCP (Transmission Control Protocol) is used over
IP (Internet Protocol) but with a much smaller
footprint over the used network and device.
Another difference between MQTT and MQTT-SN is
Fig. 1 MQTT pub/sub architecture [4]
the use of gateway and forwarders in addition to a
MQTT has the option to retain a message on the client and a broker. Gateways are used to interconnect
broker for an unlimited period. This way new clients the two protocols and forwarders are used to re-
that subscribe to that specific topic receive instantly a encapsulate data on the way between a client and a
message with the last known good data published gateway. Forwarders are used due the large number of
without waiting for new massages to be sent from the communication nodes present in WSN to assure that
publisher [2]. the date arrives at destination in the same form that
In order to prevent partial running status in MQTT was generated [5].
is implemented a keep alive system that checks if the In MQTT, unlike traditional client-server
connection between broker and clients it is still active architecture, a client does not need to be aware of
or if one of them is offline. This system is triggered by physical identifiers in order to connect to a server. A
long transmission times or lack of transmission for a big advantage of MQTT is that the message used in it
certain time. A message is request status message is can have any format. This means that the publisher and
sent by the client or broker, which in return responds subscriber need to establish beforehand the data
with an acknowledgement of the request sent. If a format.
response it is not received in an allocated time the
connection with the broker or client in question is III. MQTT USE CASE
terminated and a last will and testament (LWT)
message is sent [2,3]. As presented above MQTT is fast and lightweight
LWT is a message used to notify the reason why communication protocol, so one obvious use case is its
a connection between a broker and client, who was implementation in a smart home automation
actively subscribing or publishing to a topic, was management system. This is an obvious choice
terminated. The notification message is sent to all because most of the devices used in a smart home are
clients subscribed to that topic. LWT is not only optimized for low power consumption and have
generated by the lack of a keep alive response but also limited computational ability. Therefore, MQTT
in case a client ends a connection without a proper pub/sub architecture allows smart devices, or the user
disconnect from the broker. directly, to retrieve from sensors data and sent
A variation of MQTT is used for wireless sensor command in accordance with the received data in real
networks under the name MQTT-SN (Sensor or near real time.
Network) or MQTT-S. MQTT-SN is based on the The next step for a smart home is the use of an
same pub/sub architecture used by the original version automated demand response (ADR), implemented by
with difference that unlike MQTT, MQTT-SN was the power distribution system operator (DSO). A
created for communication protocols used in wireless system like ADR allows the DSO to request an end
sensor networks (WSN) like Zigbee [5]. user to use certain appliances when surplus power is
generated. This can become a real scenario soon due
to the increase use of distributed energy resources
(DER) like photovoltaic and wind power station by
residential users, that will cause imbalance in the
distribution power grid due to the extra power
generated locally. As an example, this system can be
used for switching between fossil-fuel heating system
and an electrical heating system, when surplus power
is present in the distribution power grid [6]. This can
Fig. 2 MQTT-SN architecture [5] be achieved only by allowing communication between
a smart meter and a smart appliance, in this case an power outage the broker will offline while the devices
electrical heater. will try to publish data to it [9].
The MQTT is perfect for this kind of operation
because it is offering a fast, robust and secure way of V. STUDY CASE - MQTT USE IN SMART
communication between the smart meter and the smart HOME AUTOMATION SYSTEM
appliance, and between the smart meter and the DSO.
MQTT provides DSO a reliable way to exchange data MQTT will be used in a simple smart home
with the end user and the DER by using the local automation system as a transmission protocol. The
communication infrastructure [6]. setup used is composed of two raspberry pi 3B+
Furthermore, the DSO can use MQTT for the boards, two wemos d1 mini development boards
integration of smart meter in cloud-based equipped with DHT11 temperature and humidity
infrastructure. By using cloud and cloud computing sensor and one wemos d1 mini equipped with a one
environments together with IoT technologies like channel relay switch.
MQTT advanced services like state estimation, The raspberry pi and wemos d1 mini are both low
demand response and user awareness can be enabled cost development boards that can be used in small
for smart grids. For function like state estimation to automation project with the big difference that
work a constant flux of data needs to be retrieved from raspberry pi is much closer to computer in terms of raw
field devices as fast as possible. MQTT pub/sub model power and can use as operating systems Linux
allows data to be gathered in real or near-real time distributions or even stripped-down versions of
from IED like smart meters and enables fast windows 10 as operating systems (OS). Wemos d1
bidirectional communication which is needed to mini is a board based on the ESP8266 wireless
send/retrieve data from/to field devices [7]. microcontroller, it does not support an operating
Results obtained from a state estimation system system nor those have the required raw power to do so.
can be used as feed in data for a network In order to use a wemos it is necessary to program it in
reconfiguration system. A network reconfiguration an environment like Arduino IDE. Wemos can be used
system can be used to balance power generation and as a standalone device if it is programed as a webserver
consumption in part of distribution power grid by to display data various sensors like DHT11 or to
reconfiguring portions/branches of distribution grid control a relay via a webpage. But the most likely
using the switch devices installed throughout the grid scenario is use of wemos boards together with a
[7]. raspberry pi or something similar, where the raspberry
Network reconfiguration can be employed with will be the master control device.
fault detection, isolation and restauration (FDIR) In the center of this configuration will be one of
system. Using the two system together the outage the raspberry pi board which will act as the MQTT
duration in a power distribution system can be reduced broker. The other devices will act as clients for MQTT,
considerably [8]. which will either publish or subscribe messages to a
As in the examples above MQTT in smart grid topic in the broker or will do both. For example the
environment is used only to send/retrieve data to/from wemos board with the DHT11 sensor will only publish
different devices and systems due to its fast-two-way data, the measured humidity and temperature, while
communication architecture. the wemos board with the one channel relay switch
will publish data, the open or closed relay state, and
IV. MQTT LIMITATION AND CHALLENGES will also subscribe to another client, that will issue
control commands for the relay.
As perfect as it sound MQTT protocol has some First, we need to install and configure the broker.
limitation and disadvantages. One of the main For this purpose, we used Mosquitto as MQTT broker.
disadvantages is MQTT dependency to the broker. In Mosquito is open source lightweight messaging
MQTT the broker affects the scalability of the overall protocol for MQTT, suitable for large range of
system, meaning that the network can expand as much devices, varying from power constrained devices up to
as the broker can support it. Also, the broker can be a state-of-the-art servers. After the Mosquitto
point of failure in an automation system because the installation on the raspberry pi we need to setup a
broker usually is plugged directly in a wall socket username and a password, used by the clients that will
while the devices that sent/retrieve data from it are subscribe or publish to the broker to authenticate.
most of the time battery operated. So, in case of a
Next the MQTT clients will be configured with present programs like this for android or iOS
broker address, network connection method and the devices, we used myMQTT for android.
password and username previously set for Unlike other programs MQTT Explorer connect
authentication. The devices configured as MQTT to the broke and display all the topics used for
client will publish messages with data to the broker publishing or subscribing. This can be a useful
and will subscribe for control commands. So, the two function in the case where you have many devices and
you don’t remember all the used topics. Also, MQTT
wemos boards equipped with the DHT11 sensor will
Explorer can keep a history with all the messages sent
be used to measure temperature and humidity from
and it will show you when value is updated.
two rooms, bedroom and living room, and will publish
data under the topic” d1/home/rooms/living
room/temperature” for temperature and“d1/ home/
rooms/ living room/ humidity” for humidity. The same
structure is used for bedroom measurements topic, we
only need to replace living room with bedroom.
For reading data from multiple topics
simultaneously, we will need to use wild cards. For
example, if we want to read the temperature from the
bedroom and the living room at the same time we will
subscribe under the topic ”d1/ home/ rooms/ +/
temperature” or if we want to read both measurements
for the two rooms together we will use the following
topic path “d1/ home/ rooms/#”. For the relay switch
wemos will publish first to the broker the open or close
Fig. 6 – MQTT Explore program
status by sending 0 or 1 values using the topic ”
confirm/relay/switchConfirm1” and will receive
control commands by subscribing to the topic
”comand/relay/switch1”. The relay is configured
through wemos to be initially opened, so it will publish
0 and will receive 1 for closing the relay switch.
In order to check if everything is working
properly, we will need to create a subscribing client to
the created topics. This can be done in several ways as
follow:
• Using directly the comand line terminal in the
raspberry pi OS and writing mosquitto_sub -
d -u ‘username’ -P ‘password’ -t ‘topic’ for
subscribing to a topic and for publishing to a
topic mosquitto_pub -d -u ‘username’ -P
‘password’ -t ‘topic’ -m "Message", where -
d if for debug mode, so all messages
published or subscribed are outputted on the
screen. Another way to do this is by enabling
SSH on the raspberry and connecting to it via
SSH, this will open directly the comand line
terminal in raspberry pi OS and typing one of
the commands;
• Using a windows or MacOS program like
MQTT.fx or MQTT Explorer. MQTT.fx
needs to be configured with the broker
address and login credentials for publishing
or subscribing to topics. Also, there are Fig. 7 – myMQTT android phone scree capture
Fig. 8 – openHAB control panel

All this data retrieved from the wemos devices automate lighting control in house based on the
needs to centralized. It can be used with programs like ambient light. Furthermore, implementing a cloud
MQTT Explorer, but it is not a practical way to do this. solution will allow you to access data from
In this scope on the second raspberry pi board will be everywhere, not only your personal network
installed openHAB, short for open automation bus. A system like this will evolve with the further
OpenHAB is opensource home automation platform development of technologies like ESP8266, openHAB
with the ability to integrate devices from different
platform and MQTT. For openHAB there is already a
developers and vendors like LG, Philips or Samsung
version 3 in development.
and it also support the MQTT protocol, although this
was made available with version 2 of openHAB [10].
OpenHAB work by linking things that are represented VII. REFERENCES
by physical objects with items that represents the
[1] MQTT protocol [Online]: http://mqtt.org/faq
functionality of the thing. In our case the thing is
[2] Perry Lea, "Internet of Things for Architects", Packt
represented by the wemos devices that are linked to Publishing, 2018
items represented by the temperature or humidity [3] MQTT v3.1.1 Oasis standard [Online]:
readings or the relay status. http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-
In our case openHAB is used to display v3.1.1.html
temperature and humidity, in Celsius degrees and [4] MQTT pub/sub architecture [Online]:
percentages, from the two rooms, bedroom and living https://1sheeld.com/mqtt-protocol/
[5] Andy Stanford-Clark and Hong Linh Truong,
room and to control a power switch from the living "MQTT For Sensor Networks (MQTT-SN) Protocol
room with a toggle button, represented in fig. 8. Specification", IBM, November 14, 2013
[6] John C. Hastings, David M. Laverty, "Modernizing
VI. VI. CONCLUSION AND FUTURE WORK wide-area grid communications for distributed
energy resource applications using MQTT publish-
subscribe protocol", IEEE Power & Energy Society
In our setup we presented a simple MQTT General Meeting, 16-20 July 2017
implementation example in a smart home environment [7] Marco Pau et al., "A cloud-based smart metering
using ESP8266 based development boards, wemos d1 infrastructure for distribution grid services and
mini, and openHAB platform. The ESP8266 offers the automation", Sustainable Energy, Grids and
Networks, Volume 15, September 2018, Pages 14-
ability to control devices like sensors or relay shields 25
via wireless internet network. This means that devices [8] Abouzar Estebsari, Edoardo Patti, Luca Barbierato,
can be used anywhere on our premises as long they are "Fault Detection, Isolation and Restoration Test
in the Wi-Fi router coverage area. The configuration Platform Based on Smart Grid Architecture Model
Using Internet-of-Things Approaches", 18th IEEE
used can be extended by adding sensor for AC voltage International Conference on Environment and
and current measurements for all the power sockets Electrical Engineering (EEEIC), June 2018
where the daily used appliances are plugged in. In [9] Andrew Minteer, "Analytics for the Internet of
addition, there can be added automation controls for Things (IoT)", Packt Publishing, 2017
[10] OpenHAB documentation [Online]:
powering on the air conditioning system when the https://www.openhab.org/docs/
temperature exceeds a certain setpoint value or to

You might also like