Home Automation Using Iot
Home Automation Using Iot
Project Overview:
The objective of this project is to design and implement a smart home automation system that allows
users to control and monitor their home appliances remotely using a smartphone application. The system
will use various sensors and communication modules to automate and enhance the functionality of home
appliances.
Project Components:
2. Sensors: Temperature, humidity, motion, and light sensors to collect environmental data.
3. Actuators: Relays and smart plugs to control home appliances like lights, fans, and air
conditioners.
4. Communication Modules: Wi-Fi module for internet connectivity and communication with the
smartphone app.
5. Smartphone Application: A mobile app (Android/iOS) to interface with the home automation
system.
6. Cloud Platform: A cloud service (such as AWS IoT or Google Firebase) to store data and facilitate
remote control and monitoring.
Project Phases:
• Define Objectives: Clearly outline the goals and functionalities of the smart home automation
system.
• Literature Review: Study existing smart home systems, IoT protocols, and relevant technologies.
• Architecture Design: Design the overall system architecture, including hardware and software
components.
• Circuit Design: Create circuit diagrams for connecting sensors, actuators, and communication
modules to the microcontroller.
• Software Design: Plan the software architecture for both the microcontroller firmware and the
smartphone application.
Phase 3: Implementation
• Hardware Assembly:
• Firmware Development:
• Write and upload the firmware to the microcontroller to read sensor data and control
actuators.
• Implement communication protocols (e.g., MQTT) for data exchange between the
microcontroller and the cloud.
• Develop the user interface (UI) for the smartphone app using tools like Android Studio or
Xcode.
• Implement features to control appliances, view sensor data, and receive notifications.
• Cloud Integration:
• Set up the cloud service to store sensor data and facilitate remote control.
• Unit Testing: Test individual components (sensors, actuators, microcontroller, app) to ensure they
work correctly.
• Integration Testing: Test the entire system to ensure seamless communication between the
microcontroller, cloud, and mobile app.
• System Deployment: Set up the smart home automation system in a real home environment.
• User Training: Create a user manual and provide training for using the system.
• Sensors: DHT11 (temperature and humidity), PIR motion sensor, LDR (light sensor).
• Connect Sensors: Interface the DHT11, PIR, and LDR sensors to the GPIO pins of the ESP8266.
• Power Supply: Ensure proper power supply for the ESP8266 and other components.
3. Firmware Development:
• Environment: Use Arduino IDE for writing and uploading code to ESP8266.
• Libraries: Use libraries for sensor data acquisition (e.g., DHT library for DHT11) and Firebase
communication.
• Code Structure:
• Loop: Continuously read sensor data, update Firebase, and check for control commands
from the app.
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <DHT.h>
#define DHTPIN D2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define RELAY_PIN D1
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
dht.begin();
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Firebase.pushFloat("temperature", temperature);
Firebase.pushFloat("humidity", humidity);
if (Firebase.getInt("relay") == 1) {
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
delay(2000);
}
• Environment: Use Android Studio (Java/Kotlin) or Xcode (Swift) for app development.
• Features:
5. Cloud Integration:
• Firebase Setup:
• Database Structure:
• Hardware Testing: Ensure all sensors and actuators are functioning correctly.
• Software Testing: Test the firmware and mobile app for bugs.
• Integration Testing: Test the entire system for smooth operation and communication.
• Install System: Set up the system in a real environment and test its functionality.
• User Manual: Create documentation for users to understand and operate the system.
• Final Report: Document the entire project process, including objectives, design, implementation,
testing, and results.
Conclusion:
Detailed Breakdown for the Smart Home Automation System Using IoT
1. Component Selection and Procurement
Microcontroller:
• ESP8266 NodeMCU: It's cost-effective, has built-in Wi-Fi, and is easy to program.
Sensors:
Actuators:
Additional Components:
Circuit Diagram:
• Create a detailed schematic showing connections between the ESP8266, sensors, relays, and
power supply.
• Ensure proper GPIO pin assignment on the ESP8266 for each component.
Assembly:
• Connect the relay module to a digital pin (ensure it's able to handle the load of the appliances).
PIR Sensor
- VCC to 5V (if compatible) or 3.3V
- GND to GND
- Output to D3
LDR
- One end to 3.3V
- Other end to A0 through a 10k resistor to GND (forming a voltage
divider)
Relay Module
- VCC to 5V
- GND to GND
- IN1 to D1
3. Firmware Development
Development Environment:
Libraries:
• Install necessary libraries (DHT, Firebase, etc.) via Arduino Library Manager.
Code Structure:
• Setup Function:
• Initialize sensors.
• Connect to Wi-Fi.
• Initialize Firebase.
• Loop Function:
#define DHTPIN D2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define PIRPIN D3
#define LDRPIN A0
#define RELAY_PIN D1
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
dht.begin();
pinMode(RELAY_PIN, OUTPUT);
pinMode(PIRPIN, INPUT);
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
int motion = digitalRead(PIRPIN);
int lightLevel = analogRead(LDRPIN);
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Firebase.setFloat("temperature", temperature);
Firebase.setFloat("humidity", humidity);
Firebase.setInt("motion", motion);
Firebase.setInt("lightLevel", lightLevel);
if (Firebase.getInt("relay") == 1) {
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
delay(2000);
}
Development Environment:
• Use Android Studio for Android app or Xcode for iOS app.
App Features:
• UI Design: Use XML layouts (Android) or Storyboard (iOS) for interface design.
• Realtime Data: Use Firebase Realtime Database to fetch and display sensor data.
5. Cloud Integration
Firebase Setup:
Security Rules:
• Ensure your database rules only allow authenticated users to read/write data.
Unit Testing:
• Test each sensor and actuator individually to ensure they work correctly.
Integration Testing:
• Test the entire system to ensure all components communicate and function together.
Debugging:
• Use logcat (Android Studio) or console (Xcode) for debugging the mobile app.
System Deployment:
User Manual:
Final Report:
• Include sections on project objectives, system design, implementation, testing, results, and future
work.
Conclusion
This comprehensive project will showcase your skills in telecommunication engineering, particularly in
areas related to IoT, embedded systems, and software development. It will prepare you well for a career
in consumer electronics, demonstrating your ability to design and implement a complex, real-world
system.
2. Add your Android app to the project by registering it and downloading the google-services.json
file.
3. Place the google-services.json file in the app directory of your Android project.
2. XML Layout
<TextView
android:id="@+id/textViewStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Not Logged In"
android:textSize="18sp" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
<Button
android:id="@+id/buttonLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Logout"
android:visibility="gone" />
<TextView
android:id="@+id/textViewTemperature"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Temperature: "
android:textSize="18sp" />
<TextView
android:id="@+id/textViewHumidity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Humidity: "
android:textSize="18sp" />
<TextView
android:id="@+id/textViewMotion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Motion: "
android:textSize="18sp" />
<TextView
android:id="@+id/textViewLightLevel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Light Level: "
android:textSize="18sp" />
<Switch
android:id="@+id/switchRelay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relay" />
</LinearLayout>
package com.example.smarthome;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewStatus = findViewById(R.id.textViewStatus);
textViewTemperature = findViewById(R.id.textViewTemperature);
textViewHumidity = findViewById(R.id.textViewHumidity);
textViewMotion = findViewById(R.id.textViewMotion);
textViewLightLevel = findViewById(R.id.textViewLightLevel);
buttonLogin = findViewById(R.id.buttonLogin);
buttonLogout = findViewById(R.id.buttonLogout);
switchRelay = findViewById(R.id.switchRelay);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// This example uses a default email and password. You
should implement a proper login mechanism.
mAuth.signInWithEmailAndPassword("test@example.com",
"password")
.addOnCompleteListener(MainActivity.this, task -> {
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmail:failed",
task.getException());
Toast.makeText(MainActivity.this,
"Authentication failed.", Toast.LENGTH_SHORT).show();
}
});
}
});
buttonLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAuth.signOut();
}
});
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled",
databaseError.toException());
}
});
mDatabase.child("humidity").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
float humidity = dataSnapshot.getValue(Float.class);
textViewHumidity.setText("Humidity: " + humidity + "%");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled",
databaseError.toException());
}
});
mDatabase.child("motion").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int motion = dataSnapshot.getValue(Integer.class);
textViewMotion.setText("Motion: " + (motion == 1 ?
"Detected" : "Not Detected"));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled",
databaseError.toException());
}
});
mDatabase.child("lightLevel").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int lightLevel = dataSnapshot.getValue(Integer.class);
textViewLightLevel.setText("Light Level: " + lightLevel);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled",
databaseError.toException());
}
});
mDatabase.child("relay").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int relayState = dataSnapshot.getValue(Integer.class);
switchRelay.setChecked(relayState == 1);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled",
databaseError.toException());
}
});
}
}
Notes:
1. Firebase Authentication: The code uses a hardcoded email and password for simplicity. You
should implement a proper authentication mechanism.
2. Firebase Database Rules: Ensure your Firebase Realtime Database rules allow authenticated
users to read and write data.
3. Exception Handling: Add appropriate exception handling and user feedback mechanisms.
4. UI Enhancements: Consider improving the UI with more interactive and visually appealing
elements.
This example provides a basic framework for integrating Firebase with an Android application to control
and monitor a smart home system. Customize it further to fit your specific project requirements and
improve the user experience.
Below is a basic circuit diagram for connecting the components of the Smart Home Automation System
using IoT. This diagram illustrates how to connect the ESP8266 microcontroller, sensors (DHT11, PIR
motion sensor, LDR), relay module, and power supply:
Component Connections:
1. ESP8266 NodeMCU:
2. DHT11 Sensor:
• Other End: Connect to pin A0 through a 10k resistor, forming a voltage divider.
5. Relay Module:
Additional Notes:
• Ensure proper power supply connections and voltage levels for all components.
• Double-check the pin assignments and ensure they match the code and circuit diagram.
• Use appropriate resistors and voltage levels to avoid damaging the components.
• Consider using a breadboard for prototyping before soldering components onto a PCB.
• Test each component individually and verify their functionality before integrating them into the
final circuit.