Home automation using Arduino and sensors

0
352
smart home automation
smart home automation

Home automation using Arduino and sensors) makes a home smarter. The smart home is a term that has huge demand in the present world and huge possibilities in near future. A home automation system may control lights, temperature, climate, entertainment, and many other appliances. Home security is also a part of automation which includes security control and alarm system.

How Smart Home Automation work

Smart Home automation (using Arduino and Wi-Fi) typically controls all the appliances from a central hub. The end-user interface may vary based on the application. The control system could be wall-mounted, computers, a mobile phone application, or a web interface. It depends on the developers and users. In our proposed system we have used a mobile phone android application-based control system.

Smart home automation, Home automation using arduino
Block Diagram

Components used:

  • Arduino Mega 2560
  • Esp8266 NodeMCU
  • Channel Relay Module
  • MQ2 Gas Sensor
  • DHT22
  • LDR Sensor Module

In our home automation system we have developed an android based home automation system that can control home appliances based on user command. It also has its own intelligence to control all the appliances according to the user is given modes. If a user wishes to turn on or off any individual appliances it can do that. The most significant feature of this system is mode based operation. Like when the user activates the auto mode then all the lights and fan will turn on or off with its own intelligence. If there is enough light in the room than lights will remain off. On the other hand, if the light is insufficient than lights will be turned on automatically. Based on different sensor values these actions are executed.

The user controls the modes and appliances by an android based device through our application named “huom”. The user command is transmitted from the device via the internet. The esp8266 NodeMCU then receives the command from the internet via Wi-Fi. The NodeMCU then passes the command to the controller board Arduino mega 2560. Arduino then executes the commanded operation. So huom app is for end-users to give input. Esp8266 NodeMCU and Arduino Mega is the control unit of the system. The Arduino takes input from the esp8266 for executing actions. For the execution of the action, Arduino also takes inputs from the sensors. Lastly, the Arduino runs the appliances according to the user’s desired command.

Android Application (Home automation using Arduino through the android device)

For Smart home automation using Arduino and Wi-Fi, we have developed an android app (Huom) to control the system. Users can control everything of the system by the android app. User can also set modes from the application

smart home automation
smart home automation

It has 14 buttons to control different appliances and modes. The first 8 buttons are for turning on or off optimum, auto, sleeping, and activate the mode. The last 6 buttons are for controlling individual lights and fans. There is another text box for entering the IP address. As our esp8266 module generates dynamic IP addresses after connecting new networks or after reset of previous connection. The user interface of the application Huom is very interactive and very user friendly. Users can turn any button on by pressing the 19 buttons and which will give the command the system to proceed on the command.

We have developed this application by using the MIT app inventor 2. MIT app inventor 2 is an open platform for developers to develop a useful android based application. In MIT app inventor 2 the back-end operations are designed by block coding. Block codes are easier to design algorithms. The block codes of Huom are designed in a way, when a button is pressed it will connect to the IP address as provided by the user. That IP address is the NodeMCU’s IP which is connected with a network. This is how the back-end operations are designed in Huom.

Modes of Operation:

Optimum Mode

Optimum is the most power-saving mode of all. The algorithm of this mode is, first it will activate after getting motion from the motion sensor. Then, based on temperature and light intensity from the temperature and Ldr sensors appliances will activate. Condition for turning on the fan is temperature must be greater than 21 degrees Celsius.  For turning on the lights Ldr sensor value must be less than 1000. Otherwise, both appliances will be turned off. This is an automatic process where users do not need to bother about controlling. Moreover, this mode is saving power consumption by deactivating every appliance when there is no motion detected.

smart Home automation ,home automation using arduino and sensors
Flowchart of optimum mode

As shown in above Fig if no one is in the room every appliance will remain in off condition. Than if someone enters in room all appliances will activate and execute according to sensor values.

Auto Mode

Features of this mode is it controls the appliances automatically. It is more comfortable than the optimum. Auto mode has the same features as optimum mode except activating after getting any motion in the room. It does not depend on motions. So, the algorithm for turning the fan and lights on is the same as previously. Based on threshold temperature and Ldr sensor value as mentioned in optimum appliances will turn on and off. Condition for turning on the fan is temperature must be greater than 21 degrees Celsius.

Smart Home automation, home automation using arduino and sensors
Flowchart of Auto Mode
As shown in Fig above if you want to relax just activate the auto mode and relax. Once the mode is on then it does not change its mode until the user changes it. Once the auto mode is activated then if the huom application goes offline it does not change its state of mode or functionality. It will continue running on this mode.
Sleeping Mode

This mode is designed for sleeping time. The features of this mode is it will turn off all the lights and make the room dark so that users can sleep without any disturbance of light. The other feature is fan controlling. It will also control fan based on temperature. The algorithm of fan is designed in a way, it will be turned on if the temperature rises above the threshold value

Smart Home automation, home automation using arduino and sensors
Flowchart of Sleeping Mode
Active Mode

Active mode is designed for turning on and off the appliances at the same time. Activating this mode will turn on all the appliances and do not change its state until the mode is deactivated

Smart Home automation, home automation using arduino and sensors
Flowchart of Active Mode

As shown in Fig above this mode is for a very busy environment where users want to activate all the fans and lights at the same time. It can also be seen from Fig that is the algorithm for this model is very simple.

Coding (smart home automation):

DHT22 Interfacing with Arduino

#include <dht.h>
#define dataPin 8 // Defines pin number to which the sensor is connected
dht DHT; // Creats a DHT object
void setup()
{
Serial.begin(9600);
}
void loop()
{
//Uncomment whatever type you're using!
int readData = DHT.read22(dataPin); // DHT22/AM2302
//int readData = DHT.read11(dataPin); // DHT11
float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity
// Printing the results on the serial monitor
Serial.print("Temperature = ");
Serial.print(t);
Serial.print(" ");
Serial.print((char)176);//shows degrees character
Serial.print("C | ");
Serial.print((t * 9.0) / 5.0 + 32.0);//print the temperature in Fahrenheit

Serial.print(" ");
Serial.print((char)176);//shows degrees character
Serial.println("F ");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" % ");
Serial.println("");
delay(2000); // Delays 2 seconds
}

MQ2 Interfacing with Arduino

float sensorValue; //variable to store sensor value
void setup()
{
 Serial.begin(9600); // sets the serial port to 9600
 Serial.println("Gas sensor warming up!");
 delay(20000); // allow the MQ-6 to warm up
}
void loop()
{
 sensorValue = analogRead(MQ2pin); // read analog input pin 0
 Serial.print("Sensor Value: ");
 Serial.print(sensorValue);
 if(sensorValue > 300)
 {
 Serial.print(" | Smoke detected!");
 }
 Serial.println("");
 delay(2000); // wait 2s for next reading
}

Interface DHT22 with Esp8622

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "DHT.h"
// Uncomment one of the lines below 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
/*Put your SSID & Password*/
const char* ssid = "YourNetworkName"; // Enter SSID here
const char* password = "YourPassword"; //Enter Password here
ESP8266WebServer server(80);
// DHT Sensor
uint8_t DHTPin = D8;

// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
float Temperature;
float Humidity;
void setup() {

 Serial.begin(115200);
 delay(100);
 pinMode(DHTPin, INPUT);
 dht.begin();
 Serial.println("Connecting to ");
 Serial.println(ssid);
 //connect to your local wi-fi network
 WiFi.begin(ssid, password);
 //check wi-fi is connected to wi-fi network
 while (WiFi.status() != WL_CONNECTED) {
 delay(1000);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected..!");
 Serial.print("Got IP: "); Serial.println(WiFi.localIP ());
 server.on("/", handle_OnConnect);
 server.onNotFound(handle_NotFound);

server.begin();
 Serial.println("HTTP server started");
}
void loop() {
 server.handleClient();
}
void handle_OnConnect() {
Temperature = dht.readTemperature(); // Gets the values of the temperature
 Humidity = dht.readHumidity(); // Gets the values of the humidity
 server.send(200, "text/html", SendHTML(Temperature,Humidity));
}
void handle_NotFound(){
 server.send(404, "text/plain", "Not found");
}
String SendHTML(float Temperaturestat,float Humiditystat){
 String ptr = "<!DOCTYPE html> <html>\n";
 ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initialscale=1.0, user-scalable=no\">\n";
 ptr +="<title>ESP8266 Weather Report</title>\n";
 ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto;
text-align: center;}\n";
 ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
 ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";

 ptr +="</style>\n";
 ptr +="</head>\n";
 ptr +="<body>\n";
 ptr +="<div id=\"webpage\">\n";
 ptr +="<h1>ESP8266 NodeMCU Weather Report</h1>\n";
 ptr +="<p>Temperature: ";
 ptr +=(int)Temperaturestat;
 ptr +="°C</p>";
 ptr +="<p>Humidity: ";
 ptr +=(int)Humiditystat;
 ptr +="%</p>";
 ptr +="</div>\n";
 ptr +="</body>\n";
 ptr +="</html>\n";
 return ptr;
}

Leave a Reply