Run Code Based on Time From GSM Module (SIM900)

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:

AT+CCLK?

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

OK

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
  delay(500);
}

void loop() {

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

SendTextMessage();


}

///GetTime
void GetTime()
{
    SIM900.print("AT+CCLK?");
}

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

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

You have a function that sends the command to the modem. You have no code to read the modem's response. You have no way to tie a response to a specific command. You have no code to save the modem's response in an array. You have no code to parse the stored data, to see if it is in response to the CCLK command. You have no code to parse the time from the rest of the response.

So, I'd have to say that the answer to your question is "You can't". Yet, anyway.

I got my code all working now, but there is one glitch. After detecting motion the first time when the time range is right my code gets hung up the on the second sensing of motion and doesn’t text me.

Why is my code getting hung up the second time it tries to send a text message? Any suggestions would be great.

Serial Output:

No Motion
No Motion
No Motion
No Motion
No Motion
No Motion
No Motion
Motion Detected!
Inside of time
No Motion
No Motion
No Motion
No Motion
No Motion
No Motion
No Motion
No Motion
No Motion
No Motion
No Motion
Motion Detected!

Current code:

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

int time1 = 6; // time to disable sending of txt
int time2 = 18; // time to enable sending of txt

byte sens1 = 3; // input pin for sensor 1
byte sens2 = 5; // input pin for sensor 2
byte sens3 = 6; // input pin for sensor 3
byte indicator = 13; // LED indicator pin


void setup()
{
  pinMode(sens1,INPUT);
  pinMode(sens2,INPUT);
  pinMode(sens3,INPUT);
  pinMode(indicator,OUTPUT);
  
  PowerOn(); //power on GSM Module
  
  GPRS.begin(19200); // software serial for GSM Shield
  Serial.begin(19200); // serial monitor 
  delay(3000); //delay 3 seconds
}

/////////////////////////////////////////
void loop()
{
  byte sens1state = digitalRead(sens1); // read high/low state of sensor 1
  byte sens2state = digitalRead(sens2); // read high/low state of sensor 2
  byte sens3state = digitalRead(sens3); // read high/low state of sensor 3

  // if any of three sensors are in high state, motion is being detected
  if(sens1state==1 || sens2state==1 || sens3state==1){
    digitalWrite(indicator,true);
    Serial.println("Motion Detected!");
    delay(500);
    
    if(TimeCheckForTXT()){
      Serial.println("Inside of time");
      delay(1000);
      SendTextMessage();
      delay(3000); // wait 3 seconds before sensing again, limit number of txt
    } else {
      Serial.println("Outside of time");
    }
    
  }
  // if all three sensors are in low state, motion is not being detected
  else if (sens1state==0 && sens2state==0 && sens3state==0){
    digitalWrite(indicator,false);
    Serial.println("No Motion");
  } else {
    digitalWrite(indicator,false);
    Serial.println("Error");
  }
  delay(500); // delay motion sensing for half second
}
//////////////////////////////////////////

///TimeCheckForTXT()
///Checks to see if time is within desired range from cell network
bool TimeCheckForTXT() {
  char c1, c2;
  int hour = 0;
  byte readMore = true;
  GPRS.listen();      // discard old input
  GPRS.write("AT+CCLK?\r"); // send clock command
  while (readMore) {
    while (!GPRS.available()) {}; // wait for a character
    char c = GPRS.read();
    if ( c == ',' ) {
      c1 = GPRS.read();
      c2 = GPRS.read();
      
      hour = ((c1 - '0') *10) + (c2 - '0');
      
      readMore = false;
    }
    delay(5);
  }
  return (hour >= time2 || hour < time1 );
}

///SendTextMessage()
/// Sends a sms message to desired phone number
void SendTextMessage()
{
  GPRS.print("AT+CMGF=1\r");    //puts SIM900 in text mode
  delay(100);
  GPRS.println("AT + CMGS = \"+12137005555\"");//send sms message, country code required
  delay(100);
  GPRS.println("Motion Detected!");//message
  delay(100);
  GPRS.println((char)26);//the ASCII code of the ctrl+z is 26
  delay(100);
  GPRS.println();
}

///PowerOn()
///Software power on of GSM module
void PowerOn() 
{
  ///software equivalent of pressing the GSM shield "power" button
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(7000);
}

I was able to get the code working by changing GPRS.listen(); with a function below.

///GPRSFlush()
///Clears GPRS serial data
void GPRSFlush(){
  while(GPRS.available() > 0) {
    char t = GPRS.read();
  }
}