Saturday, June 6, 2015

GSM Based Home Automation

GSM based home automation, project allows you to control electrical appliances using your mobile phone SMS. It consists of ATmega8 microcontroller, SIM300 GSM modem, Relays.


There are many Home Automation Systems available in our market. Most of these are simple home appliances controlling systems like DTMF controlled home Appliances, IR Remote based home appliances controlling, RF based home appliances controlling. we are going to discuss about GSM based Home/Office Appliances Control (Home Automation System).
Here all circuit details with PCB design, Code are provided.


SMS based home automation

Features:
1. Control your home electrical appliances
2. Send Simple "(FNNN)" SMS from your phone to control your devices.
3.  You can get current status by sending "(S)" message to it.
4. Uses Standard GSM Modem. 
5. Low Cost.
Step 1: Components Required
1. Atmega 8 Microcontroller
2. Relays
3. ULN2003
4. LM7805
5. BC548
6. 1K Ohm Resistors
7. GSM Modem (SIM300 or SIM900)
8. Buzzer


Step 2: Circuit Design and PCB Manufacturing
Download Requires Files
1. Download pdf Complete Circuit Diagram
GSM Based home automation circuit diagram
2. Download pdf PCB Layout
GSM Based home automation PCB Layout
4. Download pdf Component Placement Diagram
5. Assemble components using above files.
Step 3: Programming the controller
Download Hex File

AVR Studio C Code

#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h>
//Relay Connection
#define D0   PC1
#define D1   PC2
#define D2   PC3
#define D3   PC4

//Switches Connection
#define S1  PB1
#define S2  PB0

#define S3  PB2
#define S4  PD7

#define S5  PB3
#define S6  PD6

#define S7  PB4
#define S8  PD5

//Declaration
unsigned char u8_data;
void delay(unsigned int de);
void USART_Transmit(char data );
void senddata(char string[16]);
void USART_Init();
void USART_Receive();
void change();
void TriggerDelay(unsigned char i);
void DeleteAllSMS();
void Command(unsigned char y);
void ProcessFifo();
void SendSMS();
void ReadSMS(char SMSnumber);

//Serial Data packet
//Send-sw1,sw2,sw3,sw4
//Recive-sw1,sw2,sw3,sw4
char a,b,c,d,status,SendStatus,counter,SMSrecived,SMSnumber;
unsigned char Fifo[150],RollOut,FifoCnt,FiCnt,Value1[16],Mob[20],i;
/*********************************************************************************/
/*         Main Routine                                 */
/*********************************************************************************/
int main(void)
{
 delay(100);
 DDRB = 0b00011111; 
 PORTB = 0b00011111; //Enable internal pullups for switches
 DDRD = 0b11100010;  //Enable internal pullups for switches
 PORTD = 0b11100000;
 DDRC = 0b00011110; //Relay port directions 

 status=0x00;
 SendStatus=0;

 FifoCnt=0;
 FiCnt=0;
 RollOut=0x00;

 SMSrecived=0;

 counter=0;

 //Enable Interrupt
 GICR=0x40; 
 MCUCR=0x03;

 USART_Init();
 SREG=0x80;
 delay(100);


 USART_Transmit(0x0D);
 USART_Transmit(0x0A);

 senddata("ATE0;&W");  //Echo off
 USART_Transmit(0x0D);
 USART_Transmit(0x0A);

 while(1)
 {
  ProcessFifo();
 }

}

/*********************************************************************************/
/*         Delay of 1mSec                               */
/*********************************************************************************/
void delay(unsigned int de)
{
unsigned int rr,rr1;
   for (rr=0;rr<de;rr++)
   {
   
  for(rr1=0;rr1<30;rr1++)   //395
  {
   asm("nop");
  }
   
   }
}

/*********************************************************************************/
/*         Send data to USART                           */
/*********************************************************************************/
void USART_Transmit(char data )
{
 UDR = data;
 /* Wait for empty transmit buffer */
 while ( !( UCSRA & (1<<UDRE)) )
 ;
 /* Put data into buffer, sends the data */
 
}

/*********************************************************************************/
/*         Send Data Serially                           */
/*********************************************************************************/
void senddata(char string[16])
{
  int len,count;
  len = strlen(string);

   for (count=0;count<len;count++)
  {
    USART_Transmit(string[count]);
 }
}

/*********************************************************************************/
/*         RS232 Intitialize                            */
/*********************************************************************************/
void USART_Init()
{
/* Set baud rate */
 UBRRH = 0x00;  //9600 @ 8MHz
 UBRRL =103; 
//Set double speed
  UCSRA |= (1<<U2X);
/* Enable receiver and transmitter */
 UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
// UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
//Set interrupt on RX
  UCSRB |= (1<<RXCIE);
}

/*********************************************************************************/
/*         USART Receive                                */
/*********************************************************************************/

void USART_Receive()
{
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
/* Get and return received data from buffer */
u8_data=UDR;
} 


/*********************************************************************************/
/*         RS232 Interrupt                              */
/*********************************************************************************/
SIGNAL(USART_RXC_vect)
{
 u8_data=UDR;
 Fifo[FifoCnt]=u8_data;
 FifoCnt++;
 if(FifoCnt==150)
 {FifoCnt=0;
  RollOut=0x05;}
 return; 
}


/*********************************************************************************/
/*         Delete all SMS from SIM                      */
/*********************************************************************************/
void DeleteAllSMS()
{
 char i;
  for(i=1;i<3;i++)
  {
   delay(200);
   if(SMSrecived==0)
   {
    senddata("AT+CMGD=");
    USART_Transmit(i+0x30);
    USART_Transmit(0x0D);
    USART_Transmit(0x0A); 
   }
  }
}
/*********************************************************************************/
/*         Process Cercular Serial Buffer               */
/*********************************************************************************/
void ProcessFifo()
{
// unsigned char i;
//Use circular buffer with fill zero after read to prevent recommand

 if((FifoCnt>FiCnt) || (RollOut==0x05))
 {
   Command(Fifo[FiCnt]); 

     Fifo[FiCnt]=0x00;     

     FiCnt++;
     if(FiCnt==150)
     {FiCnt=0;
      RollOut=0x00;}
 }
 else
 {

  delay(100);
//Switch 1============================================
   if((PINB & 0x02) == 0x02)
   {
    PORTC |= (1<<D0);
    a=1;
    status=status | 0x10;
   }
   if((PINB & 0x01) == 0x01)
   {
    PORTC &=~ (1<<D0);
    a=0;
    status=status & ~(0x10);
   }
   delay(10);
//Switch 2============================================
   if((PINB & 0x04) == 0x04)
   {
    PORTC |= (1<<D1);
    b=1;
    status=status | 0x20;
   }
   if((PIND & 0x80) == 0x80)
   {
    PORTC &=~ (1<<D1);
    b=0;
    status=status & ~(0x20);
   }   
   delay(10);  
//Switch 3============================================
   if((PINB & 0x08) == 0x08)
   {
    PORTC |= (1<<D2);
    c=1;
    status=status | 0x40;
   }
   if((PIND & 0x40) == 0x40)
   {
    PORTC &=~ (1<<D2);    
    c=0;
    status=status & ~(0x40);
   }
   delay(10);
//Switch 4============================================ 
   if((PINB & 0x10) == 0x10)
   {
    PORTC |= (1<<D3);
    d=1;
    status=status | 0x80;
   }
   if((PIND & 0x20) == 0x20)
   {
    PORTC &=~ (1<<D3);
    d=0;
    status=status & ~(0x80);
   }
   delay(10);

 }
}

/*********************************************************************************/
/*         Command Decoder                           */
/*********************************************************************************/
void Command(unsigned char y)
{
 Value1[16]=Value1[15];
 Value1[15]=Value1[14];
 Value1[14]=Value1[13];
 Value1[13]=Value1[12];
 Value1[12]=Value1[11];

 Value1[11]=Value1[10];
 Value1[10]=Value1[9];
 Value1[9]=Value1[8];
 Value1[8]=Value1[7];
 Value1[7]=Value1[6];
 Value1[6]=Value1[5];
 Value1[5]=Value1[4];
 Value1[4]=Value1[3];
 Value1[3]=Value1[2];
 Value1[2]=Value1[1];
 Value1[1]=Value1[0];
 Value1[0]=y;

//===============SMS Decoder========================
//0D 0A 2B 43 4D 54 49 3A 20 22 53 4D 22 2C 32 0D 0A

//SMS Recived
 if(Value1[0]==0x0A && Value1[1]==0x0D && Value1[5]==0x4D && Value1[6]==0x53)
 {
  SMSnumber=Value1[2];
  ReadSMS(SMSnumber);
 }

//Validate Number
 if((Value1[0]==0x22) && (Value1[14]==0x22))
 {

  Mob[1]=Value1[13];
  Mob[2]=Value1[12];
  Mob[3]=Value1[11];
  Mob[4]=Value1[10];
  Mob[5]=Value1[9];
  Mob[6]=Value1[8];
  Mob[7]=Value1[7];
  Mob[8]=Value1[6];
  Mob[9]=Value1[5];
  Mob[10]=Value1[4];
  Mob[11]=Value1[3];
  Mob[12]=Value1[2];
  Mob[13]=Value1[1];
 } 
 
//Set Control
 if(Value1[0]==0x29 && Value1[5]==0x28)
 {
  //( N N F N )
  if(Value1[1]==0x4E)
  {a=1;}
  else
  {a=0;}

  if(Value1[2]==0x4E)
  {b=1;}
  else
  {b=0;}

  if(Value1[3]==0x4E)
  {c=1;}
  else
  {c=0;}

  if(Value1[4]==0x4E)
  {d=1;}
  else
  {d=0;}

  PORTC &=~(0b00011110);
  PORTC |= (a<<D0);
  PORTC |= (b<<D1);
  PORTC |= (c<<D2);
  PORTC |= (d<<D3);

  delay(100);
  senddata("AT+CMGD=");
  USART_Transmit(0x31); //SMS number
  USART_Transmit(0x0D);
  USART_Transmit(0x0A);

 for(i=1;i<=(SMSnumber-0x30);i++)
 {
  delay(200);
  senddata("AT+CMGD=");
  USART_Transmit(i+0x30); //SMS number
  USART_Transmit(0x0D);
  USART_Transmit(0x0A);
 }


 }

//Get Status (G)
 if(Value1[0]==0x29 && Value1[2]==0x28)
 {
  SendSMS();
  delay(100);
  senddata("AT+CMGD=");
  USART_Transmit(0x31); //SMS number
  USART_Transmit(0x0D);
  USART_Transmit(0x0A);
  for(i=1;i<=(SMSnumber-0x30);i++)
  {
   delay(100);
   senddata("AT+CMGD=");
   USART_Transmit(i+0x30); //SMS number
   USART_Transmit(0x0D);
   USART_Transmit(0x0A);
  }

 }
}


void SendSMS()
{
     USART_Transmit(13);
     USART_Transmit(10);
     senddata("AT+CMGD=1");
     USART_Transmit(13);
     USART_Transmit(10);
     delay(100);
          
     senddata("AT+CMGF=1");
     USART_Transmit(13);
     USART_Transmit(10);
     delay(100);
     
     senddata("AT+CMGW=");
     USART_Transmit(34);

     USART_Transmit(Mob[1]);
     USART_Transmit(Mob[2]);
     USART_Transmit(Mob[3]);
     USART_Transmit(Mob[4]);
     USART_Transmit(Mob[5]);
     USART_Transmit(Mob[6]);
     USART_Transmit(Mob[7]);
     USART_Transmit(Mob[8]);
     USART_Transmit(Mob[9]);
     USART_Transmit(Mob[10]);
     USART_Transmit(Mob[11]);
     USART_Transmit(Mob[12]);
     USART_Transmit(Mob[13]);

     USART_Transmit(34);
     USART_Transmit(13);
     USART_Transmit(10);
     delay(100);
     
     senddata("Device 1:");
     if((PORTC & 0x10) == 0x10)
     {senddata("ON");}
     else
     {senddata("OFF");}
     USART_Transmit(13);
     USART_Transmit(10);

     senddata("Device 2:");
     if((PORTC & 0x08) == 0x08)
     {senddata("ON");}
     else
     {senddata("OFF");}
     USART_Transmit(13);
     USART_Transmit(10);

     senddata("Device 3:");
     if((PORTC & 0x04) == 0x04)
     {senddata("ON");}
     else
     {senddata("OFF");}
     USART_Transmit(13);
     USART_Transmit(10);
     
     senddata("Device 4:");
     if((PORTC & 0x02) == 0x02)
     {senddata("ON");}
     else
     {senddata("OFF");}
     USART_Transmit(13);
     USART_Transmit(10);

     USART_Transmit(13);
     USART_Transmit(10);
     senddata("----------");

     USART_Transmit(13);
     USART_Transmit(10);


     delay(50);
     USART_Transmit(26); //Cntrl+Z
     delay(300);
     delay(300);

     senddata("AT+CMSS=1");
     USART_Transmit(13);
     USART_Transmit(10);
     delay(500);
}

//===================================================================================================
//                Read SMS
//===================================================================================================
void ReadSMS(char SMSnumber)
{
 delay(100);
 senddata("AT+CMGR=");
 USART_Transmit(SMSnumber); //SMS number
 senddata(",1");      //No change in SMS status
 USART_Transmit(0x0D);
 USART_Transmit(0x0A);
}


Step 4: Test the code and Hardware
1. Send SMS "(FNNF)" to circuit, you should observe the relay operation.
2. Send SMS "(G)" and you should get status reply.
3. Follow us on Google+
4. You Did it Yourself
5. Refer Tutorials from this site for more understanding of code and Circuits


1 comment: