Home Automation and Security System

Home Automation and Security System

This project investigates the potential of ‘Full Home Control’, which is the aim of the Home Automation and Security Systems. The analysis and implementation of the home automation technology using Global System for Mobile Communication (GSM) module and various sensors to control home appliances such as light, conditional system, and security system via Short Message Service (SMS). Home Security system is designed to prevent the opening of the door by unauthorized persons. The proposed work is focused on functionality of the GSM protocol, which allows the user to control the target system away from residential, using the frequency bandwidths. The concept of serial communication and AT-commands has been applied towards development of the smart GSM-based home automation system. Home owners will be able to receive feedback status of any home appliances under control whether the appliances are switched on or off remotely from their mobile phone.

The previous implemented modules are being integrated and hence the project is completed.

Hardware Requirements:

  • Arduino Uno x 2
  • Motor Driver (alternative- Servo Motor)
  • DC Motor
  • DHT11 Temperature Humidity Sensor
  • NodeMCU ESP8266 Wifi module
  • HC-05 Bluetooth
  • MQ2 Sensor
  • Buzzer
  • Keypad
  • 16×2 LCD
  • LEDS
  • Connecting Wires
  • 9V Battery

Software Requirements:

  • Arduino IDE
  • Android Application of Home Automation and Security System

The whole project is operated using two arduinos, one for Door locking system and sensing MQ2 Sensor data and the other is used to control the home appliances using Bluetooth application and the final functionality is nodeMCU ESP8266 Wifi module which is used to transport the data sensed by DHT11 Temperature Sensor to cloud/Server.

Home Automation and Security System House Model

Part one- Door Locking and Security System with Sensing MQ2 Sensor Data

  • Module is set up.
  • If the user wants to open the door, password is required.
  • If the password is valid, the door opens for 5 seconds and then it closes automatically.
  • If the password is invalid, the door remains closed.
  • In order to change the password as per the user’s convenience, the password entered must be valid and then the user must press ‘D’, it asks for current password and then asks for new password.
  • Next, It starts sensing the MQ2 Sensor data and if the value exceeds the threshold value, then the buzzer strats beeping.
  • Code is given below:
  • #include <Keypad.h>
    
    
    #include <LiquidCrystal.h>
    
    
    /*-------------------------------KEYPAD---------------------------------------*/
    const byte numRows= 4; //number of rows on the keypad
    const byte numCols= 4; //number of columns on the keypad
    char keypressed; 
    char keymap[numRows][numCols]=
    {
    {'1', '2', '3', 'A'},
    {'4', '5', '6', 'B'},
    {'7', '8', '9', 'C'},
    {'*', '0', '#', 'D'}
    };
    int in1=2;
    int in2=9;
    int smoke=A1;
    int buzzer=10;
    int sensorThres = 300;
    int count=0;
    
    //Code that shows the the keypad connections to the arduino terminals
    byte rowPins[numRows] = {A5,A4,A3,A2};//Rows 0 to 3
    byte colPins[numCols] = {13,12,11,A0};//Columns 0 to 3 
    //initializes an instance of the Keypad class
    Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
    
    /*-------------------------------CONSTANTS------------------------------------*/
    LiquidCrystal lcd(8,7,6,5,4,3); //LCDr
    
    /*-------------------------------VARIABLES------------------------------------*/
    String password="123AB"; //Variable to store the current password
    String tempPassword=""; //Variable to store the input password
    int cnf_pass; //Check twice the new passoword
    boolean armed = false; //Variable for system state (armed:true / unarmed:false)
    boolean input_pass; //Variable for input password (correct:true / wrong:false)
    boolean storedPassword = true;
    boolean changedPassword = false;
    boolean checkPassword = false;
    int i = 1; //variable to index an array
    /*----------------------------------------------------------------------------*/
    
    void setup(){
     Serial.begin(9600);
     pinMode(in1,OUTPUT);
     pinMode(in2,OUTPUT);
    
     pinMode(buzzer, OUTPUT);
     pinMode(smoke, INPUT);
     
     lcd.begin(16, 2); //Setup the LCD's number of columns and rows 
     //Print welcome message...
     lcd.setCursor(0,0);
     lcd.print(" Door Locking ");
     lcd.setCursor(0,1);
     lcd.print(" System ");
     delay(2000);
     lcd.clear();
     
     unlockTheDoor();
     
    }
    
    void loop() { //Main loop
     
     int analogSensor = analogRead(smoke);
    
     Serial.print("Pin A0: ");
     Serial.println(analogSensor);
     // Checks if it has reached the threshold value
     if (analogSensor > sensorThres)
     {
     tone(buzzer, 800, 300);
     }
     else
     {
     noTone(buzzer);
     }
     delay(2000);
     
    }
    /********************************FUNCTIONS*************************************/
    
    void unlockTheDoor(){
     lockAgain: //goto label
     tempPassword="";
     lcd.clear();
     i=6;
     //noTone(buzzer);
     digitalWrite(in1, LOW);
     digitalWrite(in2, LOW);
     while(!checkPassword){
     lcd.setCursor(0,0);
     lcd.print("For Open door: ");
     lcd.setCursor(0,1);
     lcd.print("PASS>");
     keypressed = myKeypad.getKey(); //Read pressed keys
     if (keypressed != NO_KEY)
     { 
     if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' || keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' || keypressed == '8' || keypressed == '9' || keypressed == 'A' || keypressed == 'B' || keypressed == 'C')
     {
     tempPassword += keypressed;
     lcd.setCursor(i,1);
     lcd.print("*"); //Put * on lcd
     i++;
     //tone(buzzer,800,200); //Button tone
     }
     else if (keypressed == 'D')
     {
     changePassword();
     goto lockAgain;
     }
     else if (keypressed=='#')
     {
     break;
     }
     else if (keypressed == '*'){ 
     count++;//Check for password
     if (password==tempPassword)
     {//If it's correct...
     // checkPassword = true;
     Serial.println("Password is valid");
     digitalWrite(in1, HIGH);
     digitalWrite(in2,LOW);
     
     lcd.clear(); 
     lcd.setCursor(0,0);
     lcd.print("Correct password");
     lcd.setCursor(0,1);
     lcd.print("Door is unlocked");
    
     //lcd.clear(); 
     delay(2000);
     digitalWrite(in1, LOW);
     digitalWrite(in2,LOW);
     delay(5000);
     digitalWrite(in1, LOW);
     digitalWrite(in2, HIGH);
    
     delay(2000);
     digitalWrite(in1, LOW);
     digitalWrite(in2,LOW);
     }
     else if(password!=tempPassword)
     { 
     
     Serial.println("Password is invalid");
     count++;
     if(count==3){
     Serial.println("DEFECTED....");
     
     }
     }
     
     goto lockAgain;
     
     }
     }
     }
    }
    
    //Change current password
    void changePassword(){
     retry: //label for goto
     tempPassword="";
     lcd.clear();
     i=1;
     while(!changedPassword){ //Waiting for current password
     keypressed = myKeypad.getKey(); //Read pressed keys
     lcd.setCursor(0,0);
     lcd.print("CURRENT PASSWORD");
     lcd.setCursor(0,1);
     lcd.print(">");
     if (keypressed != NO_KEY){
     if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
     keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
     keypressed == '8' || keypressed == '9' || keypressed == 'A' || keypressed == 'B' || keypressed == 'C'){
     tempPassword += keypressed;
     lcd.setCursor(i,1);
     lcd.print("*");
     i++;
     //tone(buzzer,800,200); 
     }
     else if (keypressed=='#'){
     break;
     }
     else if (keypressed == '*'){
     i=1;
     if (password==tempPassword){
     storedPassword=false;
     //tone(buzzer,500,200);
     newPassword(); //Password is corrent, so call the newPassword function
     break;
     }
     else{ //Try again
     tempPassword=""; 
     lcd.setCursor(0,0);
     lcd.print("Invalid Password");
     lcd.setCursor(0,1);
     lcd.print("Please Try Again");
     goto retry;
     }
     }
     }
     }
    }
    String firstpass;
    //Setup new password
    void newPassword(){
     tempPassword="";
     changedPassword=false;
     lcd.clear();
     i=1;
     while(!storedPassword){
     keypressed = myKeypad.getKey(); //Read pressed keys
     if (cnf_pass==0){
     lcd.setCursor(0,0);
     lcd.print("SET NEW PASSWORD");
     lcd.setCursor(0,1);
     lcd.print(">");
     }
     else{
     lcd.setCursor(0,0);
     lcd.print("One more time...");
     lcd.setCursor(0,1);
     lcd.print(">");
     }
     if (keypressed != NO_KEY){
     if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
     keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
     keypressed == '8' || keypressed == '9' || keypressed == 'A' || keypressed == 'B' || keypressed == 'C'){
     tempPassword += keypressed;
     lcd.setCursor(i,1);
     lcd.print("*");
     i++;
     //tone(buzzer,800,200);
     }
     else if (keypressed=='#'){
     break;
     }
     else if (keypressed == '*'){
     if (cnf_pass == 0){
     firstpass=tempPassword;
     cnf_pass=1;
     newPassword();
     }
     if (cnf_pass==1){
     cnf_pass=0;
     if (firstpass==tempPassword){
     i=1;
     firstpass="";
     password = tempPassword; // New password saved
     tempPassword="";//erase temp password
     lcd.setCursor(0,0);
     lcd.print("PASSWORD CHANGED");
     lcd.setCursor(0,1);
     lcd.print("----------------");
     storedPassword=true;
     // tone(buzzer,500,400);
     delay(2000);
     lcd.clear();
     break;
     }
     else{
     firstpass="";
     newPassword();
     }
     }
     } 
     }
     }
    }
    
    

    Part two- Controlling Home Appliances Using Bluetooth and Android Application

    • User needs to pair the Mobile device Bluetooth with HC-05 Bluetooth
    • Once it is paired, the android application is started and by clicking on blub or fan or any appliances, they are controlled in the house.

Android Application as per the House model:

Android Application for Controlling Home Appliances

Code is given below:

    • #include <SoftwareSerial.h>
       
      SoftwareSerial bluetooth(10, 11); // TX, RX
      
      #define MAX_BUFFER 4
       
      int fan=2;
      int k_led=8;
      int l_led1=7;
      int l_led2=6;
      int b_led=5;
      int g_led=4;
      
      char data;
      char* buffer;
      boolean receiving = false;
      int pos;
      
      void setup() { 
       bluetooth.begin(9600);
       Serial.begin(9600);
       //bluetooth.println("Bluetooth On");
       //pinMode(motor, OUTPUT);
       
       buffer = new char[MAX_BUFFER];
       pinMode(fan,OUTPUT);
       pinMode(k_led,OUTPUT);
       pinMode(l_led1,OUTPUT);
       pinMode(l_led2,OUTPUT);
       pinMode(b_led,OUTPUT);
       pinMode(g_led,OUTPUT);
      } 
      
      void loop() { 
       
       if (bluetooth.available()){
       
       data=bluetooth.read();
       
       
       
       switch(data) {
       //3: End of transmission
       
       case 3: receiving = false; 
       
       bluetooth.print(buffer);
       
       Serial.print(buffer);
       if(buffer[0] == 'a'){
       //Serial.print("a bulb is on...");
       l1_On();
       }
       else if(buffer[0] == 'b')
       {
       l1_Off();
       }
       else if(buffer[0] == 'c')
       {
       c_On(); 
       }
       else if(buffer[0] == 'd')
       {
       c_Off(); 
       }
       else if(buffer[0] == 'e')
       {
       fan_On();
       }
       else if(buffer[0] == 'f')
       {
       fan_Off(); 
       }
       else if(buffer[0] == 'g')
       {
       b_On();
       }
       else if(buffer[0] == 'h')
       {
       b_Off();
       }
       else if(buffer[0] == 'i')
       {
       k_On(); 
       }
       else if(buffer[0] == 'j')
       {
       k_Off(); 
       }
       else if(buffer[0] == 'k')
       {
       g_On();
       }
       else if(buffer[0] == 'l')
       {
       g_Off();
       }
       else if(buffer[0] == 'm')
       {
       all_Off();
       }
       else
       {
       }
       /* speed = buffer2int(buffer);
       
       bluetooth.print("Received: ");
       bluetooth.print(buffer);
       bluetooth.print(", Speed: ");
       bluetooth.println(speed);*/
      
       break; //end message
       default: if (receiving == false) resetData();
       buffer[pos] = data;
       pos++;
       receiving = true; 
       }
       } 
      }
      
       void resetData(){
       for (int i=0; i<=pos; i++) buffer[i] = 0; 
       pos = 0;
      }
       
      int buffer2int(char* buffer){
       int i;
       sscanf(buffer, "%d", &i);
       return i;
      } 
      void l1_On()
      {
       Serial.println("Living room light1 ON");
       digitalWrite(l_led1,HIGH);
      }
      
      void l1_Off()
      {
       Serial.println("Living room light1 OFF");
       digitalWrite(l_led1,LOW);
      }
      
      void c_On()
      {
       Serial.println("Living room ligh2 ON");
       digitalWrite(l_led2,HIGH);
      }
      
      void c_Off()
      {
       Serial.println("Living room ligh2 OFF");
       digitalWrite(l_led2,LOW);
      }
      
      void fan_On()
      {
       Serial.println("Bedroom Fan ON");
       digitalWrite(fan,HIGH);
      }
      
      void fan_Off()
      {
       Serial.println("Bedroom Fan OFF");
       digitalWrite(fan,LOW);
      }
      
      void b_On()
      {
       Serial.println("Bedroom light ON");
       digitalWrite(b_led,HIGH);
      }
      
      void b_Off()
      {
       Serial.println("Bedroom light OFF");
       digitalWrite(b_led,LOW);
      }
      
      void k_On()
      {
       Serial.println("Kitchen light ON");
       digitalWrite(k_led,HIGH);
      }
      
      void k_Off()
      {
       Serial.println("Kitchen light OFF");
       digitalWrite(k_led,LOW);
      }
      
      void g_On()
      {
       Serial.println("Garden light ON");
       digitalWrite(g_led,HIGH);
      }
      
      void g_Off()
      {
       Serial.println("Garden light OFF");
       digitalWrite(g_led,LOW);
      }
      
      void all_Off()
      {
       Serial.println("All OFF");
       digitalWrite(l_led1,LOW);
       digitalWrite(l_led2,LOW);
       digitalWrite(fan,LOW);
       digitalWrite(b_led,LOW);
       digitalWrite(k_led,LOW);
       digitalWrite(g_led,LOW);
      }

      Part three- Sensing the current Temperature and Humidity

      • Connect the DHT11 Temperature and Humidity Sensor to NodeMCU ESP8266 Wifi Module.
      • DHT11 Temperature and Humidity Sensor sense the current temperature and humidity data and simultaneously, these data are transferred to the Thingspeak cloud platform and the graph is generated by providing the api key.                                                                                                                                                                                                                               Some of the data graph generated:
        Temperature and Humidity data generated on Thingspeak

        Code is given below:

      • #include <ThingSpeak.h>
        #include <DHT.h> 
        #include <ESP8266WiFi.h> 
        #include <WiFiClient.h> 
         
         #define DHTPIN D4 
         #define DHTTYPE DHT11 
         DHT dht(DHTPIN, DHTTYPE); 
         
         const char* ssid = "thinkTANKER"; 
         const char* password = "[email protected]"; 
         WiFiClient client; 
         
         unsigned long myChannelNumber = 265399; 
         const char * myWriteAPIKey = "G0WX6RMTU1EMPJUG"; 
         uint8_t temperature, humidity, fahrenheit; 
         
         void setup() 
         { 
         Serial.begin(115200); 
         dht.begin(); 
         WiFi.mode(WIFI_STA);
         delay(10); 
         // Connect to WiFi network 
         Serial.println(); 
         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.println(""); 
         Serial.println("WiFi connected"); 
         // Print the IP address 
         Serial.println(WiFi.localIP()); 
         ThingSpeak.begin(client); 
         } 
         void loop() 
         { 
         static boolean data_state = false; 
         temperature = dht.readTemperature(); 
         humidity = dht.readHumidity();
         fahrenheit = dht.readTemperature(true);
        
         Serial.println(""); 
         
         Serial.print("Temperature Value is :"); 
         Serial.print(temperature); 
         Serial.println(" C"); 
        
         Serial.print("Fahrenheit Value is :"); 
         Serial.print(fahrenheit); 
         Serial.println(" F"); 
        
         Serial.print("Humidity Value is :"); 
         Serial.print(humidity); 
         Serial.println(" %"); 
         
         Serial.println("");
         
         // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different 
         // pieces of information in a channel. Here, we write to field 1. 
         if( data_state ) 
         { 
         ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey); 
         data_state = false;
         delay(2000); 
         }
         else if(data_state) 
         { 
         ThingSpeak.writeField(myChannelNumber, 2, fahrenheit, myWriteAPIKey); 
         data_state = false;
         delay(4000); 
         } 
         else 
         { 
         ThingSpeak.writeField(myChannelNumber, 3, humidity, myWriteAPIKey); 
         data_state = true;
         delay(6000); 
         } 
         delay(3000); // ThingSpeak will only accept updates every 15 seconds. 
         }

        Youtube link: