Getting specific values from char or string

Hello, I'm using a SIM800 module for my project. I need to get the time from gsm in order to update my DS3231 rtc every time my board powers on because my custom made board doesn't have room for a coin-cell battery. From the fona libray, I can get a char like this:
"22/03/07,04:16:36+08". How can I get each value (day, month, hour etc.) and store it on different ints?
Probably I can't figure any way of using a delimiter because there 2 different symbols, the "/" and the ":".
How can this be done?

The strtok() function can take multiple delimiters.

Hello

Here is an example with sscanf : dfSzrI - Online C++ Compiler & Debugging Tool - Ideone.com

1 Like

A demo of the strtok function with your data:

char array[] = "22/03/07,04:16:36+08";
char *strings[10]; // an array of pointers to the pieces of the above array after strtok()
char *ptr = NULL;

void setup()
{
   Serial.begin(115200);
   //Serial.print(array);
   byte index = 0;
   ptr = strtok(array, "/,:+");  // delimiters space and comma
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, "/,:+");
   }
   //Serial.println(index);
   // print all the parts
   Serial.println("The Pieces separated by strtok()");
   for (int n = 0; n < index; n++)
   {
      Serial.print("piece ");
      Serial.print(n);
      Serial.print(" = ");
      Serial.println(strings[n]);
   }
   //22/03/07,04:16:36+08 
   int year = atoi(strings[0]);
   int month = atoi(strings[1]);
   int day = atoi(strings[2]);
   int hour = atoi(strings[3]);
   int minute = atoi(strings[4]);
   int second = atoi(strings[5]);
   int hundredth = atoi(strings[6]);

   Serial.println();
   Serial.println("Pieces converted to numeric variables (integers)");
   Serial.print("Year  >>  ");
   Serial.println(year);
   Serial.print("Month  >>  ");
   Serial.println(month);
   Serial.print("Day  >>  ");
   Serial.println(day);
   Serial.print("Hour  >>  ");
   Serial.println(hour);
   Serial.print("Minute  >>  ");
   Serial.println(minute);
   Serial.print("Second  >>  ");
   Serial.println(second);
   Serial.print("Hundredth  >>  ");
   Serial.println(hundredth);  
   Serial.println(); 
}

void loop()
{
   // step away, nothing to see here.
}

1 Like

Guys thank's a lot for your suggestions. Let me try and I'll report back to you!

Well, thank's again!

Guix's suggestion was working only with a const char. I was using a non const char.

GroundFungus's was the problem solving one!

Is the format and spacing always exactly the same? If so, then it would not be necessary to parse the text.

#include <TimeLib.h>
char data[] = "22/03/07,04:16:36+08";

void setup() {
  Serial.begin(115200);
  tmElements_t tm;
  
  tm.Year = CalendarYrToTm(2000 + (data[0] - '0') * 10 + (data[1] - '0'));
  tm.Month = (data[3] - '0') * 10 + (data[4] - '0');
  tm.Day = (data[6] - '0') * 10 + (data[7] - '0');
  tm.Hour = (data[9]-'0') * 10 + (data[10] - '0');
  tm.Minute = (data[12]-'0') * 10 + (data[13] - '0');
  tm.Second = (data[15]-'0') * 10 + (data[16] - '0');
  
  time_t t = makeTime(tm);
  Serial.println(data);
  if (month(t) < 10) Serial.print('0');
  Serial.print(month(t));
  Serial.print('/');
  if (day(t) < 10) Serial.print('0');
  Serial.print(day(t));
  Serial.print('/');
  Serial.print(year(t));
  Serial.print(' ');
  if (hour(t) < 10) Serial.print('0');
  Serial.print(hour(t));
  Serial.print(':');
  if (minute(t) < 10) Serial.print('0');
  Serial.print(minute(t));
  Serial.print(':');
  if (second(t) < 10) Serial.print('0');
  Serial.print(second(t));
  Serial.println();
}

void loop() {
}
1 Like

Yes exactly the same format each time.

It seems I solved this problem.
Thank you for your reply by the way!

It seems there's a misunderstanding.

The char I get from the SIM800 contains the (") at the start and the end of the array.
For example when I serial.print the char I get this: ."22/03/07,05:35:23+08"

In your examples you thought that I got this: 22/03/07,05:35:23+08

Everything works except the year. It doesn't show 22 as it should, but I get a 0 instead.

I'll try to tinker it a bit.

Ok, it's done. For everyone interested, I changed a part of groundFungus's code to this:

 byte index = 0;
   ptr = strtok(buffer, "/,:+\"");  // delimiters space and comma
   while (ptr != NULL){
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, "/,:+\"");
   }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.