Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

Project Request

Return
Emmanuel Abraham
mwanyamaki_1992

posted on 2016/12/20 10:58:31 AM CET

Audio & Speech

PROBLEM ON CODE OF GSM CONTROLLING APPLIANCES THROUGH SMS

Dear members I have a problem, I have prepared the code of GSM CONTROLLING APPLIANCES THROUGH SMS and when I compile it no any error comes after compiling and when i tested it on proteus using PIC16F877A all lights are turned on initially and always remain on until i stopped a simulation. Any one who can collect my code please can help me.. Below it's my code the code was compiled in MikroC for PIC.


/*GSM based device control system*/
/*The device status is stored in EEPROM inorder to avoid power failure problem*/
/*Mobile phone interfacing functions requires serial port with a baud rate of 9600*/


/*************************************************************/



sbit Device1 at RC1_bit; /*Device 1*/
sbit Device2 at RC2_bit; //Device 2
sbit Device3 at RC3_bit; //Device 3



unsigned char msg[7]; /*array to store message content*/


/***********************sends a command passed to it***************************/


void Send_Command(unsigned char *ptr)
{
/*it sends the array until a null character reached*/
unsigned char Tx_Count=0;
do
{ Uart1_Write(ptr[Tx_Count]); Tx_Count++;
}while(ptr[Tx_Count]!='\0'); /*checks for null*/
}


/***********************Initmobile***********************/


void Init_Mobile()
{ Send_Command("AT\r\n");
Delay_Ms(1000);
Send_command("ATE0\r\n"); /*ECHO off*/
Delay_Ms(1000);
Send_Command("AT+CMGF=1\r\n"); /*Select TEXT mode*/
Delay_Ms(1000);
}
/******************Reads message from 1st location****************/


/*The address of the destination array is passed*/
unsigned char Read_Message(unsigned char *ptr_array)
{
unsigned char Rx_Count=0,Rx_Buffer,temp;
unsigned char array1[6];
Rx_Buffer=RCREG;
Rx_Buffer=RCREG;
Rx_Buffer=RCREG;
RCSTA.CREN=0;
RCSTA.CREN=1;
Send_Command("AT+CMGR=1\r\n"); /*Command for reading message*/
do{
while(Uart1_Data_Ready()==0){;} Rx_Buffer=RCREG; //Read message
array1[Rx_Count]=Rx_Buffer; Rx_Count++;
}while(Rx_Count<6);


array1[Rx_Count-1]='\0';
if(strcmp(array1,"ERROR")==0){ Send_Command("AT+CMGR=1\r\n");
}


if(Rx_Buffer=='S') /*Checks for valid message*/
return 0;
else if(Rx_Buffer=='G')
{
do /*if message is there*/
{
while(Uart1_Data_Ready()==0){;} /*receive upto next 0x0A*/
Rx_Buffer=RCREG; //avoid number and msg status
}while(Rx_Buffer!=0x0a);


Rx_Count=0;
do
{ while(Uart1_Data_Ready()==0){;} Rx_Buffer=RCREG; //Read message
ptr_array[Rx_Count]=Rx_Buffer; Rx_Count++;
}while(Rx_Buffer!=0x0d); /*receive message uptp 0x0d*/
ptr_array[Rx_count-1]='\0'; /*the array is terminated with a null*/
return 1;
}
}
void Send_msg(){ Send_Command("AT+CMGS=\"9089189115\"\r");
Delay_Ms(1000);
Send_Command("Activated");
Delay_Ms(1000);
Uart1_Write(0x0d);
Delay_Ms(1000);
Uart1_Write(26); // Ctrl+z
}


/*************************************************************************
**/
void main()
{
unsigned char Count=0,Dev_Status;
Uart1_Init(9600); /*Init Uart*/
Dev_Status=Eeprom_Read(0x00); /*read the previous status*/
ADCON1=0x06; /*Porta as digital*/
TRISA=0XFF; /*PORTA input*/
TRISC&=0XF0;
PORTC=Dev_Status; /*output the status*/
Init_Mobile(); /*Init mobile*/
Delay_Ms(1000); /*1sec delay*/


while(1)
{
/*************************************************************************
***/
if(Read_Message(&msg[0])==1)
{
if(strcmp(msg,"1 ON")==0)
{ Device1=1;
Dev_Status =0x02;
Eeprom_Write(0x00,Dev_Status);
Send_msg();
}


else if(strcmp(msg,"1 OFF")==0)
{
Device1=0;
Dev_Status&=0xfd;
Eeprom_Write(0x00,Dev_Status);
Send_msg();
}


else if(strcmp(msg,"2 ON")==0)
{ Device2=1; Dev_Status=0x04;
Eeprom_Write(0x00,Dev_Status);
Send_msg();
}


else if(strcmp(msg,"2 OFF")==0)
{ Device2=0; Dev_Status&=0xfA;
Eeprom_Write(0x00,Dev_Status);
Send_msg();
}


else if(strcmp(msg,"3 ON")==0)
{ Device3=1; Dev_Status=0x08;
Eeprom_Write(0x00,Dev_Status);
Send_msg();
}


else if(strcmp(msg,"3 OFF")==0)
{ Device3=0; Dev_Status&=0xf7;
Eeprom_Write(0x00,Dev_Status);
Send_msg();
}
Delay_Ms(2000); /*2sec delay*/
Send_Command("AT+CMGD=1\r\n"); //Delete message
Delay_Ms(2000);
}


else
{
;
}


Delay_Ms(3000); //Delay before next checking



}
}