Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a SIM900 module connected to an Arduino Uno, using an AT command for listing the SMS I get this output, how can i split this based on (,) to store each data in an array?

AT+CMGL="ALL"

+CMGL: 2,"REC READ","+639321576684","Ralph Manzano","16/04/25,21:51:33+32"
Yow!!!

OK
share|improve this question
    
Since arduino is very limited in memory and available flash, I suggest you to avoid trying to split the whole line in chunks, but rather just take the segments you need. If you can't, then it would still be better not to store it, but rather analyze the segments one by one while you read them. Anyway I suggest you to parse the whole line one char at a time and check it, but be careful that there is a comma also between the date and the time, so if you don't want it to be split you should also group the parameters according to the double quotes – frarugi87 Apr 26 at 8:35
up vote 4 down vote accepted

You could try to use strtok. Code from my mind and not tested:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char string[] = "AT+CMGL=\"ALL\"\n"
    "\n"
    "+CMGL: 2,\"REC READ\",\"+639321576684\",\"Ralph Manzano\",\"16/04/25,21:51:33+32\"\n"
    "Yow!!!\n"
    "\n"
    "OK\n";
    char delimiter[] = ",\n";

    // initialize first part (string, delimiter)
    char* ptr = strtok(string, delimiter);

    while(ptr != NULL) {
        printf("found one part: %s\n", ptr);
        // create next part
        ptr = strtok(NULL, delimiter);
    }

    return 1;
}

Outputs:

found one part: AT+CMGL="ALL"
found one part: +CMGL: 2
found one part: "REC READ"
found one part: "+639321576684"
found one part: "Ralph Manzano"
found one part: "16/04/25
found one part: 21:51:33+32"
found one part: Yow!!!
found one part: OK
share|improve this answer
    
thank you so mcuh! but the strings may vary depending on the message sent, who sent the message and when it was sent, how can i parse this? – Ralph Apr 26 at 10:41
    
@Ralph I updated my answer. You can have multiple delimter. I think you should be able to parse all the needed information out of the string you get ;) – salomonderossi Apr 26 at 10:55
    
ohhh thank you! does this mean ill just store the response of the at command in the string[] array? and ill be able to parse it despite the content? – Ralph Apr 26 at 11:03
    
@Ralph imho yes – salomonderossi Apr 26 at 11:05
    
sorry im really new to parsing, will definitely try this, thanks! – Ralph Apr 26 at 11:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.