How to take date/time data from SIM 900?

Hello guys

So I have a project to display time and temperature data logger, which also could sent SMS when there was any temperature rise

Instead of using a RTC module, I used RTC attached to SIM 900. My problem is:

  1. How to take date/time data from AT+CCLK command and print the timestamp to the LCD screen (yeah i used LCD screen as a display monitor for user)?

Here is my experiment code, which inspired from this post Data processing. Getting time and date. - Syntax & Programs - Arduino Forum

#include <LiquidCrystal.h>
#include "SIM900.h"
#include <SoftwareSerial.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
SoftwareSerial mySerial(2, 3); // RX, TX
int i=0;

#define CCLK_MSG_LEN 28  // total characters in the CCLK message
#define TIME_STR_LEN 18  // the actual number of characters for date and time

char timeStr[TIME_STR_LEN]; // holds the time string
char timePreamble[] = "CCLK: \"" ; // there is a quotation mark in the string so we need the C escape char

void setup(){
  lcd.begin(16, 2);  
 Serial.begin(9600);
 Serial.println("GSM Shield testing.");
 gsm.begin(9600);
  //Start configuration of shield with baudrate.
  //For http uses is raccomanded to use 4800 or slower.
  mySerial.begin(9600);
  mySerial.println( "AT+CLTS=1");
 mySerial.println( "AT+CCLK =\"15/06/23,13:28:00+28\"");
 lcd.setCursor(0,0);
 lcd.print("Just Testing");
 mySerial.println( "AT+CCLK?" );
 }
 
boolean getTime(){
 if(mySerial.available() >= CCLK_MSG_LEN ){
   if(mySerial.read() == '+'){ // check if start of string
     for(int i=0; i < strlen(timePreamble); i++){
       if(mySerial.read() != timePreamble[i] )
         return false; // exit if the received data does not match the preamble 
     }
     // we are now at the start of the time data
     for(int i=i; i < TIME_STR_LEN; i++)
       timeStr[i] = mySerial.read();
     return true; // we have filled the timeString with valid data   
   }
 }   

 return false; // we dont have a full message starting with the CCLK preamble 
}

// converts two characters from date string into a two digit decimal value
int dateToDecimal(char * date){
 int value = ((date[0] - '0') * 10) +  (date[1] - '0');
 return value;
}

void loop()
{  
 int a, b, c,  d, e,  f;
 
 if(getTime() == true){
   a  = dateToDecimal(&timeStr[0]) ;
   b = dateToDecimal(&timeStr[3]) ;
   c   = dateToDecimal(&timeStr[6]) ;
   d  = dateToDecimal(&timeStr[9]) ;
   e   = dateToDecimal(&timeStr[12]) ;
   f   = dateToDecimal(&timeStr[15]) ;}

lcd.setCursor(0,1);
   lcd.print(c);
   lcd.print("/");
   lcd.print(b);
   lcd.print("/");
   lcd.print(a);
   lcd.print(" ");
   lcd.print(d);
   lcd.print(":");
   lcd.print(e); //this line of code use to create dd:mm:yy hh:mm

   delay(10000);}

but the current result from this code, my LCD screen shows unknown numbers 4357/257/0 659:4546

any idea why this thing happened? Thank you so much and sorry for my bad english :wink:

I think your problem is here:

for(int i=i; i < TIME_STR_LEN; i++)

Shouldn't 'i' start at zero?

johnwasser:
I think your problem is here:

for(int i=i; i < TIME_STR_LEN; i++)

Shouldn't 'i' start at zero?

thank you johnwasser, i already changed i=0 but the it still shows unknown sequence of numbers

i did some modification in code, to check where the error happens. I put 'else' condition after if statement

if(getTime() == true){
   a  = dateToDecimal(&timeStr[0]) ;
   b = dateToDecimal(&timeStr[3]) ;
   c   = dateToDecimal(&timeStr[6]) ;
   d  = dateToDecimal(&timeStr[9]) ;
   e   = dateToDecimal(&timeStr[12]) ;
   f   = dateToDecimal(&timeStr[15]) ;}
else Serial.println("There is something wrong");

and yes, "There is something wrong" is printed on Serial monitor. It seems like getTime() method return false value. And maybe there is something wrong with getTime() method

boolean getTime(){
 if(mySerial.available() >= CCLK_MSG_LEN ){
   if(mySerial.read() == '+'){ // check if start of string
     for(int i=0; i < strlen(timePreamble); i++){
       if(mySerial.read() != timePreamble[i] )
         return false; // exit if the received data does not match the preamble 
     }
     // we are now at the start of the time data
     for(int i=0; i < TIME_STR_LEN; i++)
       timeStr[i] = mySerial.read();
     return true; // we have filled the timeString with valid data   
   }
 }   

 return false; // we dont have a full message starting with the CCLK preamble 
}

any idea? :slight_smile:

getTime() will return false until at least 27 characters have arrived. It will also return false if the first character read is a '+' and the next seven characters don't match the preamble.

IF THE FIRST CHARACTER IS NOT A '+' it treats the next TIME_STR_LEN characters as a valid time string. There is a very good chance you are hitting a non-'+' character and reading the wrong part of the string.

Try this:

boolean getTime(){
  // Check for available bytes
 if(mySerial.available() < CCLK_MSG_LEN )
    return false;  // Not enough characters for a full message.  Try again later

   if(mySerial.read() != '+')
     return false;  // Not in sync yet

     // In sync.  Check the preamble
     for(int i=0; i < strlen(timePreamble); i++) {
       if(mySerial.read() != timePreamble[i] )
         return false; // exit if the received data does not match the preamble 
     }

     // Preamble checked.  We are now at the start of the time data
     for(int i=0; i < TIME_STR_LEN; i++)
       timeStr[i] = mySerial.read();
     return true; // we have filled the timeString with valid data 
}

johnwasser thank you very much :slight_smile: i already use the code, and finally get the right result for each value.

but there is still something weird about it. It only get right in a few second. Later on it shows unknown value (again)

I tried to print the result in serial, here is the result:

There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
date components are
15
6
25
10
18
22
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
date components are
15
6
25
-385
-385
-385
date components are
15
6
25
10
19
15
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932
There is something wrong
date components are
0
926
404
4357
669
3932

maybe there is something wrong about my baud rate or loop? since the right result only shown in few times