/*
   PICGSMOC Demo program using 18F4520
   
   - check and set/reset alarm
   - open garage doors
   - open gate
   
   Written from V5 and modified as example in book
*/

#include <18F4520.h>
#device adc=10
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES XT                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.0V
#FUSES PUT                      //Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors,stream=pcrs)

#include "stdlib.h"
#include <math.h>

//Program all eeprom to zero's during first programming
#rom 0xf00000={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

#priority rda

//IO REDEFINE
#define almi   pin_a1   //Alarm Active input 
#define siri   pin_a0   //Alarm 12v siren
#define hbled  pin_a4   //Heartbeat LED
#define gatr   pin_c3   //reed relay gate remote
#define garr   pin_d0   //reed relay garage remote
#define almr   pin_d1   //reed relay alarm remote
#define modon  pin_d2   //Modem ON/OFF

//Varaiables for SMS
long len;
char chin;
char istr[300];
char tstr[50];
int q;

//User setup
char baassel[15];
char userpass[15];
char atplace[25];
char cellu1[15];
char cellu2[15];
char cellu3[15]; 
char cellu4[15];
char cellu5[15];
char cellu6[15];
char user[30];
char cell[13],sel[13];
char smstxt[160];

char *ptr;
int i;
int16 selacc0,selacc1;
char tmp[45];

#bit cel1a  =  selacc0.0
#bit cel2a  =  selacc0.1
#bit cel3a  =  selacc0.2
#bit cel4a  =  selacc0.3
#bit cel5a  =  selacc0.4
#bit cel6a  =  selacc0.5
#bit cel1p  =  selacc0.6
#bit cel2p  =  selacc0.7
#bit cel3p  =  selacc1.0
#bit cel4p  =  selacc1.1
#bit cel5p  =  selacc1.2
#bit cel6p  =  selacc1.3
#bit atp    =  selacc1.4
#bit baasp  =  selacc1.5
#bit pwa    =  selacc1.6

const char smss[3]="S+";
const char smsr[3]="S-";
char c1sms[10],c2sms[10],c3sms[10];
char c4sms[10],c5sms[10],c6sms[10];

//!!!This is the backdoor hardcoded full access data!!!
const char init_cell[15]="+27825740415";  //MASTER OVERRIDE!!! Change to yours
const char initpass[20]="JANHAM1";        //MASTER PASSWORD!!! Change to yours

//Variable declarations
int flags;       //Bit flags x16

#bit lfcr   =  flags.0     //LfCr for rda interrupt and sms
#bit insms  =  flags.1     //SMS was read by serial interrupt
#bit legal  =  flags.2     //Sms received from legit cellno
#bit initf  =  flags.3     //flag for heartbeat led on in sms
#bit rngok  =  flags.4     //Valid number called system
#bit acta   =  flags.5     //Siren is on thus we have an intrusion

//Write cell no to internal eeprom
void wrcell(int sadr){
   strncpy(smstxt,strstr(istr,tstr),50);
   smstxt[50]='\0';
   ptr=strchr(smstxt,'+');
   for(i=0;i<=11;i++)tmp[i]=ptr[i];
   tmp[12]='\0';
   for(i=0;i<=12;i++)write_eeprom(sadr+i,tmp[i]);        
}

//Read cell no from internal eeprom
void rdcell(int adr){
   int cc;
   for(cc=0;cc<=11;cc++)sel[cc]=read_eeprom(adr+cc);
   sel[12]='\0';
   if(adr==40)strcpy(baassel,sel);
   if(adr==55)strcpy(cellu1,sel);
   if(adr==70)strcpy(cellu2,sel);
   if(adr==85)strcpy(cellu3,sel);
   if(adr==100)strcpy(cellu4,sel);
   if(adr==115)strcpy(cellu5,sel);
   if(adr==130)strcpy(cellu6,sel);   
}

//Write to internal eeprom
void wr_ee(int address,int value){
   write_eeprom(address,value);
   }

//Read internal eeprom
int rd_ee(int address){
   int value;
   value=read_eeprom(address);
   return(value);
   }

//EE write for settings
void eesave(){
   write_eeprom(0,selacc0);
   write_eeprom(1,selacc1);
}

//Setup modem
void modem_init(){
   initf=true;
   delay_ms(100);
   output_high(modon);
   delay_ms(2000);
   output_low(modon);
   delay_ms(30000);
   fprintf(pcrs,"AT\r");
   delay_ms(2000);
   fprintf(pcrs,"AT+IPR=9600\r");
   delay_ms(2000);   
   fprintf(pcrs,"ATZ0\r");
   delay_ms(2000);
   fprintf(pcrs,"AT+CSDH=1\r");
   delay_ms(2000);  
   fprintf(pcrs,"ATS0=10\r");
   delay_ms(2000);
   fprintf(pcrs,"ATE0\r");
   delay_ms(2000);
   fprintf(pcrs,"AT+CMGF=1\r");
   delay_ms(2000);
   fprintf(pcrs,"AT+CSMP=17,167,0,0\r");
   delay_ms(2000);
   fprintf(pcrs,"AT+CNMI=1,2,0,0,0\r");
   delay_ms(2000);
   fprintf(pcrs,"AT+CLIP=1\r");
   delay_ms(2000);   
   fprintf(pcrs,"AT&W\r");
   delay_ms(2000);   
   initf=false;
}

//Clear the buffer
void clr_buf(){
   long i;
   len=0;
   for(i=0;i<=299;i++)istr[i]='\0';
   lfcr=false;
}

//SMS send commands to modem
void sms_now(char *nom, char *txt){
   fprintf(pcrs,"AT+CMGS=\"%s\"\r",nom);
   delay_ms(2000);
   fprintf(pcrs,"%s:\n\r%s\x1A\r",atplace,txt);
   delay_ms(5000);
}

//Change international format to national format
void cmodten(char *nom){
   sel[10]=NULL;
   sel[9]=nom[11];
   sel[8]=nom[10];
   sel[7]=nom[9];
   sel[6]=nom[8];
   sel[5]=nom[7];
   sel[4]=nom[6];
   sel[3]=nom[5];
   sel[2]=nom[4];
   sel[1]=nom[3];
   sel[0]='0';
}

//get user pass
void get_pass()
{
int cc,adr;
   adr=160;
   for(cc=0;cc<=14;cc++)smstxt[cc]=read_eeprom(adr+cc);
   smstxt[15]='\0';
   strcpy(userpass,smstxt);
}

//get @ message
void get_att()
{
int cc,adr;
   adr=13;
   for(cc=0;cc<=24;cc++)smstxt[cc]=read_eeprom(adr+cc);
   smstxt[25]='\0';
   strcpy(atplace,smstxt);
}

//PSELx SMSx+ SMSx- PSELx-
void pselx(){
   int i;
   
   for(i=1;i<=6;i++){
      sprintf(tstr,"PSEL%1u+",i);
      if(strstr(istr,tstr)!=0){         
         if(i==1){wrcell(55);cel1p=true;cel1a=false;rdcell(55);}
         if(i==2){wrcell(70);cel2p=true;cel2a=false;rdcell(70);}
         if(i==3){wrcell(85);cel3p=true;cel3a=false;rdcell(85);}
         if(i==4){wrcell(100);cel4p=true;cel4a=false;rdcell(100);}
         if(i==5){wrcell(115);cel5p=true;cel5a=false;rdcell(115);}
         if(i==6){wrcell(130);cel6p=true;cel6a=false;rdcell(130);}
      } 
   }
   
   for(i=1;i<=6;i++){
      sprintf(tstr,"PSEL%1u-",i);
      if(strstr(istr,tstr)!=0){         
         if(i==1){cel1p=false;cel1a=false;strcpy(cellu1,"!PROG");}
         if(i==2){cel2p=false;cel2a=false;strcpy(cellu2,"!PROG");}
         if(i==3){cel3p=false;cel3a=false;strcpy(cellu3,"!PROG");}
         if(i==4){cel4p=false;cel4a=false;strcpy(cellu4,"!PROG");}
         if(i==5){cel5p=false;cel5a=false;strcpy(cellu5,"!PROG");}
         if(i==6){cel6p=false;cel6a=false;strcpy(cellu6,"!PROG");}
      }      
   }
   
   for(i=1;i<=6;i++){
      sprintf(tstr,"SMS%1u+",i);
      if(strstr(istr,tstr)!=0){
         if(i==1)cel1a=true;
         if(i==2)cel2a=true;
         if(i==3)cel3a=true;
         if(i==4)cel4a=true;
         if(i==5)cel5a=true;
         if(i==6)cel6a=true;
      }
   }
   
   for(i=1;i<=6;i++){
      sprintf(tstr,"SMS%1u-",i);
      if(strstr(istr,tstr)!=0){
         if(i==1)cel1a=false;
         if(i==2)cel2a=false;
         if(i==3)cel3a=false;
         if(i==4)cel4a=false;
         if(i==5)cel5a=false;
         if(i==6)cel6a=false;
      }
   }
      
      eesave();          
}



//Send to other cell
void sms_to(){
     strcpy(cell,baassel);
     sms_now(cell,smstxt);     
     if(cel1a){strcpy(cell,cellu1);
     sms_now(cell,smstxt);}
     if(cel2a){strcpy(cell,cellu2);
     sms_now(cell,smstxt);}
     if(cel3a){strcpy(cell,cellu3);
     sms_now(cell,smstxt);}
     if(cel4a){strcpy(cell,cellu4);
     sms_now(cell,smstxt);}
     if(cel5a){strcpy(cell,cellu5);
     sms_now(cell,smstxt);}
     if(cel6a){strcpy(cell,cellu6);
     sms_now(cell,smstxt);} 
}


//1s interrupt
#int_RTCC
void sek1(){

}

//Heartbeat led
#int_TIMER1
void  hb_isr(void) 
{
   if(initf)output_low(hbled);
      else output_toggle(hbled);    
}

#int_RDA
void  RDA_isr(void) 
{
      chin=fgetc(pcrs);
   if(!insms){
      if((chin=='\r')||(chin=='\n')){chin='*';lfcr=true;}
      if((chin>=' ')&&(chin<='~')){
         istr[len]=toupper(chin);
         len++;
      if(len>299){len=0;lfcr=true;istr[299]=0;}
      }
   }
}

//Main program
void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_20);
   setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);  
   setup_oscillator(OSC_NORMAL);
   enable_interrupts(INT_RTCC);
   enable_interrupts(INT_TIMER1);
   disable_interrupts(int_rda);    
   enable_interrupts(GLOBAL);   
   flags=0;
   rs232_errors=0; 
   insms=false;
   clr_buf();
   modem_init();              //Init modem
   selacc0=read_eeprom(0);
   selacc1=read_eeprom(1);

   if(atp)get_att();else strcpy(atplace,"!PROG"); 
   if(cel1p)rdcell(55);else strcpy(cellu1,"!PROG");
   if(cel2p)rdcell(70);else strcpy(cellu2,"!PROG");
   if(cel3p)rdcell(85);else strcpy(cellu3,"!PROG");   
   if(cel4p)rdcell(100);else strcpy(cellu4,"!PROG");
   if(cel5p)rdcell(115);else strcpy(cellu5,"!PROG");
   if(cel6p)rdcell(130);else strcpy(cellu6,"!PROG");
   if(baasp)rdcell(40);else strcpy(baassel,init_cell);
   if(pwa)get_pass();else strcpy(userpass,"HELP!");

   sprintf(smstxt,"Boot OK!\n\rSystem ready...");
   strcpy(cell,baassel);
   sms_now(cell,smstxt);
   delay_ms(2000);

   enable_interrupts(int_rda); 
   
//Main loop
   while(true){

   //Wait for lfcr from rda and process
   if(lfcr){
      setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);   
      delay_ms(3000);       
      insms=true;
      istr[len+1]='\0';
      legal=false;

   //Check if CLIP
      strcpy(tstr,"+CLIP:");
      if(strstr(istr,tstr)!=0){
      rngok=false;
   //Get the sender's cell number and store
      strcpy(tstr,"""0");
      strncpy(cell,strstr(istr,tstr),10);
      cell[10]=NULL;   //strncpy does not terminate string!  
   //Check if it is the baassel
      cmodten(baassel);  
      if(!strcmp(sel,cell)){rngok=true;goto rngpass;} 
   //Check users cell numbers
      strcpy(tstr,cellu1);
      cmodten(tstr);
      if(!strcmp(sel,cell)){rngok=true;goto rngpass;}
      strcpy(tstr,cellu2);
      cmodten(tstr);
      if(!strcmp(sel,cell)){rngok=true;goto rngpass;}
      strcpy(tstr,cellu3);
      cmodten(tstr);
      if(!strcmp(sel,cell)){rngok=true;goto rngpass;}
      strcpy(tstr,cellu4);
      cmodten(tstr);
      if(!strcmp(sel,cell)){rngok=true;goto rngpass;}
      strcpy(tstr,cellu5);
      cmodten(tstr);
      if(!strcmp(sel,cell)){rngok=true;goto rngpass;}
      strcpy(tstr,cellu6);
      cmodten(tstr);
      if(!strcmp(sel,cell)){rngok=true;goto rngpass;}
      delay_ms(2000);
      fprintf(pcrs,"ATH\r"); 
      goto nxtsms;
      rngpass:;
         //pulse gate remote 
         output_high(gatr);
         delay_ms(1000);
         output_low(gatr);    
         }

   //Check if pc wants to talk
      strcpy(tstr,"!!!");
      if(strstr(istr,tstr)!=0){
         strcpy(cell,"PC");
         strcpy(user,"PC");
         legal=true;
         goto psmsok;}
         
   //Check if SMS
      strcpy(tstr,"+CMT:");
      if(strstr(istr,tstr)==0)goto nxtsms;   //not a SMS

   //Get the sender's cell number and store
      strcpy(tstr,"+27");
      if(strstr(istr,tstr)==0)goto nxtsms;
      strncpy(cell,strstr(istr,tstr),12);
      cell[12]='\0';   //strncpy does not terminate string!      

   //Check if INITPASSWORD: access system from any cellphone
      strcpy(tstr,initpass);
      if(strstr(istr,tstr)!=0){legal=true;goto psmsok;}
   //Check if INITCELL     
      strcpy(tstr,init_cell);
      if(!strcmp(tstr,cell)){legal=true;goto psmsok;}
   //Check if USERPASSWORD: access system from any cellphone
      strcpy(tstr,userpass);
      if(strstr(istr,tstr)!=0){strcpy(user,"PASSWORD"),legal=true;goto psmsok;}
   //Check if it is the baassel
      strcpy(tstr,baassel);
      if(!strcmp(tstr,cell)){legal=true;goto psmsok;}
   //Check users cell numbers
      strcpy(tstr,cellu1);
      if(!strcmp(tstr,cell)){strcpy(user,"USER1"),legal=true;goto psmsok;}   
      strcpy(tstr,cellu2);
      if(!strcmp(tstr,cell)){strcpy(user,"USER2"),legal=true;goto psmsok;}   
      strcpy(tstr,cellu3);
      if(!strcmp(tstr,cell)){strcpy(user,"USER3"),legal=true;goto psmsok;}   
      strcpy(tstr,cellu4);
      if(!strcmp(tstr,cell)){strcpy(user,"USER4"),legal=true;goto psmsok;}   
      strcpy(tstr,cellu5);
      if(!strcmp(tstr,cell)){strcpy(user,"USER5"),legal=true;goto psmsok;}   
      strcpy(tstr,cellu6);
      if(!strcmp(tstr,cell)){strcpy(user,"USER6"),legal=true;goto psmsok;}   
psmsok:;   
   
   //Grant access to parsing if OK
   if(!legal){
      strcpy(user,atplace);
      strcpy(atplace,"ACCESS DENIED!");
      sprintf(smstxt,"%s reported to owner!",cell);
      sms_now(cell,smstxt);
      strcpy(atplace,user);
      //Now send sms to baas
      sprintf(smstxt,"%s trying access!",cell);
      sms_now(baassel,smstxt);
      goto nxtsms;
   }  

   selacc0=read_eeprom(0);
   selacc1=read_eeprom(1); 

      //Check incoming SMS and execute accordingly
      for(q=0;q<=12;q++){
   
      //Parse the SMS
         if(q==0){
            strcpy(tstr,"ALARM+");
            if(strstr(istr,tstr)!=0){            
               if(input(almi)){
                  sprintf(smstxt,"Alarm already activated.");
                  sms_now(cell,smstxt);
                  goto nosms1;
               }  
               //Pulse the alarm remote button
               output_high(almr);
               delay_ms(1000);
               output_low(almr);
               delay_ms(2000);
               if(input(almi)){
                  sprintf(smstxt,"Alarm ARMED!");            
                  sms_now(cell,smstxt);
                  strcpy(tstr,baassel);
                  if(!strcmp(tstr,cell))goto sendonce;
                  strcpy(cell,baassel);
                  sprintf(smstxt,"ALARM ARMED! by %s",user);
                  sms_now(cell,smstxt);
                  sendonce:;
               }
               else{ 
                  sprintf(smstxt,"ERROR: ALARM NOT ARMED!!!");
                  sms_now(cell,smstxt);            
               }
            }
            nosms1:;   
         }
         
         if(q==1){
            strcpy(tstr,"ALARM-");
            if(strstr(istr,tstr)!=0){
   
            
               if(!input(almi)){
                  sprintf(smstxt,"Alarm already disarmed.");
                  sms_now(cell,smstxt);
                  goto nosms2;
               }
               //Pulse the alarm remote button
               output_high(almr);
               delay_ms(1000);
               output_low(almr);
               delay_ms(2000);
               if(!input(almi)){
                  sprintf(smstxt,"Alarm DISARMED!");
                  sms_now(cell,smstxt);
   
                  strcpy(tstr,baassel);
                  if(!strcmp(tstr,cell))goto sendoncez;
                  strcpy(cell,baassel);
                  sprintf(smstxt,"ALARM DISARMED! by %s",user);
                  sms_now(cell,smstxt);
                  sendoncez:;
               }
               else{
                  sprintf(smstxt,"ERROR: ALARM STILL ARMED!!!");
                  sms_now(cell,smstxt);          
               }
            }
         nosms2:;
         acta=false;
         }
   
         if(q==2){
            strcpy(tstr,"ALARM?");
            if(strstr(istr,tstr)!=0){
               if(input(almi))sprintf(smstxt,"ALARM is ARMED!");
                  else sprintf(smstxt,"ALARM is DISARMED!");          
               sms_now(cell,smstxt);
            }
         }     
         
         //Program @ message
         if(q==3){
            strcpy(tstr,"PPLEK+");
            if(strstr(istr,tstr)!=0){
            strncpy(smstxt,strstr(istr,tstr),50);
            smstxt[50]='\0';
            ptr=strchr(smstxt,'+');
            for(i=1;i<=24;i++){
               if(ptr[i]=='*')break;
               atplace[i-1]=ptr[i];
            }
            atplace[i-1]='\0';
            atplace[24]='\0';
            for(i=0;i<=24;i++)write_eeprom(13+i,atplace[i]);
            atp=true;
            eesave();
            get_att();
            }
         }
         
         //Program baas
         if(q==4){
            strcpy(tstr,"PBAAS+");
            if(strstr(istr,tstr)!=0){
            wrcell(40);
            baasp=true;
            eesave();
            rdcell(40);        
            }
         }
          
         //Program cell attributes
         if(q==5){
            pselx();
         }     
         
         if(q==6){
            strcpy(tstr,"PBAAS-");
            if(strstr(istr,tstr)!=0){
               baasp=false; 
               strcpy(baassel,"!PROG");
               eesave();
            }
         }       
          
         if(q==7){
            strcpy(tstr,"PPLEK-");
            if(strstr(istr,tstr)!=0){
               atp=false;  
               strcpy(atplace,"!PROG");
               eesave();
            }
         }       
    
         if(q==8){
            strcpy(tstr,"CNPS?");
            if(strstr(istr,tstr)!=0){
               if(cel1a)strcpy(c1sms,smss);
                  else strcpy(c1sms,smsr);
               if(cel2a)strcpy(c2sms,smss);
                  else strcpy(c2sms,smsr);
               if(cel3a)strcpy(c3sms,smss);
                  else strcpy(c3sms,smsr);
              if(cel4a)strcpy(c4sms,smss);
                  else strcpy(c4sms,smsr);
               if(cel5a)strcpy(c5sms,smss);
                  else strcpy(c5sms,smsr);
               if(cel6a)strcpy(c6sms,smss);
                  else strcpy(c6sms,smsr);     
               sprintf(smstxt,"INFO:\n\r1=%s-%s\n\r2=%s-%s\n\r3=%s-%s\n\r4=%s-%s\n\r5=%s-%s\n\r6=%s-%s\n\r",
                  cellu1,c1sms,cellu2,c2sms,cellu3,c3sms,cellu4,c4sms,cellu5,c5sms,cellu6,c6sms);
               sms_now(cell,smstxt);
            }         
         }
   
         //Special info command 
         if(q==9){
            strcpy(tstr,"INFO?");
            if(strstr(istr,tstr)!=0){
               sprintf(smstxt,"Baas: %s\n\rPass: %s\n\rPICGSMOC 4 book V1.0\n\r(c)2015 Jannie Hamman\n\r"__DATE__">"__FILENAME__"@"__TIME__,
               baassel,userpass);
               sms_now(cell,smstxt);   
            }
         }
         
         //Program userpass
         if(q==10){
            strcpy(tstr,"PPASS+");
            if(strstr(istr,tstr)!=0){
            strncpy(smstxt,strstr(istr,tstr),50);
            smstxt[50]='\0';
            ptr=strchr(smstxt,'+');
            for(i=1;i<=15;i++){
               if(ptr[i]=='*')break;
               userpass[i-1]=ptr[i];
            }
            userpass[i-1]='\0';
            userpass[14]='\0';
            for(i=0;i<=14;i++)write_eeprom(160+i,userpass[i]);
            pwa=true;
            eesave();
            get_pass();
            }
         }
         
           //Open the gate
         if(q==11){
            strcpy(tstr,"GATE");
            if(strstr(istr,tstr)!=0){  
               //pulse gate remote 
               output_high(gatr);
               delay_ms(1000);
               output_low(gatr);
            }
         }
         
          //Open the garage
         if(q==12){
            strcpy(tstr,"GARAGE");
            if(strstr(istr,tstr)!=0){  
               //pulse garage remote 
               output_high(garr);
               delay_ms(1000);
               output_low(garr);
               }
         }
      }     //if q 
      
   nxtsms:
   clr_buf();
   insms=false;
         
   } //if lfcr

//Rest of user program

   //Flash hb led slow when parsing sms else fast
      setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
   //Send SMS on intruder detection, ie siren is on
   
   if(input(siri)&&acta){
      delay_ms(5000);      //allow siren to beep and beepbeep on arming/disarm
      if(!input(siri))goto nointrus;
      if(!acta){
         acta=true;
         sprintf(smstxt,"!!!ALARM ACTIVATION!!!");            
         sms_to();
      }
   nointrus:;      
   }

   }  //end of while loop
}     //end of main
