Sunday, August 11, 2013

PROJECT 3. HOME AUTOMATION

                          
DESIGN


            Home Automation system consists of a microcontroller circuit to control 5 devices to ON/OFF through its ports. PIC 16F877A is used with portB connected to devices by each of its bits. Once power ON the system, then controller checks for all device status and saves in memory. System waits for an SMS from the user for a command.
            User can message in a specified format to command the system. First user sending command to replay that the current status of the devices. The controller checks the status of the devices and save to memory then sends to the user. User can resend SMS in specified a format to change the status of devices.

/////////////////////////////////////            HOME AUTOMATION       ////////////////////////////////////////////
#include <16F877A.h>
#device adc=8
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES WRT_50%                  //Lower half of Program Memory is Write Protected
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C4,rcv=PIN_C5,bits=8,stream=gsm)
int i,j,f=0,flag1=0;
char str[7], str1[6], str2[6] = ”STATUS”, a, x;
const char m0[4]  = "AT\r";
const char m1[11] = "AT+CMGF=1\r";
const char m2[22] = "AT+CMGS=\"9496084923\"\r";
const char m3[11] = "AT+CMGR=1\r";
const char m4[11] = "AT+CMGD=1\r";
void receive();
void msg();
void check();
short timeout_error;
char timed_getc()
 {
   long timeout;
   timeout_error = FALSE;
   timeout = 0;
   while(!kbhit()&&(++timeout<5000))
   delay_us(10);
   if(kbhit(gsm))
   return(fgetc(gsm));
   else
    {
     timeout_error = TRUE;
     return(0);
    }
 }
     check()
            {
            if(!input(pin_B0))
                str[0]=0x00;
            else
                str[0]=0x01;
if(!input(pin_B1))
                str[1]=0x00;
else
                str[1]=0x01;
if(!input(pin_B2))
                str[2]=0x00;
else
                str[2]=0x01;
if(!input(pin_B3))
                str[3]=0x00;
else
                str[3]=0x01;
if(!input(pin_B4))
                str[4]=0x00;
else
                str[4]=0x01;
                str[5]=’\0’;
}
       change()
            {
if(str[1] == 0)
    output_low(PIN_C1);
else
    output_high(PIN_C1);
if(str[2] == 0)
    output_low(PIN_C2);
else
    output_high(PIN_C2);
if(str[3] == 0)
    output_low(PIN_C3);
else
    output_high(PIN_C3);
if(str[4] == 0)
    output_low(PIN_C4);
else
    output_high(PIN_C4);
if(str[5] == 0)
    output_low(PIN_C5);
else
    output_high(PIN_C5);
}



/////////////////////////////////////////////////////////////msg sending/////////////////////////////////////////////////////
void msg()   
  {
  for(i=0;i<3;i++)
   {
   fputc(m0[i],gsm);
   a=timed_getc();
   }
   a=timed_getc();
   a=timed_getc();    

for(i=0;i<10;i++)
   {
   fputc(m1[i],gsm);
   a=timed_getc();
   }
   a=timed_getc();
   a=timed_getc();
for(i=0;i<21;i++)
   {
   fputc(m2[i],gsm);
   a=timed_getc();
   }
   a=timed_getc();
   a=timed_getc();
for(i=0;str[i]!='\0';i++)
   {
   fputc(str[i],gsm);
   a=timed_getc();
   }
   a=timed_getc();
   a=timed_getc();
   delay_ms(100);
   fputc(0X1A,gsm);
   a=timed_getc();
   output_bit(PIN_a3,1);  
   delay_ms(100);
   output_bit(PIN_a3,0);
 }
//////////////////////////////////////////////////////////receive/////////////////////////////////////////////////////////
void receive()
   {
   for(i=0;i<3;i++)
   {
   fputc(m0[i],gsm);
   a=timed_getc();
   }
   a=timed_getc();
   a=timed_getc();  
for(i=0;i<10;i++)
   {
   fputc(m1[i],gsm);
   a=timed_getc();
   }
   a=timed_getc();
   a=timed_getc();
output_bit(PIN_a4,1);  
delay_ms(300);
output_bit(PIN_a4,0);
for(i=0;i<10;i++)
   {
   fputc(m3[i],gsm);
   a=timed_getc();
   }
   a=timed_getc();
   a=timed_getc();   
while(a!='*')
   {
   a=timed_getc();  
   }    
for(i=0;str1[i-1]!='*';i++)
   {
   a=fgetc(gsm);
   str1[i]=a;
   }
str1[i-1] = '\0';
a=timed_getc();
a=timed_getc(); 
for(i=0;i<10;i++)
   {
   fputc(m4[i],gsm);                         //delete memory of modem
   a=timed_getc();
   }
   a=timed_getc();
   a=timed_getc();
output_bit(PIN_a5,1);  
delay_ms(100);
output_bit(PIN_a5,0);  
   }
   void main()
  {
       while(1)
        {
        delay_ms(300);
        receive();
if(str[0] == ’C’)
     change();
        if(strcmp(str1,str2) == 0)
     check();
      msg();
             reset_cpu();
        }
 }
------------------
 

1 comment:

  1. Great article, it was very helpful! I just started in this and I'm getting to know it better! Cheers, keep up the good work!

    PIC Grant

    ReplyDelete