Seeed-Studio / GPRS_Shield_Suli

GPRS Shield based on SIM900
http://www.seeedstudio.com/depot/GPRS-Shield-V20-p-1379.html?cPath=19_20
MIT License
48 stars 40 forks source link

GSM recieving problem #2

Closed ortegafernando closed 9 years ago

ortegafernando commented 9 years ago

Hi,

thanks for this great library.

I have found an issue in GPRS_LoopHandle example.

I'm not using your official shield, but a third party SIM900 shield.

It's about finding CMTI text in uart.

If you don't config your SIM900 with the AT command +CNMI (is used to specify how newly arrived SMS messages should be handled. You can tell the GSM/GPRS modem or mobile phone either to forward newly arrived SMS messages directly to the PC, or to save them in message storage and then notify the PC about their locations in message storage), you could not recieve CMTI text in uart.

We have two possible solution (I prefer b) solution)

a. Always config with this AT command: CNMI b.Change the library with a function like: isSMSpresent that will return the index of the first unread SMS

Here is the code from SIM900.h library:

char SMSGSM::IsSMSPresent(byte required_status) { char ret_val = -1; char *p_char; byte status;

 if (CLS_FREE != gsm.GetCommLineStatus()) return (ret_val);
 gsm.SetCommLineStatus(CLS_ATCMD);
 ret_val = 0; // still not present

 switch (required_status) {
 case SMS_UNREAD:
      gsm.SimpleWriteln(F("AT+CMGL=\"REC UNREAD\""));
      break;
 case SMS_READ:
      gsm.SimpleWriteln(F("AT+CMGL=\"REC READ\""));
      break;
 case SMS_ALL:
      gsm.SimpleWriteln(F("AT+CMGL=\"ALL\""));
      break;
 }

 // 5 sec. for initial comm tmout
 // and max. 1500 msec. for inter character timeout
 gsm.RxInit(5000, 1500);
 // wait response is finished
 do {
      if (gsm.IsStringReceived("OK")) {
           // perfect - we have some response, but what:

           // there is either NO SMS:
           // <CR><LF>OK<CR><LF>

           // or there is at least 1 SMS
           // +CMGL: <index>,<stat>,<oa/da>,,[,<tooa/toda>,<length>]
           // <CR><LF> <data> <CR><LF>OK<CR><LF>
           status = RX_FINISHED;
           break; // so finish receiving immediately and let's go to
           // to check response
      }
      status = gsm.IsRxFinished();
 } while (status == RX_NOT_FINISHED);

 switch (status) {
 case RX_TMOUT_ERR:
      // response was not received in specific time
      ret_val = -2;
      break;

 case RX_FINISHED:
      // something was received but what was received?
      // ---------------------------------------------
      if(gsm.IsStringReceived("+CMGL:")) {
           // there is some SMS with status => get its position
           // response is:
           // +CMGL: <index>,<stat>,<oa/da>,,[,<tooa/toda>,<length>]
           // <CR><LF> <data> <CR><LF>OK<CR><LF>
           p_char = strchr((char *)gsm.comm_buf,':');
           if (p_char != NULL) {
                ret_val = atoi(p_char+1);
           }
      } else {
           // other response like OK or ERROR
           ret_val = 0;
      }

      // here we have gsm.WaitResp() just for generation tmout 20msec. in case OK was detected
      // not due to receiving
      gsm.WaitResp(20, 20);
      break;
 }

 gsm.SetCommLineStatus(CLS_FREE);
 return (ret_val);

}

Could you update your library with a similar function ?

May be your function could return both index and total number of UNREAD sms

Hope your answer.

Best regards,

ortegafernando commented 9 years ago

I'm a noob, but I can get index with this function. It HAVE TO BE improve with people like you (buffer sizes,how to get how many messages you have, and so on)

int IsSMSPresent(byte required_status) { char gprsBuffer[100]; char *p;

 sim900_check_with_cmd("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
 suli_delay_ms(1000);
 switch (required_status) {
 case SMS_UNREAD:
      sim900_send_cmd("AT+CMGL=\"REC UNREAD\"\r\n");
      break;
 case SMS_READ:
      sim900_send_cmd("AT+CMGL=\"REC READ\"\r\n");
      break;
 case SMS_ALL:
      sim900_send_cmd("AT+CMGL=\"ALL\"\r\n");
      break;
 }
sim900_clean_buffer(gprsBuffer,100);
sim900_read_buffer(gprsBuffer,100,DEFAULT_TIMEOUT);

if(NULL != ( s = strstr(gprsBuffer,"+CMGL:"))){  
           // there is some SMS with status => get its position
           // response is:
           // +CMGL: <index>,<stat>,<oa/da>,,[,<tooa/toda>,<length>]
           // <CR><LF> <data> <CR><LF>OK<CR><LF>
           p = strstr(gprsBuffer,":");
           if (p != NULL) {
                return atoi(p+1);
           }               
  }      
return 0; 

}

lawliet004 commented 9 years ago

hi,

    I am very glad to hear from you, and thanks sincerely for your advice in email, it is really a question that we need to focus on.

    We just test the GPRS_Shield_Suli library with our GPRS Shield module in China and it works fine.  So it may have some bugs with a third party SIM900 Shield or in other country.  As you said,  you have some problem on receiving the SMS message, it is about the AT+ CNMI command. Actually, our GPRS Shield will set command "AT+CNMI = 2,1" when initializing. So, when a new message arrives, it will send the CMTI text to uart directly, (e.g. +CMTI: "SM",24\r\n). 

    I also have a problem with your int IsSMSPresent(byte required_status) function, AT+CMGL is used to list out the specified messages(unread / read/ all), how can you distinguish the message that you need from the message lists? 

    Looking forward for your reply and further discussion. thank you~

lawliet.zou@gmail.com

From: op2op2op2 Date: 2014-10-10 01:15 To: Seeed-Studio/GPRS_Shield_Suli Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2) I'm a noob, but I can get index with this function. It HAVE TO BE improve with people like you (buffer sizes,how to get how many messages you have, and so on)

int IsSMSPresent(byte required_status) { char gprsBuffer[100]; char *p; sim900_check_with_cmd("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD); suli_delay_ms(1000); switch (required_status) { case SMS_UNREAD: sim900_send_cmd("AT+CMGL=\"REC UNREAD\"\r\n"); break; case SMS_READ: sim900_send_cmd("AT+CMGL=\"REC READ\"\r\n"); break; case SMS_ALL: sim900_send_cmd("AT+CMGL=\"ALL\"\r\n"); break; } sim900_clean_buffer(gprsBuffer,100); sim900_read_buffer(gprsBuffer,100,DEFAULT_TIMEOUT); if(NULL != ( s = strstr(gprsBuffer,"+CMGL:"))){
// there is some SMS with status => get its position // response is: // +CMGL: ,,<oa/da>,,[,<tooa/toda>,] // OK p = strstr(gprsBuffer,":"); if (p != NULL) { return atoi(p+1); }
}
return 0;

} — Reply to this email directly or view it on GitHub.

ortegafernando commented 9 years ago

Hi,

My shield is also a sIM900 chip one. I dont know if it can have a diferent firmware.

As I said I am a noob with arduino, but when you call for "UNREAD SMS" AT+CMGL will return a list of all SMS that meets that condition, so if there is only one, you will recieve that SMS, if there are several, you will recive the text for the first SMS, then the second, and so on.

I think that if you call again CMGL all SMS will be marked as READ (I can't ensure this), and also, I think that if you call your funtion "readSMS" with the index that I've obtained with my function, the SMS will be marked as READ (I can't ensure this)

CMGL writes into uart +CMGL: so you can get the index of the UNREAD SMS.

Problems or bugs that my sketch has:

  1. Buffer is only 100 chars, so I you recieve a message with more chars, will be a problem (Also you have to add length of CMGL response
  2. If CMGL sends several SMS you will have to return an array of indexes of that messages.

I think that an arduino program working with a SIM900 will have to do this secuence:

  1. check if a UNREAD SMS is present.
  2. read it
  3. DELETE it
  4. process SMS.

Why delete it ?

Because otherwise, SIM CARD will be full and you will not recieve any more SMS.

In order to do the secuence, I think that a function like isSMSpresent is necessary, but has to be improve to return an array of indexes.

What do you think about it ?

lawliet004 commented 9 years ago

hi, sorry for my late reply :)
Yes, it is really a good idea to get the SMS message as your secuence. Maybe a little complicated, but I think that it is strong enough to work normally.
and, er... would you like to finish your Read SMS function for this library and then pull request to the github? If your answer is yes, then I could offer you some coupon in our bazzar .^_^.
Thanks for your attention & contribution again. Seeed is very appreciative of your efforts and sharing. We need and love makers like you, open hardware movement needs makers like you.

ortegafernando commented 9 years ago

Ok, I will try and I will use your coupon .^_^. I never pull changes to github, could you help me how to do it ? best regards,

Date: Sun, 12 Oct 2014 19:47:41 -0700 From: notifications@github.com To: GPRS_Shield_Suli@noreply.github.com CC: op2op2op2@hotmail.com Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2)

hi,

   sorry for my late reply :)

   Yes, it is really a good idea to get the SMS message as your secuence. Maybe a little complicated, but I think that it is strong enough to work normally. 

   and, er...  would you like to finish your Read SMS function for this library and then pull request to the github? If your answer is yes, then I could offer you some coupon in our bazzar .^_^. 

   Thanks for your attention & contribution again. Seeed is very appreciative of your efforts and sharing. We need and love makers like you, open hardware movement needs makers like you.

— Reply to this email directly or view it on GitHub. =

lawliet004 commented 9 years ago

hi, you can just follow the instruction of the two URL below https://help.github.com/articles/using-pull-requests/ https://help.github.com/articles/creating-a-pull-request/

    If you are not familiar with the operation, you can also create your test repository firstly, and then pull request to it as a try.  Good luck!

lawliet.zou@gmail.com

From: op2op2op2 Date: 2014-10-15 03:17 To: Seeed-Studio/GPRS_Shield_Suli CC: lawliet zou Subject: Re: [GPRS_ShieldSuli] GSM recieving problem (#2) Ok, I will try and I will use your coupon .^^. I never pull changes to github, could you help me how to do it ? best regards,

Date: Sun, 12 Oct 2014 19:47:41 -0700 From: notifications@github.com To: GPRS_Shield_Suli@noreply.github.com CC: op2op2op2@hotmail.com Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2)

hi,

sorry for my late reply :)

Yes, it is really a good idea to get the SMS message as your secuence. Maybe a little complicated, but I think that it is strong enough to work normally.

and, er... would you like to finish your Read SMS function for this library and then pull request to the github? If your answer is yes, then I could offer you some coupon in our bazzar .^_^.

Thanks for your attention & contribution again. Seeed is very appreciative of your efforts and sharing. We need and love makers like you, open hardware movement needs makers like you.

— Reply to this email directly or view it on GitHub. = — Reply to this email directly or view it on GitHub.

ortegafernando commented 9 years ago

HI, I have try more time than I expected. Also add a funtion to get phone number and datetime strings for a SMS. Please, check it, and with my a great coupon !!!! (it has been a lot of work). Best regards

Date: Tue, 14 Oct 2014 19:00:27 -0700 From: notifications@github.com To: GPRS_Shield_Suli@noreply.github.com CC: op2op2op2@hotmail.com Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2)

hi,

    you can just follow the instruction of the two URL below

    https://help.github.com/articles/using-pull-requests/ 

    https://help.github.com/articles/creating-a-pull-request/ 

    If you are not familiar with the operation, you can also create your test repository firstly, and then pull request to it as a try.  Good luck!

lawliet.zou@gmail.com

From: op2op2op2

Date: 2014-10-15 03:17

To: Seeed-Studio/GPRS_Shield_Suli

CC: lawliet zou

Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2)

Ok, I will try and I will use your coupon .^_^.

I never pull changes to github, could you help me how to do it ?

best regards,

Date: Sun, 12 Oct 2014 19:47:41 -0700

From: notifications@github.com

To: GPRS_Shield_Suli@noreply.github.com

CC: op2op2op2@hotmail.com

Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2)

hi,

sorry for my late reply :)

Yes, it is really a good idea to get the SMS message as your secuence. Maybe a little complicated, but I think that it is strong enough to work normally.

and, er... would you like to finish your Read SMS function for this library and then pull request to the github? If your answer is yes, then I could offer you some coupon in our bazzar .^_^.

Thanks for your attention & contribution again. Seeed is very appreciative of your efforts and sharing. We need and love makers like you, open hardware movement needs makers like you.

Reply to this email directly or view it on GitHub. =

Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub. =

lawliet004 commented 9 years ago

hi,

   Thanks for your contribution sincerely,  would you like to tell me your email? We will inform you the coupon via email and it should be the early next month. 

lawliet.zou@gmail.com

From: op2op2op2 Date: 2014-10-19 04:57 To: Seeed-Studio/GPRS_Shield_Suli CC: lawliet zou Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2) HI, I have try more time than I expected. Also add a funtion to get phone number and datetime strings for a SMS. Please, check it, and with my a great coupon !!!! (it has been a lot of work). Best regards

Date: Tue, 14 Oct 2014 19:00:27 -0700 From: notifications@github.com To: GPRS_Shield_Suli@noreply.github.com CC: op2op2op2@hotmail.com Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2)

hi,

you can just follow the instruction of the two URL below

https://help.github.com/articles/using-pull-requests/

https://help.github.com/articles/creating-a-pull-request/

If you are not familiar with the operation, you can also create your test repository firstly, and then pull request to it as a try. Good luck!

lawliet.zou@gmail.com

From: op2op2op2

Date: 2014-10-15 03:17

To: Seeed-Studio/GPRS_Shield_Suli

CC: lawliet zou

Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2)

Ok, I will try and I will use your coupon .^_^.

I never pull changes to github, could you help me how to do it ?

best regards,

Date: Sun, 12 Oct 2014 19:47:41 -0700

From: notifications@github.com

To: GPRS_Shield_Suli@noreply.github.com

CC: op2op2op2@hotmail.com

Subject: Re: [GPRS_Shield_Suli] GSM recieving problem (#2)

hi,

sorry for my late reply :)

Yes, it is really a good idea to get the SMS message as your secuence. Maybe a little complicated, but I think that it is strong enough to work normally.

and, er... would you like to finish your Read SMS function for this library and then pull request to the github? If your answer is yes, then I could offer you some coupon in our bazzar .^_^.

Thanks for your attention & contribution again. Seeed is very appreciative of your efforts and sharing. We need and love makers like you, open hardware movement needs makers like you.

Reply to this email directly or view it on GitHub. =

Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub. = — Reply to this email directly or view it on GitHub.

ortegafernando commented 9 years ago

Hi, I am working in your library, but where is my coupon ?

I have more things to publish in your library.

And i have also TWO IMPORTANT QUESTIONS about your library.

One: Why have you use in all your function "0" to say "true" and "-1" to say "false" ? It is just the opossite to "true or false" constants in arduino enviroment. Is it something that I dont know that made -1 and 0 usefull ?

Second: Why do you use "int" as data returned for all functions ? Could it be byte to save SRAM ? Is it something that I don't know that made the use of ints in all function better ?

I hope your answer and my coupon.

Best regards

KillingJacky commented 9 years ago

Answered you with email.

ortegafernando commented 9 years ago

I don't know why my emails are been rejected. Any way, i will write here:

"Thanks, what about these two questions ?

Best regards,"

ortegafernando commented 9 years ago

Could you close this issue ?