0

Can't read SMS <data> from storage
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Can't read SMS <data> from storage

by sahillyard on Sun Feb 25, 2018 6:06 pm

I am using the FONA Sim 808 with an Arduino Mega. I am trying to poll the modem to see if a SMS message has arrived and then load that message into a char array for parsing. When I attempt to read the message from memory, only the header is read into the array. The data is missing. Because the messages (commands to the Arduino) come infrequently, I only need the message from slot number 1 and I will erase it--and any other messages in memory--after it is loaded into the array. (I can read the data if I by-pass storage and pass it directly to the TE (Arduino) (AT+CNMI=2,2,0,0,0).


Here is my code:

Code: Select all | TOGGLE FULL SIZE

#include <string.h>

#define GSM_MODEM Serial2
#define GSM_RESET_PIN 4
#define MONITOR Serial

char smsTxt [40];

void setup() {
 pinMode (17, INPUT);  //Serial2 from GSM modem
  pinMode (16, OUTPUT);  //Serial2 to GSM Modem
  pinMode (GSM_RESET_PIN, INPUT_PULLUP);

MONITOR.begin(9600);  //IDE Monitor
  delay(200);
  GSM_MODEM.begin(9600); //GSM Shield wired to Serial2
  delay(200);

/*INitiallize the GSM modem for sending and recieving sms text messages */

delay (1000);  //delay for GSM autobaud to get stabalized

  GSM_MODEM.println ("AT\r"); 
  delay (1000);
  modemPrintToMonitor(); //Checks modem response
 
  GSM_MODEM.print ("AT+IPR=9600\r"); //Set modem to 9600 baud
  delay (1000);
  modemPrintToMonitor();  //Checks modem response
 

//  GSM setup ///

    GSM_MODEM.println ("AT+CGNSPWR = 1\r");  //Power on the GPS
    delay (1000);
 
    GSM_MODEM.println ("AT+CSMP=17,167,0,0\r");
    delay (1000); 
   
    GSM_MODEM.println ("AT+CPMS=\"SM\",\"SM\",\"SM\"\r"); //Set storage to SIM memory
    delay (1000); 

    GSM_MODEM.println ("AT+CMGF=1\r");  //SMS msg as text
    delay (1000);
    modemPrintToMonitor();  //Checks modem response

    GSM_MODEM.println ("AT+CSCS=\"GSM\"\r"); // Tell GSM modem to send 7 bit ascii to TE
    delay(1000);
     modemPrintToMonitor();  //Checks modem response

//    GSM_MODEM.println ("AT+CSCA?\r"); // returned; "+12063130004",145
//    AT+CSCA =<sca>.[,<tosca>); //Service Cente Address 3CPP page 38
 
    GSM_MODEM.println ("AT+CSDH=1\r"); //Include all header info — doesn’t work
    delay (1000);
    modemPrintToMonitor ();

    GSM_MODEM.println ("AT+CNMI=0,0,0,0,0\r"); //place  SMS messages into memory and do not send received alerts
    delay (1000);
    modemPrintToMonitor ();

//    GSM_MODEM.println ("AT+CSAS = 0\r");  //Save SMS cmds
//    delay (1000);


//    GSM_MODEM.println ("AT+CRES = 0\r");  //Retrives stored SMS cmds
//    delay (1000);
 
     
//    GSM_MODEM.println ("AT&W\r"); //Saves current settings to mememory
//    delay (1000);

}
////////////////////////////////////LOOP BEGINS ///////////////////////////
void loop()
{   
 
MONITOR.println ("Into Loop");
      GSM_MODEM.println ("AT+CMGR=1\r"); // Get sms message in memory slot 1
MONITOR.println (GSM_MODEM.available()); // Print size of result from previous command
      delay (1000);
   
   if (GSM_MODEM.available()>20)  // If size of result code indicates more that modem “Ok” proceed
      {
MONITOR.println ("INTO READ") ; // Identify place in program for debugging
      char incomingTxt [110];  //create array for sms text
      char *inTxt = incomingTxt;  //create pointer to buffer
      GSM_MODEM.readBytes(inTxt,100);  //read from  modem to array
        GSM_MODEM.flush (); // wait until SMS text is completely transferred
        inTxt = '\0';      // Insert NUL at end of SMS text     
      MONITOR.println (incomingTxt);  // Print text array to debug monitor
        delay (1000);
MONITOR.println ("\n\nPast print incoming text") ;  //Identify place in code for debugging
    GSM_MODEM.println ("AT+CMGD=1,4\r");  // Erase all message in storage
     }
     else
     {
     modemPrintToMonitor (); //Clear OK from GSM_Modem if size of result doesn’t indicate that a SMS message
              //was received
     }
  }

///////////////////////// LOOP ENDS///////////////////////////

/******************Modem Print to monitor**************/

/*This function reads the output of Serial 2, the GSM modem reply
 * and prints it out on the monitor
 */
void modemPrintToMonitor ()
{

  if (GSM_MODEM.available())
  {
    while (GSM_MODEM.available())  //Increase the prior delay to try and capture the modem's response
   MONITOR.write (GSM_MODEM.read());

  }


Here is the printout from the debug monitor:
[img]
Into Loop
0
INTO READ

+CMGR: "REC UNREAD","+18312383860","","18/02/24,17:55:33-32",


Past print incoming text
Into Loop
0

OK

OK
Into Loop
[/img]







Can someone identify my problem?

Thanks!

sahillyard
 
Posts: 5
Joined: Fri Dec 08, 2017 10:25 pm

Re: Can't read SMS <data> from storage

by adafruit_support_mike on Tue Feb 27, 2018 2:19 am

AT+CNMI only stores the messages indicating an SMS has arrived.

Try doing AT+CMGL to see what items are in the preferred storage location you set with AT+CPMS

adafruit_support_mike
 
Posts: 50680
Joined: Thu Feb 11, 2010 2:51 pm

Re: Can't read SMS <data> from storage

by sahillyard on Wed Feb 28, 2018 8:05 pm

Thanks Mike!

I did as you suggested and queried the modem with AT+CMGL. With +CNMI = 0,0,0,0,0 the modem returns the header but still no data:

OK
Into Loop
Serial available: 6

OK
Into Loop
Serial available: 23

OK

+CMTI: "SM",1
Into Loop
Serial available: 63
INTO READ

+CMGL: 1,"REC UNREAD","+18312383860","","18/02/28,15:11:53-32


Past print incoming text
Into Loop
Serial available: 12

Then, I tried the same AT+CMGL with the AT+CNMI = 2,2,0,0,0 and got both the header and the data (11002 in this case):

OK
Into Loop
Serial available: 6

OK
Into Loop
Serial available: 63
INTO READ

OK

+CMT: "+18312383860","","18/02/28,15:18:52-32"
11002



Past print incoming text
Into Loop
Serial available: 12

Note that the modem transmitted the message with +CMT meaning that it passed the message through without responding to the AT+CMGL command, and the message didn't go into storage before it was sent to the TE. (I would just use the pass through of the message and be done with the problem. However, I need to use the GPS data as well as communicate with the unit using SMS, and when a SMS unsolicited response code comes barreling through uninvited, it corrupts the GPS stream and sometimes puts the modem into a continuous loop. So, I need to poll the modem and retrieve the message from storage.) I understand that there may be an compression issue, but I don't know how to solve that either.

Steve

sahillyard
 
Posts: 5
Joined: Fri Dec 08, 2017 10:25 pm

Re: Can't read SMS <data> from storage

by sahillyard on Tue Mar 06, 2018 8:45 pm

HI Mike,

Did you have a moment to consider my latest post?

I just noticed that the modem reported a time zone of -32. I am in -8, so the report is 24 hours off. Interesting.

Steve

sahillyard
 
Posts: 5
Joined: Fri Dec 08, 2017 10:25 pm

Re: Can't read SMS <data> from storage

by adafruit_support_mike on Wed Mar 07, 2018 2:39 am

What specific issue are you still trying to solve? I got the impression that you were getting SMS messages as you wanted.

adafruit_support_mike
 
Posts: 50680
Joined: Thu Feb 11, 2010 2:51 pm

Re: Can't read SMS <data> from storage

by sahillyard on Mon Mar 12, 2018 4:19 pm

Mike,

Sorry for the delay in responding; the folks around here think I should pay more attention to moving house than this project.

I am trying to retrieve sms messages stored in the SIM 808 memory. I can get the message headers, but not the content, or "data".

To put this into context, this small routine will be integrated into a much larger sketch that recovers operating data and implements a geo-fence reporting scheme for a boat. SMS messages are used for sending commands and reporting vessel operating parameters.

Thanks again,

Steve

sahillyard
 
Posts: 5
Joined: Fri Dec 08, 2017 10:25 pm

Re: Can't read SMS <data> from storage

by adafruit_support_mike on Mon Mar 12, 2018 10:45 pm

Okay, how is this:

Then, I tried the same AT+CMGL with the AT+CNMI = 2,2,0,0,0 and got both the header and the data (11002 in this case):

not doing what you want?

adafruit_support_mike
 
Posts: 50680
Joined: Thu Feb 11, 2010 2:51 pm

Re: Can't read SMS <data> from storage

by sahillyard on Tue Mar 13, 2018 2:47 pm

Mike,

Thanks--you guys are great. I see my error in communication.

Although I was using AT+CMGL at the time, I was also using AT=CNMI=2,2,0,0,0. As expected, the sms unsolicited response code went directly from the cell network to the TE as opposed to going to SIM 808 storage and then being retrieved by the CMGL command. The TE received: +CMT: "+18312383860","","18/02/28,15:18:52-32"11002, which includes the data: 11002. However, note the "+CMT" indicating that the incoming data stream was not in response to the CMGL command. When I use AT+CNMI=0,0,0,0 to send the incoming unsolicited response code from the cell network to SIM 808 storage, the AT+CMGL command retrieves the incoming message header but not the data.

In the larger program of which the above code will be a part, unsolicited transmission of messages from the SIM 808 sms facility to the TE disrupt the TE's reading/processing of the the data from the SIM 808 gps facility. Therefore, I need to separate the two data streams in time. Thus, I need to set AT+CNMI to AT+CNMI=0,0,0,0,0 to send the incoming messages to SIM 808 storage and poll the SIM 808 and retrieve messages--including the data--using AT+CMGL or AT+CMGR--at least that is what I think I need to do. For some reason, under this configuration, the SIM 808 does not include the sms data when responding to either AT+CMGL or AT+CMGR. (In the larger program, I poll SIM 808 and receive the gps data, and I need to do the same with the sms data.)

Thanks again!

Steve

sahillyard
 
Posts: 5
Joined: Fri Dec 08, 2017 10:25 pm

Please be positive and constructive with your questions and comments.