Siddhartha mudgal
Published © GPL3+

Controlling Appliances Using Android Device and Arduino

This project is simple representation of home automation and can be installed with conventional switchboards too.

IntermediateFull instructions provided6,046
Controlling Appliances Using Android Device and Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
IR Transceiver (Generic)
use transceiver 1 for entry side and transceiver 2 for exit side of the room
×2
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
RGB Backlight LCD - 16x2
Adafruit RGB Backlight LCD - 16x2
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Bluetooth Terminal for Arduino
Use this app for code (without panel)
Bluetooth Electronics
Use this app for the code(to control with panel)

Story

Read more

Schematics

Schematics

Code

Code

Arduino
Use this code if you want to control only via terminal emulator app.
Upload this code on the android device.
Download any terminal emulator app from play store.
Now open the app and pair it with the hc 05.
Send commands as explained in the code.
#include <LiquidCrystal.h>
#include <SimpleDHT.h>
#include <Streaming.h>

int pinDHT11 = 6;                                                /* this pin is assigned to DHT sensor's output. The data from DHT is read from this pin of arduino*/
SimpleDHT11 dht11;                                               

const int rs = 10, en = 9, d4 = 5, d5 = 4, d6 = 3, d7 = 7;       /* these pins are assigned to the LCD(16x2), used to display temprature, humidity */   
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define in 14                                                    /* This pin is assigned to first proximity sensor's data, it actuates when a person enteres in the room*/ 
#define out 19                                                   /* This pin is assigned to second proximity sensor's data, it actuates when a person leaves the room*/  

#define relay1 2                                                /* this pin actuates when count value becomes greater than 1
                                                                   the count increases by 1 as the first proximity sensor actuates  
                                                                   the count decreaes by 1 as the second proximity sensor actuates*/                                                                     
int LED1 = 11;                                                 /* this pin actuates as the command ON1 and turns off when the command OFF1 is given through command line*/ 
int LED2 = 12;                                                 /* this pin actuates as the command ON2 and turns off when the command OFF2 is given through command line*/
int LED3 = 13;                                                 /* this pin actuates as the command ON3 and turns off when the command OFF3 is given through command line*/
String Data;                                                   /* All the pin actuates relay on, when command ONALL is given through command line*/ 
                                                               /* All the pin turns off relay, when command OFFALL is given through command line*/ 
int count = 0;                                                 /*This will increase as proximity sensor 1 actuates and decrements when proximity sesnsor 2 actuuates*/      

void IN()                                                      /*this function senses the actuation of proximity sensor 1*/
{                                                          
  count++;
  Serial.println("Person In ROOM\n");
  delay(1000);
}

void OUT()                                                     /*this function senses the actuation of proximity sensor 2*/
{
  count--;
  Serial.println("Person Leaves ROOM\n");
  if (count < 0)
  {
    count = 0;
  }
  
  delay(1000);
}

void irsensor() {                                                     // this function generates signal for the above two functions, this recieves the value of count, count here 
                                                                      // represents number of persons,as a person enters, proximity 1 will become high and increases count by 1 and if a   
  if ((digitalRead(in) || digitalRead(out)))                          // person leaves the room proximity 2 will become high and decrements the count by 1.
  {                                                                   // Now if count becomes more than one after zero, then the relay pin 2 output remains high and a single light will automatically
    if (digitalRead(in))                                              // turns on and if count becomes zero then all the appliances connected to relay gets off automatically.                                            
      IN();
    if (digitalRead(out))
      OUT();

    if (count > 0)
    {
      digitalWrite(relay1, LOW);
      delay(200);
    }
    else
    {
      digitalWrite(relay1, HIGH);
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      delay(200);
    }
    
  }

}

void btcontrol() {                                               // this is the function used for bluetooth communication with the arduino board. 
  if (Serial.available())                                        // this will recieves and send data to arduino to control the appliances connected to the relay
  { //wait for data receiverj                                    // by the help of terminal emulator 
    Data = Serial.readString();
    Serial.print(Data);
    if (Data == "ON1") {                                         //Send ON1 from terminal emulator to turn the appliance on connected to pin 3 of relay  
      digitalWrite(LED1, LOW);
      Serial.println("LED1\n");
      Serial.println("LED1 On!\n");
    }
    else if (Data == "OFF1") {                                   //Send OFF1 from terminal emulator to turn the appliance off connected to pin 3 of relay 
      digitalWrite(LED1, HIGH);
      Serial.println("LED1 Off!\n");
      Serial.println("LED1 Off !\n");
    }
    if (Data == "ON2") {                                         //Send ON2 from terminal emulator to turn the appliance on connected to pin 4 of relay                
      digitalWrite(LED2, LOW);
      Serial.println("LED2 On\n");
      Serial.println("LED2 On!\n");
    }
    else if (Data == "OFF2") {                                   //Send OFF2 from terminal emulator to turn the appliance off connected to pin 4 of relay  
      digitalWrite(LED2, HIGH);
      Serial.println("LED2 Off\n");
      Serial.println("LED2 Off !\n");
    }
    if (Data == "ON3") {                                         //Send ON5 from terminal emulator to turn the appliance on connected to pin 5 of relay    
      digitalWrite(LED3, LOW);
      Serial.println("LED3 On\n");
      Serial.println("LED3 On!\n");
    }
    else if (Data == "OFF3") {                                   //Send OFF5 from terminal emulator to turn the appliance off connected to pin 5 of relay      
      digitalWrite(LED3, HIGH);
      Serial.println("LED3 Off!\n");
      Serial.println("LED3 Off !\n");
    }
    if (Data == "ON4") {                                        //Send ON4 from terminal emulator to turn the appliance on connected to pin 2(i.e also actuated from IR sensor)of relay. 
      digitalWrite(relay1, LOW);
      Serial.println("relay1 On\n");
      Serial.println("relay1 On!\n");
    }
    else if (Data == "OFF4") {                                  //Send OFF4 from terminal emulator to turn the appliance off connected to pin 2 of relay 
      digitalWrite(relay1, HIGH);
      Serial.println("relay1 Off!\n");
      Serial.println("relay1 Off !\n");
    }
    if (Data == "ONALL") {                                      //Send ONALL from terminal emulator to turn all the appliances on which are connected to relay   
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      digitalWrite(relay1, LOW);
      Serial.println("LEDs ON\n");
      Serial.println("LEDs On!\n");
    }
    else if (Data == "OFFALL") {                                    //Send OFFALL from terminal emulator to turn all the appliances off which are connected to relay        
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(relay1, HIGH);
      Serial.println("LEDs Off!\n");
      Serial.println("LEDs OFF  !\n");
    }
  }
 
}

void temprature() {                                                           // This function is for DHT sensor which reads and writes the value of temperature and humidity  
  
  if (Data=="TEMP"){                                                         // Send Temp from terminal emulator to get the current temprature and humidity   
  
  Serial.println("=================================");
  Serial.println("DHT11 readings...");
  delay(2000);
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  
  if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("No reading , err="); Serial.println(err); delay(1000);
    return;
  }
  Serial.print("temp: ");
  Serial.print((int)temperature); Serial.print(" Celcius, ");
  Serial.print("humidity:");
  Serial.print((int)humidity); Serial.println(" %");
    
  lcd.clear();                                                   // this will print the data on the LCD display

  lcd.setCursor(0, 0);
  
  lcd.print("Temp: ");
  
  lcd.print((int)temperature);
  
  lcd.setCursor(0, 1);
  
  lcd.print("Humidity(%): ");
  
  lcd.print((int)humidity);
  delay(750);
  
  }
}

void setup()                                                                   // This is the function used to initiate the program
{
  Serial.begin(9600);

  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(relay1, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  Serial.println("Waiting for command...");
  Serial.println("Send ON'X' to turn on the LED. Send OFF'X' to turn Off");

  digitalWrite(relay1, HIGH);
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(pinDHT11,HIGH);
  
  lcd.begin(16, 2);

}

void loop()
{
  if ((digitalRead(in) || digitalRead(out)))
  {
    irsensor();
  }
  if (Serial.available())
  {
    btcontrol();
  }
  temprature();
  
}

Code(to control with panel panel)

Arduino
This code is used with the panel file, which is a .kwl file.
Upload this code in the arduino.
Then on the android device download the following software:-
https://play.google.com/store/apps/details?id=com.keuwl.arduinobluetooth&hl=en
Now open the bluetooth electronics app
Now download and paste the Mini pronect (1).kwl file in the keuwlsoft folder in the file manager.
Now go back to the app and click on save icon, on the top left of the app.
Then click on Load Panels.
Then upload this file into the software as panel as follows
Now Click on connect and pair yor android device with the HC 05
The uploaded panel is now showing in the list, click on it to open.
#include <LiquidCrystal.h>
#include <SimpleDHT.h>
#include <Streaming.h>
int pinDHT11 = 6;

SimpleDHT11 dht11;
const int rs = 10, en = 9, d4 = 5, d5 = 4, d6 = 3, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define in 14
#define out 19
#define relay1 2
int LED1 = 11; // the on-board LED
int LED2 = 12;
int LED3 = 13;
String Data; // the data received
int count = 0;

void IN()
{
  count++;
  Serial.println("Person In ROOM\n");
  delay(1000);
}

void OUT()
{
  count--;
  Serial.println("Person Leaves ROOM\n");
  if (count < 0)
   {
     count = 0;
   }
  
  delay(1000);
}

void irsensor() {

  if ((digitalRead(in) || digitalRead(out)))
  {
    if (digitalRead(in))
      IN();
    if (digitalRead(out))
      OUT();

    if (count > 0)
    {
      digitalWrite(relay1, LOW);
      delay(200);
    }
    else
    {
      digitalWrite(relay1, HIGH);
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
    }
    Serial.print(count); Serial <<"*P" << (count) << "*" <<endl;
    Serial.println("\n");
  }
}
void btcontrol() {
  if (Serial.available())
  { //wait for data receiverj
    Data = Serial.readString();
    Serial.print(Data);
    if (Data == "ON1"&&(digitalRead(LED1))) {
      digitalWrite(LED1, LOW);
      Serial.println("LED1\n");
      Serial.println("LED1 On!\n");
      }
    else if (Data == "OFF1"&&(!digitalRead(LED1))) {
      digitalWrite(LED1, HIGH);
      Serial.println("LED1 Off!\n");
      Serial.println("LED1 Off !\n");
     }
    if (Data == "ON2"&&(digitalRead(LED2))) {
      digitalWrite(LED2, LOW);
      Serial.println("LED2 On\n");
      Serial.println("LED2 On!\n");
    }
    else if (Data == "OFF2"&&(!digitalRead(LED2))) {
      digitalWrite(LED2, HIGH);
      Serial.println("LED2 Off\n");
      Serial.println("LED2 Off !\n");
     
    }
    if (Data == "ON3"&&(digitalRead(LED3))) {
      digitalWrite(LED3, LOW);
      Serial.println("LED3 On\n");
      Serial.println("LED3 On!\n");
     
    }
    else if (Data == "OFF3"&&(!digitalRead(LED3))) {
      digitalWrite(LED3, HIGH);
      Serial.println("LED3 Off!\n");
      Serial.println("LED3 Off !\n");
     
    }
    if (Data == "ON4"&&(digitalRead(relay1))) {
      digitalWrite(relay1, LOW);
      Serial.println("relay1 On\n");
      Serial.println("relay1 On!\n");
     
    }
    else if (Data == "OFF4"&&(!digitalRead(relay1))) {
      digitalWrite(relay1, HIGH);
      Serial.println("relay1 Off!\n");
      Serial.println("relay1 Off !\n");
     
    }
    if (Data == "ONALL") {
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      digitalWrite(relay1, LOW);
      Serial.println("LEDs ON\n");
      Serial.println("LEDs On!\n");
    }
    else if (Data == "OFFALL") {
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(relay1, HIGH);
      Serial.println("LEDs Off!\n");
      Serial.println("LEDs OFF  !\n");
    }
      
  }
}

void temprature() {
  irsensor();
  if (Data=="TEMP"){
  
  Serial.println("=================================");
  Serial.println("DHT11 readings...");
  delay(2000);
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  
  if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("No reading , err="); Serial.println(err); delay(1000);
    return;
  }
  Serial.print("temp: ");
  Serial.print((int)temperature); Serial.print(" Celcius, ");
  Serial.print("humidity:");
  Serial.print((int)humidity); Serial.println(" %");
  Serial <<"*T" <<(int)temperature << "*" <<endl;
  Serial <<"*H" << ((int)humidity) << "*" <<endl;
    
  lcd.clear();

  lcd.setCursor(0, 0);
  
  lcd.print("Temp: ");
  
  lcd.print((int)temperature);
  
  lcd.setCursor(0, 1);
  
  lcd.print("Humidity(%): ");
  
  lcd.print((int)humidity);
  delay(750);
  
  }
}

void setup()
{
  Serial.begin(9600);

  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(relay1, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  Serial.println("Waiting for command...");
  Serial.println("Send ON'X' to turn on the LED. Send OFF'X' to turn Off");

  digitalWrite(relay1, HIGH);
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(pinDHT11,HIGH);
  
  lcd.begin(16, 2);

}

void loop()
{
  if ((digitalRead(in) || digitalRead(out)))
  {
    irsensor();
  }
  if (Serial.available())
  {
    btcontrol();
  }
  temprature();
   statuS();
  }
void statuS(){
 
if(!digitalRead(LED1))
Serial.print("*A1=ON*");
 if(digitalRead(LED1))
Serial.print("*A1=Off*");  
 if(!digitalRead(LED2))
Serial.print("*B2=ON*");
 if(digitalRead(LED2))
Serial.print("*B2=Off*");  
 if(!digitalRead(LED3))
Serial.print("*C3=ON*");
 if(digitalRead(LED3))
Serial.print("*C3=Off*");  
 if(!digitalRead(relay1))
Serial.print("*D4=ON*");
 if(digitalRead(relay1))
Serial.print("*D4=Off*"); 
 if(!digitalRead(LED1)&&!digitalRead(LED2)&& !digitalRead(LED3)&& !digitalRead(relay1))
Serial.print("*EALL=ON*"); 
 if(digitalRead(LED1)&&digitalRead(LED2)&&digitalRead(LED3)&&digitalRead(relay1))
Serial.print("*EALL=Off*"); 

}

Credits

Siddhartha mudgal

Siddhartha mudgal

0 projects • 3 followers

Comments