Industrial Training Report (1) .T
Industrial Training Report (1) .T
REPORT/SUMMER TRAINING
REPORT
HOME AUTOMATION USING IOT
NNN
Submitted By
I hereby declare that the Industrial/ Summer Training Report entitled "Home
Automation Using IoT" is an authentic record of my own work as requirements of
Industrial Training during the period from 22nd July 2023 to 26th August 2023 for the
award of degree of B.Tech. (IIOT), School of Engineering and Technology, VIPS-TC.
(Tanishq Pancholi)
(0197711721)
Date: ____________________
i
ACKNOWLEDGEMENT
Sincerely,
Tanishq Pancholi
ii
ABOUT COMPANY
iii
TABLE OF CONTENT
iv
TABLE OF IMAGES
v
INTRODUCTION TO PROJECT
A home automation system utilizing the ESP8266 microcontroller and MQTT broker
aims to create an intelligent and interconnected living environment where various devices
and appliances can be controlled, monitored, and managed remotely. This system
addresses the growing need for efficient and user-friendly home management solutions
that enhance convenience, energy efficiency, and security. The core problem this project
addresses is the lack of a unified platform that enables homeowners to seamlessly control
and monitor a wide range of devices, including lights, thermostats, door locks, security
cameras, and more. Traditional methods of home automation often involve multiple
disjointed apps or physical controls, leading to a fragmented user experience. This
complexity can hinder efficient energy usage, remote management, and realtime
monitoring of the home. The proposed solution leverages the ESP8266 microcontroller, a
cost-effective and Wi-Fienabled device with the capability to connect various sensors and
actuators. MQTT (Message Queuing Telemetry Transport) is utilized as the
communication protocol, enabling efficient data exchange between the ESP8266 devices
and a central MQTT broker. This broker acts as the intermediary hub for transmitting
messages between devices, sensors, and the user interface. The key features and
functionalities of the home automation system include: Remote Control: Users can
control devices from anywhere with an internet connection. This eliminates the need to
physically interact with devices, enhancing convenience and accessibility. Energy
Efficiency: Through remote control and scheduling, users can optimize energy usage. For
instance, lights and appliances can be turned off automatically when not needed,
contributing to reduced energy consumption. Real-time Monitoring: Sensors integrated
into the system provide real-time data about the environment, such as temperature,
humidity, and occupancy. Users can monitor this data via a centralized dashboard,
enabling informed decisions and a comfortable living space. 4 | P a g e Security
Enhancement: Integration with security cameras, door locks, and motion sensors
enhances home security. Users receive immediate alerts for suspicious activities and can
take necessary actions. Customization and Scenes: The system allows users to create
personalized automation scenarios or scenes. For example, a "Good Morning" scene
1
might involve gradually turning on lights and adjusting the thermostat upon waking up.
Expandability: The modular nature of the ESP8266-based devices allows for easy
addition of new devices and functionalities as needed. User-Friendly Interface: A user-
friendly mobile or web application serves as the interface for controlling and monitoring
devices. This simplifies the user experience and encourages wider adoption. Reliability
and Scalability: MQTT's publish-subscribe model ensures reliable message delivery,
even in unreliable network conditions. The system can also be scaled to accommodate a
growing number of devices. In conclusion, the home automation system powered by
ESP8266 and MQTT broker offers a comprehensive solution to the challenges of
traditional home management methods. By seamlessly integrating devices, offering
remote control and monitoring, and enhancing energy efficiency and security, this system
aligns with the modern lifestyle's demand for convenience and connectivity. It not only
streamlines daily tasks but also empowers users to create a safer, more comfortable, and
energy-efficient living environment.
.
2
COMPONENTS USED
2. DHT Sensor :- The DHT sensor family, comprising models like the DHT11 and
DHT22 (AM2302), stands out as a pivotal player in the realm of digital temperature
and humidity sensing, owing to its amalgamation of affordability, simplicity, and
functional versatility. These sensors operate on a digital interface, with a single-wire
connection simplifying integration into microcontrollers and digital systems. The
DHT22 boasts a broader operational range and enhanced accuracy compared to the
more basic DHT11. Functionally, these sensors employ a streamlined protocol, where
the host device initiates communication and receives temperature and humidity data
in return. Widely deployed in applications ranging from weather monitoring and
3
home automation to industrial setups, the DHT sensors provide crucial insights into
ambient conditions. Despite their utility, it's imperative to consider limitations such as
a finite refresh rate, potentially necessitating calibration for precision-critical
scenarios. Implementing best practices, such as ensuring a stable power supply and
guarding against condensation, enhances the reliability of these sensors, making them
indispensable tools for diverse projects.
4
Fig 3: Breadboard
5
TECHNOLOGY USED
6
allows users to wire together various nodes, each representing a specific function or
service, to create a visual representation of a workflow or application logic. It is
particularly popular in the context of the Internet of Things (IoT) and home
automation due to its ability to easily integrate with a wide range of devices and
services. Node-RED supports a large number of nodes out of the box, enabling users
to interact with databases, APIs, hardware devices, and more. Its extensibility is
further enhanced through a vibrant community that contributes additional nodes and
functionality. Node-RED simplifies the development process by abstracting the
complexities of coding, making it accessible to both developers and non-developers
alike, fostering a collaborative environment for building powerful and scalable
applications.
8
CODE
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
// Uncomment one of the lines bellow for whatever DHT sensor type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Change the credentials below, so your ESP8266 connects to your router
const char *mqtt_broker = "broker.emqx.io";
const char *mqtt_username = "admin";
const char *mqtt_password = "public123";
const int mqtt_port = 1883;
const char *ssid = "The IOT Academy";
const char *pass = "academyiot@23";
//For example
//const char* mqtt_server = "192.168.1.106";
// Initializes the espClient. You should change the espClient name if you have
multiple ESPs running in your home automation system
WiFiClient espClient;
PubSubClient client(espClient);
// DHT Sensor - GPIO 5 = D1 on ESP-12E NodeMCU board
const int DHTPin = D4;
// Lamp - LED - GPIO 4 = D2 on ESP-12E NodeMCU board
const int switch1 = D1;
const int switch2 = D2;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
// Timers auxiliar variables
long now = millis();
long lastMeasure = 0;
9
// This functions connects your ESP8266 to your router
void setup_wifi()
{
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
// This function is executed when some device publishes a message to a topic that
your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device
publishes a message to a topic that
// your ESP8266 is subscribed you can actually do something
10
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic room/lamp, you check if the message is
either on or off. Turns the lamp GPIO according to the message
if(topic=="room/switch1") {
Serial.print("Changing Room lamp to ");
if(messageTemp == "on") {
digitalWrite(D1, HIGH);
Serial.print("On");
} else if(messageTemp == "off") {
digitalWrite(D1, LOW);
Serial.print("Off");
}
}
if(topic=="room/switch2") {
Serial.print("Changing Room lamp to ");
if(messageTemp == "on") {
digitalWrite(D2, HIGH);
Serial.print("On");
} else if(messageTemp == "off") {
digitalWrite(D2, LOW);
Serial.print("Off");
}
}
Serial.println();
}
// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your
ESP8266
11
void reconnect()
{
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
/*
YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING
PROBLEMS WITH MQTT
MULTIPLE CONNECTIONS
To change the ESP device ID, you will have to give a new name to the
ESP8266.
Here's how it looks:
if (client.connect("ESP8266Client")) {
10 | P a g e
You can do it like this:
if (client.connect("ESP1_Office")) {
Then, for the other ESP:
if (client.connect("ESP2_Garage")) {
That should solve your MQTT multiple connections problem
*/
if (client.connect("ESP8266Client", MQTT_username, MQTT_password)) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more LEDs in this example)
client.subscribe("room/lamp");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
12
delay(5000);
}
}
}
// The setup function sets your ESP GPIOs to Outputs, starts the serial
communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup()
{
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
dht.begin();
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
// For this project, you don't need to change anything in the loop function.
Basically it ensures that you ESP is connected to your broker
void loop()
{
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("ESP8266Client", MQTT_username, MQTT_password);
now = millis();
// Publishes new temperature and humidity every 30 seconds
if (now - lastMeasure > 30000) {
lastMeasure = now;
13
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperatureC = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float temperatureF = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
14
CODE EXPLANATION
Header Files:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
These lines include the necessary libraries for working with the ESP8266, MQTT, and
the DHT sensor.
Client Initialization:
WiFiClient espClient;
PubSubClient client(espClient);
These lines initialize the Wi-Fi and MQTT clients.
15
const int switch1 = D1;
const int switch2 = D2;
DHT dht(DHTPin, DHTTYPE);
These lines define the GPIO pins for the DHT sensor and switches (lamps). The DHT
sensor is connected to GPIO D4, and two switches are connected to GPIO D1 and D2.
Setup Function:
void setup()
This function initializes GPIO pins, serial communication, Wi-Fi, MQTT, and sets the
callback function.
Loop Function:
void loop()
16
This function is the main execution loop. It checks if the MQTT client is connected and
attempts to reconnect if necessary. It also publishes temperature and humidity readings to
the MQTT broker every 30 seconds.
Overall, this code creates an ESP8266-based IoT device that can measure temperature
and humidity, control two switches (lamps) based on MQTT messages, and publish
sensor readings to an MQTT broker.
17
NODE RED DASHBOARD
18
NODE RED MOBILE VIEW
19
CONCLUSIONS
In conclusion, the presented smart home project, built on Node-RED, MQTT, and
ESP8266, serves as a foundational platform with vast potential for future enhancements.
The project's expansion into more comprehensive sensor integration, incorporation of
machine learning algorithms, development of a dedicated mobile application, and
emphasis on security and energy optimization mark the beginning of a sophisticated and
intelligent home automation system.
The envisioned future of the project involves a seamless integration of diverse sensors
that enable a nuanced understanding of the home environment. From air quality
monitoring to security and adaptive lighting, the expanded sensor capabilities promise a
holistic approach to smart home management.
The introduction of machine learning algorithms elevates the project to a predictive and
adaptive system. By learning from historical data, the smart home system can optimize
energy usage, anticipate user preferences, and enhance overall efficiency. This capability
not only provides convenience but aligns with the growing demand for intelligent, self-
learning systems.
The development of a dedicated mobile application addresses the need for user-friendly
interfaces and remote accessibility. Real-time data visualization, device control, and
timely notifications empower users to actively manage and monitor their homes from the
convenience of their smartphones. This step towards greater user interaction ensures that
the smart home system is not just a technological marvel but also a practical and user-
centric solution.
Security enhancements are paramount in the era of interconnected devices. Advanced
encryption, secure MQTT communication, and anomaly detection mechanisms contribute
to a robust security framework. By prioritizing user privacy and safeguarding against
cyber threats, the smart home system becomes a trusted and reliable part of the user's
daily life.
Energy optimization emerges as a key focus, promoting sustainability and cost savings.
Predictive scheduling, integration of renewable energy sources, and optimization based
on user behavior all contribute to a more energy-efficient smart home. As environmental
20
consciousness grows, these features become essential for aligning the smart home system
with modern ecological standards.
Voice and gesture control represent a futuristic leap in human-machine interaction. By
enabling users to interact with their smart home through natural language or simple
gestures, the system becomes more intuitive and accessible. This feature is not just a
technological advancement but a testament to the commitment to user convenience and
inclusivity.
Cloud integration transforms the smart home system into a scalable and flexible solution.
Remote access, advanced analytics, and seamless updates contribute to a dynamic and
future-proof infrastructure. The cloud becomes the backbone for ensuring the smart home
system stays current, adaptable, and capable of meeting evolving user needs.
Fostering a community around the project through open-source collaboration is a
visionary approach. By inviting contributions, feedback, and diverse perspectives, the
smart home system becomes a collective effort. This collaborative model ensures
continuous improvement, innovation, and adaptability to emerging technologies, making
it a sustainable and evolving solution for home automation.
In conclusion, the presented smart home project not only addresses the current needs of
home automation but lays the foundation for a dynamic and intelligent future. The
envisioned enhancements encompass a holistic approach, focusing on user experience,
security, energy efficiency, and adaptability. As technology advances and user
expectations evolve, this smart home system is poised to remain at the forefront of
innovation and serve as a benchmark for intelligent home automation solutions.
21
FUTURE SCOPE
22
communication, securing the MQTT broker, and incorporating user authentication
mechanisms are essential steps. Additionally, introducing anomaly detection
algorithms can enhance the system's ability to identify and respond to suspicious
activities, providing users with a heightened sense of security. A secure smart home
system not only protects the user's privacy but also ensures the reliability and
integrity of the entire home automation infrastructure.
23
environmental sustainability goals but also results in cost savings for users in the long
run.
Voice and Gesture Control: The integration of voice and gesture control represents
a futuristic leap in human-machine interaction. Enabling users to control devices and
interact with the smart home system through voice commands or simple gestures
enhances accessibility and convenience. This feature is particularly valuable in
scenarios where hands-free operation is desired, such as when cooking, carrying
groceries, or for individuals with mobility challenges. As natural language processing
and gesture recognition technologies continue to advance, integrating these features
ensures the smart home system remains at the forefront of user-friendly and intuitive
home automation solutions.
Cloud Integration: Leveraging cloud services for data storage, analysis, and remote
access brings scalability and flexibility to the smart home system. Cloud integration
enables users to access their data from anywhere, enhancing the system's remote
monitoring capabilities. Additionally, the cloud can serve as a powerful platform for
advanced analytics, allowing users to gain insights into their home's performance and
efficiency over time. Cloud-based solutions also facilitate seamless updates and
maintenance, ensuring the smart home system stays current with the latest features
and security patches.
Community and Open Source Collaboration: Fostering a community around the
project through open-source collaboration can accelerate innovation and
development. Opening up the project to contributions from the community allows for
diverse perspectives, expertise, and continuous improvement. Collaborative efforts
can lead to the identification of new use cases, the development of plugins for
additional devices or platforms, and the discovery of potential security vulnerabilities.
Creating a vibrant community ensures the smart home system remains adaptable to
evolving technologies and user needs, making it a sustainable and continuously
improving solution for home automation.
24