I am trying to check the time from the GSM module to let my code run or do nothing.

If I send the command, AT+CCLK? to the SIM900. This is the response:


+CCLK: "17/02/14,16:58:25-20"


How do I parse the time information from the SIM900 and setup a loop for my code to run if it is past 6pm but before 6am?

My code currently:

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7,8); //RX,TX

void setup() {
  PowerOn(); //power on GSM Module
  delay(10000); //delay 10 seconds
  SIM900.begin(19200); //software serial for GSM Shield

void loop() {

// if time is past 6pm and before 6am send text message else do nothing



void GetTime()

void SendTextMessage()
    SIM900.print("AT+CMGF=1\r");//Because we want to send the SMS in text mode
    SIM900.println("AT + CMGS = \"+12137777777\"");//send sms message, be careful need to add a country code before the cellphone number
    SIM900.println("Motion Detected!");//the content of the message
    SIM900.println((char)26);//the ASCII code of the ctrl+z is 26

void PowerOn() 
  ///software equivalent of pressing the GSM shield "power" button
  digitalWrite(9, HIGH);
  digitalWrite(9, LOW);

1 Answer 1


I'm imagining SIM900 is a serial stream that supports input methods available() and read(). The hour portion of the response string is a two-digit integer delimited by a comma and a colon. The following snippets of code assume you trigger a response and then start a listen() to connect to the software serial input. Since listen() discards old data, presumably a message like +CCLK: "17/02/14,16:58:25-20" will be the next data to arrive.

This code converts any string of digits that it sees into a number. Whenever it sees a non-digit character, it treats it as a delimiter.

When the previous and current delimiters are ',' and ':', it checks if the current number value is in the desired time range, and if so, it sets a flag, else clears a flag.

When the previous and current delimiters are '-' and '"', it assumes it is done with the parse. Or one could, possibly, end the parse as soon as the hour field has been processed.

void loop() {
  // ...
  // somewhere within loop()
  byte num, dell;       // Currrent number and left delimiter
  byte timeFlag, readMore, foundTime;

  // Send string to elicit +CCLK and tell parser to handle response
  SIM900.listen();      // Discard old input
  SIM900.write("AT+CCLK?"); // Elicit new input
  readMore = true;
  timeFlag = foundTime = false;
  num = 0;
  dell = 0;
  // Read and parse +CCLK input; set or clear timeFlag
  while (readMore) {
    while (!SIM900.available()) {}; // Wait for a character
    char c = SIM900.read();
    if (c >= '0' && c <= '9') { // Is c a digit?
      num = 10*num + c-'0'; // Yes, add it to num
    } else {   // Not a digit, so check for Hour field or End of parse
      if (dell=='-' && c == '"') {
        readMore = false;
      } else
        if (dell==',' && c == ':') {
          timeFlag = (num < 6) || (num >= 18);
          foundTime = true;
      dell = c;            // Record left delimiter of next number
      num = 0;             // Get ready to evaluate next number
  // If foundTime, then timeFlag is true for time in 6 PM to 6 AM, else false
  // ...
  // etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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