HI guys,
My project is about switching on and off of motor by sms control.. I tried to check whether my controller is working fine or not using simple blinking of LED program , its working good, no problem with oscillator and reset switch part... everythng is fine but when i try for serial programming i am not getting the output.. I checked my gsm modem using hyperterminal its working fine.but its not working with 8051.
below is my code
#include <reg51.h> void init (void); // prototype declaration void Uart_gsm (void); void delay(unsigned char); void Recievedata(); void gsminit(void); void gsmcmdsend(unsigned char *); unsigned char Rx_data(void); unsigned char message[11]; unsigned char count; unsigned char code password1[10]="device1 on"; // Commands for controlling devices unsigned char code password2[11]="device1 off";
void main() { // port initialization
init(); Uart_gsm(); // serial port initialization gsminit(); Recievedata(); // for recieving the data from modem }
void init(void) { P2 = 0xFF; }
void Uart_gsm(void) { TMOD = 0x20; TH1 = 0xFD; SCON = 0X50; TR1 = 1; } void serial_put(char c) { do{}while(!TI); TI=0; SBUF=c; }
void sms(void) //sending pump is on { char t5[]={'A','T','+','C','M','G','S','=','"','+','9','1','9','1','7','6','3','6','6','4','9','6','"'} ; char t7[]="PUMP IS ON"; unsigned int i; delay(1000); gsmcmdsend(t5); for(i=0;t7[i];i++)serial_put(t7[i]); do{}while(!TI); TI=0; SBUF=0x1A; delay(20000); }
void Recievedata() { unsigned int count,count1,i; while(1) { while(Rx_data()!='+'); // checking message format while(Rx_data()!='C'); while(Rx_data()!='M'); while(Rx_data()!='T'); while(Rx_data()!=':'); while(Rx_data()!='"'); while(Rx_data()!='\n'); for(count=0;count<11;count++) // storing msg in a buffer { message[count]=Rx_data(); if(message[count]=='\r') break; } message[count]='\0';
for(i=0;i<10;i++) //verifying message { if(message[i]==password1[i]) count++; } if(count==10) { P2 = 0x00; sms(); }
for(i=0;i<11;i++) { if(message[i]==password2[i]) count1++; }
if(count1==11)
{ P2 = 0xFF; }
} } // TO RECIEVE SERAIL DATA unsigned char Rx_data(void) { RI=0; while(RI==0); return(SBUF); }
void delay(unsigned char x) { unsigned int i,j; for(i=0;i<x;i++) for(j=0;j<2000;j++); }
void gsminit(void) { // AT COMMANDS unsigned char gsm_cmd1[]="AT"; unsigned char gsm_cmd2[]="ATE0"; unsigned char gsm_cmd3[]="AT&W"; unsigned char gsm_cmd4[]="AT+CMGF=1"; unsigned char gsm_cmd5[]="AT+CNMI=2,2,0,0,0"; gsmcmdsend(gsm_cmd1); gsmcmdsend(gsm_cmd2); gsmcmdsend(gsm_cmd3); gsmcmdsend(gsm_cmd4); gsmcmdsend(gsm_cmd5); }
void gsmcmdsend(unsigned char *cmd) { unsigned char i; for(i=0;*cmd!='\0';i++) { SBUF=*cmd; while(TI==0); TI=0; cmd++; } delay(2); SBUF=0x0A; while(TI==0); TI=0; SBUF=0x0D; while(TI==0); TI=0; while(RI==0); RI=0; }
I complied this program using KEIL complier , 0 errors and 0 warnings.
After switching On the supply, port 2 goes high..i dont whats the problem with my circuit.. :sad:
You do not (!) test for the reception of a string by having multiple while loops after each other like this:
while(Rx_data()!='+'); while(Rx_data()!='C'); while(Rx_data()!='M'); while(Rx_data()!='T'); while(Rx_data()!=':'); while(Rx_data()!='"'); while(Rx_data()!='\n');
How long do you think your code would have to wait if it accidentally missed to receive the initial '+' character?
What happens if no character at all is received?
What happens if a different string is received, that happens to look like:
ADDITION (+) CAN BE IMPLEMENTED AS: "1+1=2"\n
Would you really like my string to match your string comparison? So why then write a pattern matcher that does find false patterns in random noise? Have you looked at the implementation of strcmp()? Is it implemented with multiple while loops - one for each character to match?
Keil complier doesnt support strings... string header file is not available in the library....
The C language doesn't have strings.
But it has a strings library, working on zero-terminated character arrays.
And Keil most definitely do supoprt these library functions. What was the name of the header file you tried to use?
And if you really wanted to implement your own strcmp() function, a sequence of while loops after each other - one for every character in the match string - is still not a valid way.
Why no response to the fact that the string:
matches your pattern test code?
Gsm incoming message will be in this format
+CMT: "+91xxxxxxxxxx" , xxxtimestampxxxxxx actual message
my code will will test for +CMT in while loop after that again a while loop to check for \n to extract the message.
So if 2+3 is in the message it wont bother.cos program would ve come out of the while loop test for '+' symbol...
@Per Westermark : oh ya u r right i dint notice that..(sorry for the previous reply) .
so how can i extract meggase from the received message.?? can u just modify the code ??? i struggling with that logic....