Smart Home Automation With Iot Dipankar Saha
Smart Home Automation With Iot Dipankar Saha
Smart Home
Automation with IoT
The ultimate DIY guide for making
smart homes using open-source softwares
Dipankar Saha
www.bpbonline.com
ii
First Edition 2024
ISBN: 978-93-55519-191
All Rights Reserved. No part of this publication may be reproduced, distributed or transmitted in
any form or by any means or stored in a database or retrieval system, without the prior written
permission of the publisher with the exception to the program listings which may be entered,
stored and executed in a computer system, but they can not be reproduced by the means of
publication, photocopy, recording, or by any electronic and mechanical means.
All trademarks referred to in the book are acknowledged as properties of their respective
owners but BPB Publications cannot guarantee the accuracy of this information.
www.bpbonline.com
iii
Dedicated to
All IoT enthusists, professionals and
students who want to learn Smart Home Automation DIY
iv
Foreword
The modern home is rapidly transforming, evolving from a mere living space into an
intelligent environment where everyday tasks are seamlessly integrated and automated.
This transformation is driven by the advent of smart technology, which has made home
automation more accessible, efficient, and affordable. At the heart of this revolution is
IoT enabled by sensors, actuators, microcontrollers and the Raspberry Pi, a versatile and
powerful minicomputer that has opened up a world of possibilities for tech enthusiasts,
hobbyists, and professionals alike.
"Smart Home Automation with IoT" is a comprehensive guide designed to equip you with
the knowledge and skills needed to turn your home into a smart home. Whether you are a
beginner with no prior experience in electronics or programming, or an experienced tech
enthusiast looking to expand your skill set. This book provides step-by-step instructions
and practical examples to help you harness the full potential of the Raspberry Pi in creating
a smart home environment.
The journey begins with an introduction to the fundamental concepts of home automation
and the unique capabilities of the Raspberry Pi. From there, the book delves into the
basics of setting up your Raspberry Pi, including installation, configuration, and basic
programming. Each chapter builds upon the previous one, gradually introducing more
complex projects and integrations.
You will learn how to automate electrical and electronic appliances, and even create a
voice-controlled assistant. The book also covers advanced topics such as integrating
different smart devices using popular protocols like MQTT, leveraging cloud services for
remote control, and ensuring the security and privacy of your smart home network.
One of the most exciting aspects of home automation is the ability to customize and
expand your setup according to your specific needs and preferences. Throughout the book,
emphasis is given on the importance of creativity and experimentation. By the end, you
will not only have a fully functioning smart home but also the confidence and knowledge
to continue innovating and expanding your automation projects.
The field of home automation is constantly evolving, with new technologies and
innovations emerging at a rapid pace. As such, this book is designed to be a living
document, encouraging you to stay curious, keep learning, and continuously explore new
v
possibilities. The skills and concepts you acquire here will serve as a solid foundation for
future advancements and projects.
I hope this book inspires you to embrace the potential of smart home automation with IoT,
transforming your home into a dynamic, intelligent space that enhances your quality of
life. Welcome to the exciting world of smart home automation!
- Dr. P. R. Wankhede
Ph.D, SMIEEE, LMISTE
Associate Professor, Electronics Engg. (VLSI Design and Technology)
CSMSS Chh. Shahu College of Engineering,
Chhatrapati Sambhajinagar, India
vi
Dipankar Saha is an IT Architect for Industry 4.0 & IoT solutions having 20+ years of
experience and has worked in several implementation projects and in various product
design and developments in this domain.
He is a smart home and IoT enthusiast who has implemented smart home solutions using
open-source technologies using Raspberry Pi, NodeMCU, ESP32, relay modules and
various sensors.
He is an SAP certified Associate Enterprise Architect and has co-authored several popular
books on Industry 4.0 solutions. Currently he works as Principal Architect for Industry 4.0
solution at Incture and is based out of Kolkata, India.
vii
Acknowledgement
I want to express my deepest gratitude to my family for their unwavering support and
encouragement throughout this book's writing.
I am also grateful to the team at BPB Publications for their guidance and expertise in
bringing this book to fruition. It was a long journey of revising this book, with valuable
participation and collaboration of reviewers, technical experts, and editors.
I would also like to acknowledge the valuable contributions of my colleagues and friends
during many years working in the tech industry, who have taught me so much and
provided valuable feedback on my work, especially Pradip Ray, Abesh Bhattacharjee,
Soumya Das, Chandan Jash and Anup Ketkale.
I want to acknowledge and thank Arnab Basak, an young but very promising electronics
engineer, who has helped me making the content of the book perfect by drafting the circuit
diagrams and code used in this book.
I want to thank and acknowledge esteemed professors from various engineering colleges
who have reviewed the book and provided guidance especially Prof. Pravin Wankhede
from CSMSS Chh. Shahu College of Engineering, Aurangabad and Mr. Edgardo Peregrino
an expert in IoT and embedded electronics, who have done the technical review of the
book and helped immensely to make it perfect.
Finally, I would like to thank all the readers who have taken an interest in my book and for
their support in making it a reality. Your encouragement has been invaluable!
ix
Preface
As a child, I was always fascinated by the world of electronics and how we can use it in
our daily lives. In this era of rapid technological advancement, the intersection of Internet
of Things (IoT) and information technology have ushered a new paradigm as smart home
that transforms the way we live, interact with our surroundings, and experience the
concept of "home". The vision of a connected, intelligent home is no longer a futuristic
dream but a present reality, with IoT serving as the catalyst for this transformative journey.
As a professional working in this domain for several years as well as an enthusiast for IoT
and home automation, I felt the need to a have a guide for the ones who want to learn this
subject and want to implement the same, at very low cost using open source-softwares. As
a result, it prompted me to write this book, which I believe will help many who want to
learn and implement these scenarios, even though they may not have any deep and prior
knowledge on this topic.
This book delves into the concepts, design and the knowhow of smart home automation,
offering you a comprehensive guide to understanding, implementing, and optimizing IoT
solutions to make your home smarter, more efficient, and seamlessly integrated into the
digital age.
Whether you are a curious homeowner venturing into the realm of IoT for the first time or
a seasoned developer looking to expand your expertise or a student who wants to learn IoT
and do hands-on, this book is crafted to cater to a diverse audience. It serves as a roadmap,
navigating through the intricacies of IoT technologies, protocols, and platforms, while
providing practical insights into designing and building your smart home ecosystem.
• Device connectivity: Learn about the various sensors, actuators, and devices
that form the backbone of a connected home, and understand how to seamlessly
integrate them into your IoT ecosystem and setup and use Raspberry Pi as the
home automation server.
x
• Smart Home Platform and software: Explore open-source IoT platform such
as OpenHAB that serve as the central nervous system of your smart home,
facilitating communication and control across devices as well as other open source
technologies to build your smart home applications.
• Practical implementation: Walk through hands-on projects with the circuits and
code that demonstrate real-world applications of smart home automation.
Chapter 1: Introduction to IoT and Home Automation - This chapter explains what
is Internet of Things and how it affects our daily lives with various scenarios and its
importance. Also it explains the different scenarios of home automation and IoT with
examples and how to use free and open source technologies to implement smart home by
DIY.
Chapter 3: Using NodeMCU and ESP32 with Relays and Actuators as Control
Switch - This chapter explains how NodeMCU and ESP32 is used to connect to relay
modules and DC motors and write the program in it to control the power supply through
the relay modules and control DC motors.
Chapter 4: Connecting Various Common Sensors using Arduino - This chapter explains
how to connect to various common sensors such as Light sensor, DHT sensor, Ultrasonic
sensor, sound sensor, etc, using Arduino and write the programs to get and pass data
from those sensors to Arduino. It also explains how Arduino sends the data to ESP32
or NodeMCU using serial port to send it to the OpenHAB IoT platform through MQTT
broker.
Chapter 5: Connect Sensors and Relays with OpenHAB IoT and Voice Chatbots - This
chapter explains how to configure OpenHAB IoT platform installed on Raspberry Pi and
connect with NodeMCU and ESP32 through MQTT messaging by connecting to Mosquitto
broker to send and receive the data and commands to control the appliances and how to
store the data from OpenHAB IoT to InfluxDB. Also explains how OpenHAB IoT platform
is integrated with Amazon Alexa or Google Home for voice command enablement as well
as how to configure and use the mobile app for OpenHAB.
xi
Chapter 6: Developing Dashboards using Grafana to Monitor Smart Home and IoT
Devices - This chapter explains how to use Grafana to easily create monitoring and analytics
dashboards for the home automation IoT scenarios querying the data from InfluxDB.
Chapter 7: Get more out of Raspberry Pi - This chapter explains how to enable and use
advanced features of Raspberry Pi including connecting with LED and DC motors using
GPIO pins, using camera module in Raspberry Pi and develop AI-based Python program
for hand, face and gesture detection to control the appliances by connecting to OpenHAB
through MQTT messaging and use Docker container for application deployment.
As you embark on this journey through the pages of "Smart Home Automation with IoT"
envision the endless possibilities that could arise when technology meets your imagination.
May this book empower you to design, build, and enjoy the connected and smart home
of your dreams.
Welcome to a future where your home is not just a living space but an intelligent, adaptive
companion, attuned to your needs and preferences.
https://rebrand.ly/6mgl9gb
The code bundle for the book is also hosted on GitHub at
https://github.com/bpbpublications/Smart-Home-Automation-with-IoT.
In case there’s an update to the code, it will be updated on the existing GitHub repository.
We have code bundles from our rich catalogue of books and videos available at
https://github.com/bpbpublications. Check them out!
Errata
We take immense pride in our work at BPB Publications and follow best practices to
ensure the accuracy of our content to provide with an indulging reading experience to our
subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve
upon human errors, if any, that may have occurred during the publishing processes
involved. To let us maintain the quality and help us reach out to any readers who might be
having difficulties due to any unforeseen errors, please write to us at :
errata@bpbonline.com
Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’
Family.
Did you know that BPB offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.bpbonline.
com and as a print book customer, you are entitled to a discount on the eBook copy.
Get in touch with us at :
business@bpbonline.com for more details.
Piracy
If you come across any illegal copies of our works in any form on the internet,
we would be grateful if you would provide us with the location address or
website name. Please contact us at business@bpbonline.com with a link to
the material.
Reviews
Please leave a review. Once you have read and used this book, why not leave
a review on the site that you purchased it from? Potential readers can then see
and use your unbiased opinion to make purchase decisions. We at BPB can
understand what you think about our products, and our authors can see your
feedback on their book. Thank you!
Table of Contents
3. Using NodeMCU and ESP32 with Relays and Actuators as Control Switch............ 59
Introduction...................................................................................................................... 59
Structure............................................................................................................................ 59
Objectives......................................................................................................................... 60
Overview of Micro-Controllers..................................................................................... 60
GPIO pinout of ESP boards........................................................................................ 61
Programming ESP boards with Arduino IDE............................................................ 63
Write your first program...................................................................................... 66
Connecting microcontrollers with relays for switch control..................................... 68
Understanding relay module....................................................................................... 71
Circuit diagrams for relay control with ESP boards................................................... 72
Programming ESP8266 (NodeMCU) to control 4-channel relay module.................. 76
Programming ESP32 to control 8-channel relay........................................................ 83
Connecting ESP8266 with Motor Driver to Control DC Motor................................ 84
Circuit diagram for connecting L293D motor driver with ESP8266 for motor control....85
Programming ESP8266 for motor control.................................................................. 86
Sending and receiving MQTT messages to control relays and motors................... 87
Conclusion........................................................................................................................ 88
5. Connect Sensors and Relays with OpenHAB IoT and Voice Chatbots.................... 109
Introduction.................................................................................................................... 109
Structure.......................................................................................................................... 109
Objectives....................................................................................................................... 110
Configuring OpenHAB IoT Platform......................................................................... 110
Connecting to OpenHAB Cloud............................................................................... 112
Connecting to Mosquitto MQTT Broker.................................................................. 114
Configuring InfluxDB Persistence........................................................................... 117
Configuring Transformation rules............................................................................ 120
Creating Things and items to Connect with Sensors and Relays........................... 121
Creating channel for Thing....................................................................................... 122
Creating item for the Thing and Channel................................................................. 130
Defining rules for Device Control............................................................................... 134
Creating models to group items.................................................................................. 138
Creating pages to view Thing data............................................................................. 140
Setting up Mobile app for OpenHAB......................................................................... 146
Use Voice Chatbot with OpenHAB............................................................................. 148
Configuring OpenHAB Voice Bot............................................................................ 148
Configuring Amazon Alexa for OpenHAB.............................................................. 149
Configuring Google Home for OpenHAB ................................................................ 154
APIs of OpenHAB......................................................................................................... 156
Conclusion...................................................................................................................... 157
xvii
6. Developing Dashboards using Grafana to Monitor Smart Home and IoT Devices....159
Introduction.................................................................................................................... 159
Structure.......................................................................................................................... 159
Objectives....................................................................................................................... 159
Using Grafana for dashboards.................................................................................... 160
Creating Datasource for InfluxDB.............................................................................. 160
Creating dashboards for monitoring of sensors and devices in Smart Home..... 163
Creating alerts................................................................................................................ 170
Creating playlists for dashboards............................................................................... 176
Conclusion...................................................................................................................... 178
Index...............................................................................................................................197-200
xviii
Introduction to IoT and Home Automation 1
Chapter 1
Introduction to
IoT and
Home Automation
Introduction
This chapter introduces you to Internet of Things (IoT) and home automation and explains
what Internet of Things is and how it affects our daily lives with various scenarios and
its importance. It also explains the different scenarios of home automation and IoT with
examples and how to use free and open-source technologies to implement smart home by
Do it Yourself (DIY).
Structure
In this chapter we will discuss the following topics:
• What is IoT
• How IoT affects our daily lives
• Make your home smarter: Scenarios and use cases
• Using open source and free technologies for home automation
2 Smart Home Automation with IoT
Objectives
After studying this unit, you should be able to understand what Internet of Things is
and how it can be used for smart home automation scenarios with examples. You will also
learn which open-source technologies can be used to enable smart home automation.
What is IoT
We are all familiar with internet and aware of how it affects our daily lives. The internet
that we know and primarily use today is a network of computers which originated from
World Wide Web. In the traditional internet, computers interact with each other in native
communication protocols and typically driven by human users. The consumer and
producer of the content in traditional internet are typically human users that is, us. We
either use apps or access websites to view the content available in internet or create the
same. But over the years objects around us have become smarter and some of them can
actually transmit and consume data and behave or operate based on that. The data are
typically collected through sensors which can sense and measure its surrounding physical
conditions such as temperature, humidity, vibration, luminary brightness, etc., and
actuators which can control certain things such as toggle a switch or move an object, based
on input voltage or commands sent digitally. The sensors and actuators are connected
to the devices and objects such as the machines, home appliances or even human bodies
to exchange the data and can transmit or receive the data by connecting to the internet,
local WiFi or using local connectivity protocols such as Bluetooth, Zigbee, LORAWAN,
etc. This means that a physical object can be smart enough to exchange data with other
objects or applications and can behave and operate taking instructions from external
sources. Physical objects, machines, appliances connected with sensors and actuators
when connected through the network with each other can exchange data intelligently.
That is what we call as Internet of Things.
Like the traditional internet, which is a network of computers, Internet of Things is the
network of physical objects or things. Nevertheless, like the traditional internet, humans
can still interact with Internet of Things, to consume the data generated by the things or
to send commands to control the things.
As you may know the internet fueled by World Wide Web (www) started around mid-
1990s, though technology and concept of internet had been around since the 1960s with
the research project Advanced Research Projects Agency Network (ARPANET). Though
the widespread usage of IoT has started in recent years, it may be surprising to note that a
Coca Cola vending machine was connected to the network in 1982 by a group of students
in Carnegie Mellon University in USA to check its inventory level using microswitches,
which may be considered as one of the first IoT devices.
The term Internet of Things was coined by Kevin Ashton, a British technology pioneer
in 1999 while working with Proctor and Gamble (P&G) in the context of using Radio-
Introduction to IoT and Home Automation 3
Note: According to Fortune Business Insights, the global market size of IoT is valued
at USD 544.38 billion in 2022 which is forecasted to grow from USD 662.21 billion in
2023 to USD 3352.97 billion by 2030 with a CAGR of 26.1% during this period.
The usage of IoT is increasing rapidly in various fields as it opens new potential for
individuals and businesses by integrating and collecting data and controlling things
remotely. According to Statista the projected IoT devices connections globally is estimated
to reach 83 billion by 2024 from 35 billion in 2021.
Industrial IoT though not a very new domain as it exists for quite few years from the
time when PLC, sensors and network connectivity have become common, will continue
to grow heavily in coming years with a spending estimate globally of USD 1.3 trillion by
2026, according to Business Insider Intelligence.
Consumer IoT devices such as smart watch, smart speakers and IoT enabled home
appliances are some of the widespread and most usage of IoT, with millions of units sold
worldwide each year.
Usage of IoT can help to enable new business models which were never conceived before.
For example, smart home appliance OEMs can provide service instead of product such
as air-conditioning a service, where the OEM can monitor manages air-conditioning
equipment at customer location using IoT for which the customers will be charged based
on their usage. The equipment status and faults can be easily detected remotely through
IoT data analysis and OEMs can inform the service providers proactively to carry out the
maintenance to avoid downtimes due to sudden faults of the equipment. Essentially, the
product selling business models can change to service provider business model by this
approach.
IoT is not about a single component or software but a combination of multiple software,
components and data that helps to build an IoT application. You need to understand the
different building blocks of IoT to get an overall idea and how to design such scenarios
enabled by IoT. In the next sections, you will learn about the different building blocks of
IoT.