reading sms from gsm modem and comparing with predefind one

24 posts / 0 new
Last post
td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70
reading sms from gsm modem and comparing with predefind one

hi,

in our project, we are using pic 18f and modem sim 300.and we are using c18 compiler.in that i have written program for sending and receiving sms through gsm modem.when i use the AT command for reading sms, i m getting like this

 

AT+CMGR=1
+CMGR: "REC READ","+918108111649",,"12/07/22,14:24:11+22"
Hello

OK

 

here i have to read the sms "Hello", which comes before "OK".and compare it with a predefined string. i write for parsing string.but i m not getting the word "Hello". here is codes...

 

<code>

putrsUSART("AT\n\r");
putrsUSART("AT+CMGF=1");
putcUSART(0x0D);
Delay10KTCYx(500);
putrsUSART("AT+CMGR=1");
putcUSART(0x0D);
Delay10KTCYx(500);

 if(PIR1bits.RCIF)  
{
for(i=0;i<5;i++)

    a[i]=getcUSART();

}
putsUSART(a);
 

</code>

while exicuting this, i m getting "a " as some srange characters like ?2k> or ?ï2oÈ

how can i extract the messange hello?? please help me to solve this problem.

thank you

Manikandan
manikandan.no001@gmail.com's picture
Offline
Joined: 04/02/2011
Posts: 1
Points: 0

Hi,

   I think delay may be the problem. please check and correct the delay.

Thank you,

Manikandan A

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

Thank you for reply.

 

i have reduced the delay .and took the buffer like a[50],

in both case stange characters are coming.

mohan
mohansaini's picture
Offline
Joined: 19/08/2010
Posts: 10
Points: 1030

you are using a for loop when PIR1bits.RCIF bit is set by the interrupt thats wrong way.

do it in this way

const char arr1[]="Hello";

 

 if(PIR1bits.RCIF) 

  {

    getcUSART();// say its your interrupt subroutine

   }

 void getcUSART()

  {

     unsigned char Rcv_Byte;

     int count=0;//can be defined as a global variable

     Rcv_Byte=RXREG;

     //if u want to search for hello only

      if(Rcv_Byte==arr1[count]||count>5)

       {

         if(Rcv_Byte!=0x0D)

          {

           Buff[count]=Rcv_Byte;

           count++;

           }

          else

           {

             Buff[count]='\0';//Add NULL at last to make it a string

             count=0;

             flag=1;//set a flag here

          }

         else

   {

  count=0;

   }

}

This code just for a reference or  just to give an idea.

check with this one.

        

     

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

thanks for reply..

 

what you mean by

if(Rcv_Byte==arr1[count]||count>5)

 

is that necessary to set flag??

mohan
mohansaini's picture
Offline
Joined: 19/08/2010
Posts: 10
Points: 1030

if(Rcv_char==arr1[count])

  means count is zero initially if received char is h because arr1[0] is also h it will go to the if condition.

 

no its not mandatory to set a flag its all up to you in which way you want to write.

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

thank you 

 

here your program is for getting hello only..but the modem response is 

 

+CMGR: "REC READ","+918108111649",,"12/07/22,14:24:11+22"
Hello

OK

so 1st we have to read these whole string.how can i read this whole sting?

i used some inbuilt functions for reading charecter wise.but some strange characters are coming.here is my code.

 

for(i=0;i<70;i++)

    a[i]=getcUSART();

}
putsUSART(a);

how can i input that whole sting to pic?

 

 

 

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

i changed the steps, but i m not getting the string...

+CMGR: "REC READ","+918108111649",,"12/07/22,14:24:11+22"
Hello

OK

 

here is the pgm......

 

putrsUSART("AT+CMGR=1");
putcUSART(0x0D);

while(BusyUSART());
gets(a);
Delay1KTCYx(75);
putsUSART(a);

}

 

void gets(char *buffer)
{
  unsigned char data;
 do
  {
    while(!DataRdyUSART());// Wait for data to be received
    data = getcUSART();                         
    *buffer = data;
    buffer++;              // Increment the string pointer
  }
while(data!='K');
*buffer='\0';
}

 

please help me to print the modem response....

 

thank you

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

when i connected pic and levelconverter to pc, the above program is working fine.but, when i connected modem and pic, i can send sms.and modem can read the sms.but when the modem execute the AM+CMGR=1 command, the modem response string is not going to pic. please help me to solve the problem..

Amrith
amrith's picture
Offline
Joined: 23/04/2012
Posts: 224
Points: 1000

Hi td,

Correct your command mentioned AM+CMGR=1 as AT+CMGR = 1,

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

in my program, i have written like this

AT+CMGR = 1

when i re-write in last post, AT changes to AM. sorry for the mistske.

 

please give me one solution to my problem.how can i give modem response to pic?

Amrith
amrith's picture
Offline
Joined: 23/04/2012
Posts: 224
Points: 1000

Hi,

As you send command AT+CMGR =1 to GSM modem, you will receive the message data. you might not capturing the data properly in the buffer. Avoid delay functions in between this communication.

 

Important Note: Take care that you are connecting Tx pin of GSM module to Rx pin of DB9 serial cable & Rx pin of GSM module to Tx pin of DB9 serialcable.

 

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

i m not giving the delay aftr AT+CMGR=1

 

 

putrsUSART("AT+CMGR=1");
putcUSART(0x0D);

while(BusyUSART());
gets(a);

 

 

and i connected modem and pic directly- rx of pic to tx of modem and vice verse. but then also nothing is going to tic.ie, response of modem is not going to pic

Amrith
amrith's picture
Offline
Joined: 23/04/2012
Posts: 224
Points: 1000

Hey,

You must use level converter between your PIC & GSM modem, better share your schematics to verify how you are connecting..

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

thank for reply..

my pic is working at 3.3v and modem at 4v.so why should i use level converter? i connected rx,tx pin of modem to tx,rx pin of pic.

and connected level converter(max232) parallel to pic(used only for watching in hyper)

 

i can send sms from modem to mobile.and modem can read the sms..but the response of the modem is not going to pic

 

 

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

hi

 

in this project pic is not getting the response from modem.

i just write the AT command, then modem is responding OK.but pic is not getting that response.i tried to print the response from modem.but i m not getting any output.

when i checked pic and hyper, its working and checked modem and hyper.that is also working. ang when i connected pic and gsm modem, modem is getting all the commands from controller.and i can send sms from modem to mobile.but in case of sms reading, modem is responding for AT+CMGR=1.

but pic is not getting that response.ie,from pic to gsm, transmission is ok.but from gsm to pic, not working.

 

please help me to sove this problem

Amrith
amrith's picture
Offline
Joined: 23/04/2012
Posts: 224
Points: 1000

you need a level converter between PIC & GSM modem because the GSM modem has a level converter inside which converts ttl to rs232 level, so you should not connect GSM modem directly to controller, you got to use level converter to convert these rs232 levels to ttl levels again.

 

Amrith
amrith's picture
Offline
Joined: 23/04/2012
Posts: 224
Points: 1000

if you are using GSM module then there is no need of level converter between your controller & GSM module.

Note: Remember GSM Modem is different fromGSM module.

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

 thanks for reply.

how can i connect level converter in between modem and pic?now i connected

pic tx-------- modem rx

pic rx-------- modem tx

and connected max232  input pin(rx) ------modem tx

                        max232  output pin(tx) ------modem rx

ie, level convertr parallelly.

 

 

without connecting this max232, i can send sms in to mobile.

but the problem is response of modem is not going to pic

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

 here i m using gsm module. i directly connect gsm module and controller.

i have connected rx,tx,vcc,power key and ground of modem.is there any extra connections in modem?

when i load the pgm to pic, and connected with modem, i can send sms from modem to mobile. but the response of the modem is not going to pic.ie, when i give through pic, AT+CMGR=1, modem can read the sms.but that the response of modem(+CMGR: "REC READ","+918108111649",,"12/07/22,14:24:11+22"
Hello
OK) is not going to pic.

please give me solution.. please guide me...

Mukundan devakian...
ddmukundan's picture
Offline
Joined: 09/08/2012
Posts: 3
Points: 10

hello

 

i am also having a same issue in my project, i am using sim 900 module, when i connect to pc i am getting correct response, but when i connect to PIC i am getting the command back. i am not getting the response. i am sending first "A",then "T" and then finally 0x0D. i am getting same back on my LCD. when i connect PIC to PC i am getting the correct response. 

 

 

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

here  i m using 8mhz internal oscillator.for that i use

OSCCON =0b01100010;

(in 18f45k20)

is ther any additional registers i have to set for this?may be because of this, receiver part is not ok.

and we are using baud rate 9600.

please guide me..

td
td micro's picture
Offline
Joined: 25/06/2012
Posts: 16
Points: 70

i didnt get any solution.................

now also the response of the modem is not going to pic...........

Bharath Singh
bharathsinghece's picture
Offline
Joined: 06/12/2012
Posts: 1
Points: 5

Hi freinds ....... i need help from u ... how to compare the string that as beed present in GSM modem as a msg  in inbox after sending a Read command  to GSM modem AT+CMGR.........

Login or register to post comments
You are here