Home Automation using RF

Description

      Home automation is one of in demand concepts in today’s world. Home automation reduces the physical efforts and integrates the control for any number of appliances in to a single control unit. Hence, a simple home automation system is a remote control of different electrical appliances i.e. turning them on or off with the help of a remote.In this project, a simple home automation system using RF Module is designed. Okey,let's make it.
      
Components Required

 Transmitter 

 1. Arduino UNO  * 1
 2. 434 MHz RF Transmitter  * 1
 3. Push Buttons  * 4
 4. 1 KΩ Resistor * 4
 5. Bread Board * 1
 6. 9V Battery * 1
 7. Jumper wires  * 1

 Receiver

 1. Arduino UNO  * 1
 2. 434 MHz RF Receiver  * 1
 3. 2N2222 NPN Transistor  * 2
 4. 1 KΩ Resistor * 2
 5. 1N4007 PN Junction Diode  * 2
 6. 12V Relay  * 2
 7. Bread Board * 1
 8. Power Supply (Adapter)   * 1
 9. Jumper wires  * 1

Circuit Diagram




Program

 Transmitter

#include <VirtualWire.h> 
const int RF_TX_PIN = 8;
const int load1_ON = 6;
const int load1_OFF = 5;
const int load2_ON = 4;
const int load2_OFF = 3;

char msg[6] = "";
int load1ON = 0;
int load1OFF = 0;
int load2ON = 0;
int load2OFF = 0;

void setup()
{
  vw_set_ptt_inverted(true);
  vw_set_tx_pin(RF_TX_PIN);
  vw_setup(2000);
  
  pinMode(load1_ON, INPUT);
  pinMode(load1_OFF, INPUT);
  pinMode(load2_ON, INPUT);
  pinMode(load2_OFF, INPUT);
}

void loop()
{
  load1ON = digitalRead(load1_ON);
  load1OFF = digitalRead(load1_OFF);
  load2ON = digitalRead(load2_ON);
  load2OFF = digitalRead(load2_OFF);
  
  if (load1ON == HIGH)
  {    
    strcpy(msg, "@ABC$");    
  }
  
  if (load1OFF == HIGH)
  {    
    strcpy(msg, "@BOFF$");    
  }
  
  if (load2ON == HIGH)
  {
    strcpy(msg, "@FON$");    
  }
  
  if (load2OFF == HIGH)
  {
    strcpy(msg, "@FOFF$");    
  }
  
  if(strlen(msg)>0)
  {    
    sendMsg(msg);    
  }
  
  strcpy(msg, "");  
  resetLoads();
}

void sendMsg(char msg[])
{  
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx();
  delay(400);
}


void resetLoads()
{
  load1ON = 0;
  load1OFF = 0;
  load2ON = 0;
  load2OFF = 0;
}

   Receiver

#include <VirtualWire.h> 
const int RF_RX_PIN = 11;
const int load1 = 4;
const int load2 = 5; 
const int errorLED = 13;

boolean bload1 = false;
boolean bload2 = false;

void setup()
{
  vw_set_ptt_inverted(true);
  vw_set_rx_pin(RF_RX_PIN);
  vw_setup(2000);
  vw_rx_start();
  
  pinMode(errorLED, OUTPUT);
  pinMode(load1, OUTPUT);
  pinMode(load2, OUTPUT);
  bload1 = false;
  bload2 = false;
}
 
void loop()

  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;  
  if(vw_get_message(buf, &buflen))
  { 
    delay(500); 
    messageDecode ( (char *) buf);
    delay(500); 
  } 
  
  if(bload1 == true)
  {
    digitalWrite(load1, HIGH);
  }

  if(bload1 == false)
  {
    digitalWrite(load1, LOW);
  }
  
  if(bload2 == true)
  {
    digitalWrite(load2, HIGH);
  }
  
  if(bload2 == false)
  {
    digitalWrite(load2, LOW);
  }
}

void messageDecode(char msg[])
{
  if (strlen(msg) ==0) 
  { 
    ErrorLED(); 
    return; 
  }  
  
  
 if ( compareMessage(msg, '@') == false || compareMessage(msg, '$') == false) 
 {
  ErrorLED(); 
  return;
 }  
 

 char cTag[5] ="";
 int index =0;
 int iLoop = IndexOf(msg,'@');
 
 if(iLoop == -1)
 { 
  ErrorLED(); 
  return;
 }
 
 iLoop++;
 
 while (iLoop < strlen(msg))
 {
   if(msg[iLoop] == '$')
   {
    break;
   }
   else
   {
    cTag[index] = msg[iLoop]; 
    index++;
   }
   
   iLoop ++;
 }
 
 String sTag = String(cTag);

  if (sTag.equals("ABC")) 
  { 
    bload1 = true; 
  }

  if (sTag.equals("BOFF")) 
  { 
    bload1 = false;
  }

  if (sTag.equals("FON")) 
  { 
    bload2 = true; 
  }

  if (sTag.equals("FOFF")) 
  { 
    bload2 = false; 
  } 
  
}

int IndexOf(char msg[], char tag)
{
  if (strlen(msg) ==0) return false;
  boolean bFlag = false;
  int iIndex = -1;
  
  for( int i=0; i<strlen(msg); i++)
  {
    if(msg[i]== tag)
    iIndex = i;
  }
  
  return iIndex;
}

boolean compareMessage (char msg[], char tag)
{
  if (strlen(msg) ==0) return false;
  boolean bFlag = false;
  
  for( int i=0; i<strlen(msg); i++)
  {
    if(msg[i]== tag)
    bFlag = true;
  }
  
  return bFlag;
}

void ErrorLED()
{
  digitalWrite(errorLED, HIGH);
  delay(500);
  digitalWrite(errorLED, LOW);
}