Help needed to split incoming SMS

Hi everyone,

I need some help please.

I am writing a program that sends and receive SMS’s. The problem is to spilt the
incomming SMS in the following detail after being read from the simcard:

// Incomming SMS detail
// Year =
// Month =
// Day =
// Hour =
// Min =
// Sec =
// MessageReceived =
// NumberFrom =

SYSTEM_MODE(MANUAL);
#define TIMEOUT 10000
#define CTRL_Z 26
#define MAX_PHONE_NUMBER    14
char szPhoneNumber[MAX_PHONE_NUMBER] = "278344444";
char szCmd[64];
char szReturn[32] = "";
char szMessage[64];
char pMessage[64] = "Test from Electron";


int callback(int type, const char* buf, int len, char* param){
//    Serial.print("Return: ");
    Serial.write((const uint8_t*)buf, len);
//    Serial.println();

// Incomming SMS detail
// Year =
// Month =
// Day =
// Hour =
// Min =
// Sec
// Message
// Number

    return WAIT;
}


void setup() {
  Serial.begin(9600);
  Cellular.on ();
  Cellular.connect();
  delay(30000);
  Serial.println("AT+CMGF=1\r\n");


}

void loop() {

  Serial.println("############################################");
  Serial.println("AT+CMGR=1?\r\n");
  Cellular.command(callback, szReturn, TIMEOUT, "AT+CMGR=1\r\n");
  Serial.println("############################################");
  delay(2000);

}

Thanks

2 Likes

Thanks.

1 Like

Hi,

To everyone,

After some in depth debugging for a week and steep learning curve for a Basic programmer I think
i have a good piece of code for someone that would like to use a third party simcards and
use AT commands. I am stuck to get the message part of the sms into a string… ?

All help will be appreciated.

SYSTEM_MODE(MANUAL);
#define TIMEOUT 10000
#define CTRL_Z 26
#define MAX_PHONE_NUMBER    14
char szPhoneNumber[MAX_PHONE_NUMBER] = "270000000";
char szCmd[64];
char szReturn[32] = "";
//String szReturn;
char szMessage[64];
char pMessage[64] = "Test from Ecletron";
int waitforresultsinmseconds = 0;

// Results of AT+CSQ
int  csqin1;      // First indicator
int  csqin2;      // Second indicator
// Results of AT+CMGR





int callback2(int type, const char* buf, int len, char* param){
    Serial.write((const uint8_t*)buf, len);
    return WAIT;
}



int callback(int type, const char* buf, int len, char* param){
      Serial.write((const uint8_t*)buf, len);

        // Try to get results from AT+CSQ command
        for( int a = 1; a < 200 ; a = a + 1 ) {
          if(strncmp(&buf[2], "+CSQ:", 5) == 0){
            char csq1[5];
            char csq2[5];;
            // Serial.write((const uint8_t*)&buf[2], len - 2);
            sscanf(&buf[2],"%6s%[^','],%s",szReturn,csq1,csq2);
            String sStr1 = csq1;
            csqin1 = sStr1.toInt();
            String sStr2 = csq2;
            csqin2 = sStr2.toInt();
            Serial.println(csqin1);
            Serial.println(csqin2);
          a = 1000;   //Stop waiting for correct results
          }
          // Wait for CSQ results


          // Wait for CMGR results
          if(strncmp(&buf[2], "+CMGR", 5) == 0){
            delay (1000);
            char cmgrcellnumber [20];
            char cmgryear [5];
            char cmgrmonth [3];
            char cmgrday [3];
            char cmgrhour [3];
            char cmgrmin [3];
            char cmgrsec [3];
            char cmgrmessage [164];
            Serial.println("#Total output as string");
            String String3 = &buf[2];
            String3 = String3 + '\0';
            Serial.write(String3);
            //Serial.println(sStr3);


          //  Serial.write((const uint8_t*)buf, len);
            // up to +0.8   Header ,       "      27... "      ,       '       "      17     /       07     /       02     '       10     :       42     :       19        +08     "
            sscanf(String3,"%[^',']%*1[',']%*1[\"]%[^\"]%*1[\"]%*1[',']%*1[',']%*1[\"]%[^'/']%*1['/']%[^'/']%*1['/']%[^',']%*1[',']%[^':']%*1[':']%[^':']%*1[':']%[^'+']%*[^\"]%*[\"]%[]",szReturn,cmgrcellnumber,cmgryear,cmgrmonth,cmgrday,cmgrhour, cmgrmin, cmgrsec,cmgrmessage);


            //Serial.println("#Meaasge as string#");
            //String sStr3 = cmgrmessage;
            //Serial.println(sStr3);
            Serial.println("#Header#");
            Serial.println(szReturn);
            Serial.println("#cellnumber#");
            Serial.println(cmgrcellnumber);
            Serial.println("#year#");
            Serial.println(cmgryear);
            Serial.println("#month#");
            Serial.println(cmgrmonth);
            Serial.println("#day#");
            Serial.println(cmgrday);
            Serial.println("#hour#");
            Serial.println(cmgrhour);
            Serial.println("#min#");
            Serial.println(cmgrmin);
            Serial.println("#sec#");
            Serial.println(cmgrsec);
            Serial.println("#message#");
            Serial.println(cmgrmessage);
          a = 1000;   //Stop waiting for correct results
          }
        }
return WAIT;
}


void setup() {
  Serial.begin(9600);
  Cellular.on ();
  Cellular.connect();
}

void loop() {
//  Cellular.command(callback, szReturn, TIMEOUT, "AT+CSQ\r\n");
  Cellular.command(callback, szReturn, TIMEOUT, "AT+CMGR=1\r\n");

  delay(5000);

}

If you want your code to run stable for a long time I’d advise against the use of String objects and stick with trusted ol’ char[] strings.

When you say, you have troubles with getting the message into a string, what are these troubles exactly?

I can read the number, year… up to sec into variables and print them to the screen. The message part I am battling to “clean up from gibbersh” before and after the Message “Qwert”

Without having a close look at your code I’d just guess a missing '\0' string terminator at the end of your string or an indexing issue in your buffers (including overrun).

You should check the return value of sscanf() which would tell you how many fields actually could be filled and only print out the ones that were successfully filled.
If you also could show what the raw String3 content was.