bool GPRS::readSMS(int messageIndex, char *message,int length)
{
    int i = 0;
    char gprsBuffer[100];
    //char cmd[16];
	char num[4];
    char *p,*s;
    
    sim900_check_with_cmd("AT+CMGF=1\r\n","OK\r\n",CMD);
    delay(1000);
	sim900_send_cmd("AT+CMGR=");
	itoa(messageIndex, num, 10);
	sim900_send_cmd(num);
	sim900_send_cmd("\r\n");
//  sprintf(cmd,"AT+CMGR=%d\r\n",messageIndex);
//    sim900_send_cmd(cmd);
    sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
    sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),DEFAULT_TIMEOUT);
    if(NULL != ( s = strstr(gprsBuffer,"+CMGR:"))){
        if(NULL != ( s = strstr(s,"\r\n"))){
            p = s + 2;
            while((*p != '\r')&&(i < length-1)) {
                message[i++] = *(p++);
            }
            message[i] = '\0';
            return true;
        }
    }
    return false;   
}
int GPRS::send(const char * str, int len)
{
    //char cmd[32];
	char num[4];
    if(len > 0){
        //snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
		//sprintf(cmd,"AT+CIPSEND=%d\r\n",len);
		sim900_send_cmd("AT+CIPSEND=");
		itoa(len, num, 10);
		sim900_send_cmd(num);
		if(!sim900_check_with_cmd("\r\n",">",CMD)) {
        //if(!sim900_check_with_cmd(cmd,">",CMD)) {
            return 0;
        }
        /*if(0 != sim900_check_with_cmd(str,"SEND OK\r\n", DEFAULT_TIMEOUT * 10 ,DATA)) {
            return 0;
        }*/
        delay(500);
        sim900_send_cmd(str);
        delay(500);
        sim900_send_End_Mark();
        if(!sim900_wait_for_resp("SEND OK\r\n", DATA, DEFAULT_TIMEOUT * 10, DEFAULT_INTERCHAR_TIMEOUT * 10)) {
            return 0;
        }        
    }
    return len;
}
 bool GPRS::delBookEntry(int index)
{
	char indexStr[4];
	itoa(index, indexStr, 10);
	sim900_flush_serial();
    sim900_send_cmd(F("AT+CPBW="));
    sim900_send_cmd(indexStr);
    return sim900_check_with_cmd(F("\r"),"OK\r\n",CMD);	
}
bool GPRS::callUp(char *number)
{
  if(!sim900_check_with_cmd("AT+COLP=1\r\n","OK\r\n",CMD)) {
      return false;
  }
  delay(1000);
  sim900_send_cmd("ATD");
  sim900_send_cmd(number);
  sim900_send_cmd(";\r\n");
  return true;
}
bool GPRS::deleteSMS(int index)
{
    //char cmd[16];
	char num[4];
    //sprintf(cmd,"AT+CMGD=%d\r\n",index);
    sim900_send_cmd("AT+CMGD=");
	itoa(index, num, 10);
	sim900_send_cmd(num);
	//snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
    //sim900_send_cmd(cmd);
    //return 0;
    // We have to wait OK response
	//return sim900_check_with_cmd(cmd,"OK\r\n",CMD);
	return sim900_check_with_cmd("\r","OK\r\n",CMD);	
}
bool GPRS::callUp(char *number)
{
    //char cmd[24];
    if(!sim900_check_with_cmd("AT+COLP=1\r\n","OK\r\n",CMD)) {
        return false;
    }
    delay(1000);
	//HACERR quitar SPRINTF para ahorar memoria ???
    //sprintf(cmd,"ATD%s;\r\n", number);
    //sim900_send_cmd(cmd);
	sim900_send_cmd("ATD");
	sim900_send_cmd(number);
	sim900_send_cmd(";\r\n");
    return true;
}
bool GPRS::join()
{
    char cmd[64];
    char ipAddr[32];
    //Select multiple connection
    //sim900_check_with_cmd("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD);

    //set APN
    snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
    sim900_check_with_cmd(cmd, "OK", DEFAULT_TIMEOUT,CMD);

    //Brings up wireless connection
    sim900_check_with_cmd("AT+CIICR\r\n","OK",DEFAULT_TIMEOUT,CMD);

    //Get local IP address
    sim900_send_cmd("AT+CIFSR\r\n");
    sim900_read_buffer(ipAddr,32,2);

    if(NULL != strstr(ipAddr,"AT+CIFSR")) {
        _ip = str_to_ip(ipAddr+12);
        if(_ip != 0) {
            return true;
        }
    }
    return false;
}
int GPRS::readSMS(int messageIndex, char *message, int length, char *phone, char *datetime)  
{
  /* Response is like:
  AT+CMGR=2
  
  +CMGR: "REC READ","XXXXXXXXXXX","","14/10/09,17:30:17+08"
  SMS text here
  
  So we need (more or lees), 80 chars plus expected message length in buffer. CAUTION FREE MEMORY
  */

    int i = 0;
    char gprsBuffer[80 + length];
    char cmd[16];
    char *p,*p2,*s;
    
    sim900_check_with_cmd("AT+CMGF=1\r\n","OK\r\n",CMD);
    suli_delay_ms(1000);
    sprintf(cmd,"AT+CMGR=%d\r\n",messageIndex);
    sim900_send_cmd(cmd);
    sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
    sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),DEFAULT_TIMEOUT);
    
    if(NULL != ( s = strstr(gprsBuffer,"+CMGR:"))){
        // Extract phone number string
        p = strstr(s,",");
        p2 = p + 2; //We are in the first phone number character
        p = strstr((char *)(p2), "\"");
        if (NULL != p) {
            i = 0;
            while (p2 < p) {
                phone[i++] = *(p2++);
            }
            phone[i] = '\0';            
        }
        // Extract date time string
        p = strstr((char *)(p2),",");
        p2 = p + 1; 
        p = strstr((char *)(p2), ","); 
        p2 = p + 2; //We are in the first date time character
        p = strstr((char *)(p2), "\"");
        if (NULL != p) {
            i = 0;
            while (p2 < p) {
                datetime[i++] = *(p2++);
            }
            datetime[i] = '\0';
        }        
        if(NULL != ( s = strstr(s,"\r\n"))){
            i = 0;
            p = s + 2;
            while((*p != '\r')&&(i < length-1)) {
                message[i++] = *(p++);
            }
            message[i] = '\0';
        }
        return 0;
    }
    return -1;    
}
bool GPRS::getSubscriberNumber(char *number)
{
	//AT+CNUM								--> 7 + CR = 8
	//+CNUM: "","+628157933874",145,7,4		--> CRLF + 45 + CRLF = 49
	//										-->
	//OK									--> CRLF + 2 + CRLF = 6

    byte i = 0;
    char gprsBuffer[65];
    char *p,*s;
	sim900_flush_serial();
    sim900_send_cmd("AT+CNUM\r\n");
    sim900_clean_buffer(gprsBuffer,65);
    sim900_read_buffer(gprsBuffer,65,DEFAULT_TIMEOUT);
	//Serial.print(gprsBuffer);
    if(NULL != ( s = strstr(gprsBuffer,"+CNUM:"))) {
        s = strstr((char *)(s),",");
        s = s + 2;  //We are in the first phone number character 
        p = strstr((char *)(s),"\""); //p is last character """
        if (NULL != s) {
            i = 0;
            while (s < p) {
              number[i++] = *(s++);
            }
            number[i] = '\0';
        }
        return true;
    }  
    return false;
}
bool GPRS::getDateTime(char *buffer)
{
	//AT+CCLK?						--> 8 + CR = 9
	//+CCLK: "14/11/13,21:14:41+04"	--> CRLF + 29+ CRLF = 33
	//								
	//OK							--> CRLF + 2 + CRLF =  6

    byte i = 0;
    char gprsBuffer[50];
    char *p,*s;
	sim900_flush_serial();
    sim900_send_cmd("AT+CCLK?\r");
    sim900_clean_buffer(gprsBuffer,50);
    sim900_read_buffer(gprsBuffer,50,DEFAULT_TIMEOUT);
    if(NULL != ( s = strstr(gprsBuffer,"+CCLK:"))) {
        s = strstr((char *)(s),"\"");
        s = s + 1;  //We are in the first phone number character 
        p = strstr((char *)(s),"\""); //p is last character """
        if (NULL != s) {
            i = 0;
            while (s < p) {
              buffer[i++] = *(s++);
            }
            buffer[i] = '\0';            
        }
        return true;
    }  
    return false;
}
bool GPRS::getSignalStrength(int *buffer)
{
	//AT+CSQ						--> 6 + CR = 10
	//+CSQ: <rssi>,<ber>			--> CRLF + 5 + CRLF = 9						
	//OK							--> CRLF + 2 + CRLF =  6

	byte i = 0;
	char gprsBuffer[26];
	char *p, *s;
	char buffers[4];
	sim900_flush_serial();
	sim900_send_cmd("AT+CSQ\r");
	sim900_clean_buffer(gprsBuffer, 26);
	sim900_read_buffer(gprsBuffer, 26, DEFAULT_TIMEOUT);
	if (NULL != (s = strstr(gprsBuffer, "+CSQ:"))) {
		s = strstr((char *)(s), " ");
		s = s + 1;  //We are in the first phone number character 
		p = strstr((char *)(s), ","); //p is last character """
		if (NULL != s) {
			i = 0;
			while (s < p) {
				buffers[i++] = *(s++);
			}
			buffers[i] = '\0';
		}
		*buffer = atoi(buffers);
		return true;
	}
	return false;
}
bool GPRS::getVcc(char *buffer)
{
	//AT+CBC            --> 6 + CR
	//+CBC: 0,100,4241	--> CRLF + 16 + CRLF
	//
	//OK			    --> CRLF + 2 + CRLF
	
    byte i = 0;
    char gprsBuffer[50];
    char *p,*s;
	sim900_flush_serial();
    sim900_send_cmd(F("AT+CBC\r"));
    sim900_clean_buffer(gprsBuffer,50);
    sim900_read_buffer(gprsBuffer,50,DEFAULT_TIMEOUT);
    if(NULL != ( s = strstr(gprsBuffer,"+CBC:"))) {
        s = strstr((char *)(s),",");
        s = s + 1; 
        s = strstr((char *)(s), ","); 
        s = s + 1; //We are in the first Vcc character
        p = s + 4; //p is last character
        if (NULL != s) {
            i = 0;
            while (s < p) {
              buffer[i++] = *(s++);
            }
            buffer[i] = '\0';            
        }
        return true;
    }  
    return false;
}
  bool GPRS::getDateTime(char *buffer)
  {
  //AT+CCLK?						--> 8 + CRLF = 10
  //+CCLK: "14/11/13,21:14:41+04"   --> 29+ CRLF = 31
  //								--> CRLF     =  2
  //OK

    byte i = 0;
    char gprsBuffer[46];
    char *p,*s;
    sim900_send_cmd("AT+CCLK?\r\n");
    sim900_clean_buffer(gprsBuffer,43);
    sim900_read_buffer(gprsBuffer,43,DEFAULT_TIMEOUT);
    if(NULL != ( s = strstr(gprsBuffer,"+CCLK:"))) {
      s = strstr((char *)(s),"\"");
        s = s + 1;  //We are in the first phone number character
        p = strstr((char *)(s),"\""); //p is last character """
        if (NULL != s) {
          i = 0;
          while (s < p) {
            buffer[i++] = *(s++);
          }
          buffer[i] = '\0';
        }
        //We are going to flush serial data until OK is recieved
        return sim900_wait_for_resp("OK\r\n", CMD);
      }
      return false;
    }
bool GPRS::getLocation(const __FlashStringHelper *apn, float *longitude, float *latitude)
{    	
	int i = 0;
    char gprsBuffer[80];
	char buffer[20];
    char *s;
    
	//send AT+SAPBR=3,1,"Contype","GPRS"
	sim900_check_with_cmd("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r","OK\r\n",CMD);
	//sen AT+SAPBR=3,1,"APN","GPRS_APN"
	sim900_send_cmd("AT+SAPBR=3,1,\"APN\",\"");
	if (apn) {
      sim900_send_cmd(apn);
    }
    sim900_check_with_cmd("\"\r","OK\r\n",CMD);
	//send AT+SAPBR =1,1
	sim900_check_with_cmd("AT+SAPBR=1,1\r","OK\r\n",CMD);
	
	//AT+CIPGSMLOC=1,1
	sim900_flush_serial();
	sim900_send_cmd("AT+CIPGSMLOC=1,1\r");
	sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));	
	sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),2*DEFAULT_TIMEOUT,6*DEFAULT_INTERCHAR_TIMEOUT);
	//Serial.println(gprsBuffer);
    
	if(NULL != ( s = strstr(gprsBuffer,"+CIPGSMLOC:")))
	{
		s = strstr((char *)s, ",");
		s = s+1;
		//Serial.println(*s);
		i=0;
		while(*(++s) !=  ',')
			buffer[i++]=*s;
		buffer[i] = 0;
		*longitude = atof(buffer);
		       
		i=0;
		while(*(++s) !=  ',')
			buffer[i++]=*s;
		buffer[i] = 0;
		*latitude = atof(buffer);            
		return true;
	}
	return false;
}
//Here is where we ask for APN configuration, with F() so we can save MEMORY
bool GPRS::join(const __FlashStringHelper *apn, const __FlashStringHelper *userName, const __FlashStringHelper *passWord)
{
	byte i;
    char *p, *s;
    char ipAddr[32];
    //Select multiple connection
    //sim900_check_with_cmd("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD);

    //set APN. OLD VERSION
    //snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
    //sim900_check_with_cmd(cmd, "OK\r\n", DEFAULT_TIMEOUT,CMD);
    sim900_send_cmd("AT+CSTT=\"");
    if (apn) {
      sim900_send_cmd(apn);
    }
    sim900_send_cmd("\",\"");
    if (userName) {
      sim900_send_cmd(userName);
    }
    sim900_send_cmd("\",\"");
    if (passWord) {
      sim900_send_cmd(passWord);
    }
    sim900_check_with_cmd("\"\r\n", "OK\r\n", CMD);
    

    //Brings up wireless connection
    sim900_check_with_cmd("AT+CIICR\r\n","OK\r\n", CMD);

    //Get local IP address
    sim900_send_cmd("AT+CIFSR\r\n");
    sim900_clean_buffer(ipAddr,32);
    sim900_read_buffer(ipAddr,32);
	//Response:
	//AT+CIFSR\r\n       -->  8 + 2
	//\r\n				 -->  0 + 2
	//10.160.57.120\r\n  --> 15 + 2 (max)   : TOTAL: 29 
	//Response error:
	//AT+CIFSR\r\n       
	//\r\n				 
	//ERROR\r\n
    if (NULL != strstr(ipAddr,"ERROR")) {
		return false;
	}
    s = ipAddr + 12;
    p = strstr((char *)(s),"\r\n"); //p is last character \r\n
    if (NULL != s) {
        i = 0;
        while (s < p) {
            ip_string[i++] = *(s++);
        }
        ip_string[i] = '\0';            
    }
    _ip = str_to_ip(ip_string);
    if(_ip != 0) {
        return true;
    }
    return false;
} 
String GPRS::getImei(){
    char buf[32];
    String imei(NULL);
    sim900_send_cmd("AT+GSN\r\n");
    sim900_read_buffer(buf,sizeof(buf),DEFAULT_TIMEOUT);
    String response(buf);
    if(strstr(buf,"OK") != NULL)
        imei = response.substring(10, 25);
    return imei;
};
int GPRS::callUp(char *number)
{
    char cmd[24];
    if(0 != sim900_check_with_cmd("AT+COLP=1\r\n","OK",DEFAULT_TIMEOUT,CMD)) {
        return -1;
    }
    suli_delay_ms(1000);
    sprintf(cmd,"ATD%s;\r\n", number);
    sim900_send_cmd(cmd);
    return 0;
}
bool GPRS::ntpSyncDateTime(int8_t timezone)
{
    // e.g. "-105\0", this is not a valid value to pass to the SIM module, but we need to make sure not
    // to cross boundaries of the char array
    char timezoneString[5] = {'\0'};

    if (sim900_check_with_cmd(F("AT+CNTPCID=1\r\n"), "OK\r\n", CMD) == false)
        return false;

    sim900_send_cmd(F("AT+CNTP=\"pool.ntp.org\","));

    itoa(timezone, timezoneString, 10);

    sim900_send_cmd(timezoneString);

    if (sim900_check_with_cmd(F("\r\n"), "OK\r\n", CMD) == false)
        return false;

    return sim900_check_with_cmd(F("AT+CNTP\r\n"), "+CNTP: 1", CMD);
}
bool GPRS::sendSMS(char *number, char *data)
{
    //char cmd[32];
    if(!sim900_check_with_cmd("AT+CMGF=1\r\n", "OK\r\n", CMD)) { // Set message mode to ASCII
        return false;
    }
    delay(500);
	sim900_send_cmd("AT+CMGS=\"");
	sim900_send_cmd(number);
    //sprintf(cmd,"AT+CMGS=\"%s\"\r\n", number);
	//snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number);
//    if(!sim900_check_with_cmd(cmd,">",CMD)) {
    if(!sim900_check_with_cmd("\"\r\n",">",CMD)) {
        return false;
    }
    delay(1000);
    sim900_send_cmd(data);
    delay(500);
    sim900_send_End_Mark();
    return true;
}
bool GPRS::sendSMS(char *number, char *data)
{
  // Set message mode to ASCII
  if(!sim900_check_with_cmd("AT+CMGF=1\r\n", "OK\r\n", CMD)) {
    return false;
  }

  delay(500);
  sim900_send_cmd("AT+CMGS=\"");
  sim900_send_cmd(number);

  if(!sim900_check_with_cmd("\"\r\n",">",CMD)) {
      return false;
  }

  delay(1000);
  sim900_send_cmd(data);
  delay(500);
  sim900_send_End_Mark();
  return true;
}
//Overload with F() macro to SAVE memory
bool GPRS::connect(Protocol ptl,const __FlashStringHelper *host, const __FlashStringHelper *port, int timeout, int chartimeout)
{
    //char cmd[64];
    char resp[96];

    //sim900_clean_buffer(cmd,64);
    if(ptl == TCP) {
        sim900_send_cmd(F("AT+CIPSTART=\"TCP\",\""));   //%s\",%d\r\n",host, port);
    } else if(ptl == UDP) {
        sim900_send_cmd(F("AT+CIPSTART=\"UDP\",\""));   //%s\",%d\r\n",host, port);
    } else {
        return false;
    }
    sim900_send_cmd(host);
    sim900_send_cmd(F("\","));
    sim900_send_cmd(port);
    sim900_send_cmd(F("\r\n"));
//	Serial.print("Connect: "); Serial.println(cmd);
    sim900_read_buffer(resp, 96, timeout, chartimeout);
//	Serial.print("Connect resp: "); Serial.println(resp);    
    if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
        return true;
    }
    return false;
}
bool GPRS::sendUSSDSynchronous(char *ussdCommand, char *resultcode, char *response)
{
	//AT+CUSD=1,"{command}"
	//OK
	//
	//+CUSD:1,"{response}",{int}

	byte i = 0;
    char gprsBuffer[200];
    char *p,*s;
    sim900_clean_buffer(response, sizeof(response));
	
	sim900_flush_serial();
    sim900_send_cmd("AT+CUSD=1,\"");
    sim900_send_cmd(ussdCommand);
    sim900_send_cmd("\"\r");
	if(!sim900_wait_for_resp("OK\r\n", CMD))
		return false;
    sim900_clean_buffer(gprsBuffer,200);
    sim900_read_buffer(gprsBuffer,200,DEFAULT_TIMEOUT);
    if(NULL != ( s = strstr(gprsBuffer,"+CUSD: "))) {
        *resultcode = *(s+7);
		resultcode[1] = '\0';
		if(!('0' <= *resultcode && *resultcode <= '2'))
			return false;
		s = strstr(s,"\"");
        s = s + 1;  //We are in the first phone number character
        p = strstr(s,"\""); //p is last character """
        if (NULL != s) {
            i = 0;
            while (s < p) {
              response[i++] = *(s++);
            }
            response[i] = '\0';            
        }
		return true;
	}
	return false;
}
 bool GPRS::addBookEntry(int index, const char* number, int type, const char *name)
{
	char num[4];
     sim900_send_cmd(F("AT+CPBW="));
	if( index != -1 )
	{
		itoa(index, num, 10);
		sim900_send_cmd(num);
	}
	sim900_send_cmd(F(",\""));
	sim900_send_cmd(number);
	sim900_send_cmd(F("\","));
	itoa(type, num, 10);
	sim900_send_cmd(num);
	sim900_send_cmd(F(",\""));
	sim900_send_cmd(name);
	sim900_send_cmd(F("\""));
	return sim900_check_with_cmd(F("\r"),"OK\r\n",CMD);	
}
  bool GPRS::connect(Protocol ptl,const char * host, int port, int timeout)
  {
    //char cmd[64];
   char num[4];
   char resp[96];

    //sim900_clean_buffer(cmd,64);
   if(ptl == TCP) {
    sim900_send_cmd("AT+CIPSTART=\"TCP\",\"");
    sim900_send_cmd(host);
    sim900_send_cmd("\",");
    itoa(port, num, 10);
    sim900_send_cmd(num);
    sim900_send_cmd("\r\n");
//        sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n",host, port);
  } else if(ptl == UDP) {
    sim900_send_cmd("AT+CIPSTART=\"UDP\",\"");
    sim900_send_cmd(host);
    sim900_send_cmd("\",");
    itoa(port, num, 10);
    sim900_send_cmd(num);
    sim900_send_cmd("\r\n");

	//        sprintf(cmd, "AT+CIPSTART=\"UDP\",\"%s\",%d\r\n",host, port);
  } else {
    return false;
  }

  delay(2000);
    //sim900_send_cmd(cmd);
  sim900_read_buffer(resp,96,timeout);

//Serial.print("Connect resp: "); Serial.println(resp);
    if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
      return true;
    }
    return false;
  }
bool GPRS::is_connected(void)
{
    char resp[96];
    sim900_send_cmd("AT+CIPSTATUS\r\n");
    sim900_read_buffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
    if(NULL != strstr(resp,"CONNECTED")) {
        //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CONNECTED"
        return true;
    } else {
        //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CLOSED"
        //+CIPSTATUS: 0,,"","","","INITIAL"
        return false;
    }
}
boolean GPRS::send(const char * str)
{
	if(!sim900_check_with_cmd(F("AT+CIPSEND\r\n"),">",CMD)) {
		return false;
	}
	delay(500);
	sim900_send_cmd(str);
	delay(500);
	sim900_send_End_Mark();
	if(!sim900_wait_for_resp("SEND OK\r\n", DATA, DEFAULT_TIMEOUT * 10, DEFAULT_INTERCHAR_TIMEOUT * 10)) {
		return false;
	}   
    return true;
}
int GPRS::isSMSunread()
{
    char gprsBuffer[64];  //64 is enough to see +CMGL:
    char *p,*s;
    //List of all UNREAD SMS and DON'T change the SMS UNREAD STATUS
    sim900_send_cmd("AT+CMGL=\"REC UNREAD\",1\r\n");
    /*If you want to change SMS status to READ you will need to send:
          AT+CMGL=\"REC UNREAD\"\r\n
      This command will list all UNREAD SMS and change all of them to READ
      
     If there is not SMS, response is (26 chars)
         AT+CMGL="REC UNREAD",1
         OK
     If there is SMS, response is like (>64 chars)
         AT+CMGL="REC UNREAD",1
         +CMGL: 9,"REC UNREAD","XXXXXXXXX","","14/10/16,21:40:08+08"
         Here SMS text.
         OK  
         
         or

         AT+CMGL="REC UNREAD",1
         +CMGL: 9,"REC UNREAD","XXXXXXXXX","","14/10/16,21:40:08+08"
         Here SMS text.
         +CMGL: 10,"REC UNREAD","YYYYYYYYY","","14/10/16,21:40:08+08"
         Here second SMS        
         OK           
    */
    sim900_clean_buffer(gprsBuffer,64);
    sim900_read_buffer(gprsBuffer,64,DEFAULT_TIMEOUT); 
    
    if(NULL != ( s = strstr(gprsBuffer,"OK"))) {
        //In 64 bytes "doesn't" fit whole +CMGL: response, if recieve only "OK"
        //    means you don't have any UNREAD SMS
        return 0;
    } else {
        if(NULL != ( s = strstr(gprsBuffer,"+CMGL:"))) {
            //There is at least one UNREAD SMS, get index/position
            p = strstr((char *)gprsBuffer,":");
            if (p != NULL) {
                //We are going to flush serial data until OK is recieved
                sim900_wait_for_resp("OK", DEFAULT_TIMEOUT, CMD);
                return atoi(p+1);
            }
        } else {
            return 0; 
        }
    } 
    return -1;
}
bool GPRS::openBearer(const __FlashStringHelper *apn)
{
    const uint8_t checkBearerRetryCount = 3;
    int i;

    //send AT+SAPBR=3,1,"Contype","GPRS"
    if (sim900_check_with_cmd(F("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r"),"OK\r\n", CMD) == false)
        return false;

    //send AT+SAPBR=3,1,"APN","<apn>"
    sim900_send_cmd(F("AT+SAPBR=3,1,\"APN\",\""));
    if (apn) {
      sim900_send_cmd(apn);
    }

    if (sim900_check_with_cmd(F("\"\r"),"OK\r\n", CMD) == false)
        return false;

    //send AT+SAPBR =1,1
    if (sim900_check_with_cmd(F("AT+SAPBR=1,1\r\n"),"OK\r\n", CMD) == false)
        return false;

    for(i = 0; i < checkBearerRetryCount; i++)
    {
        // 3 means "closed"
        uint8_t bearerStatus = 3;
        // 1 means "connected"
        queryBearer(&bearerStatus);
        if (bearerStatus == 1)
            break;
    }

    if (i >= checkBearerRetryCount)
        return false;

    return true;
}
bool GPRS::sendSMS(const char *number, const char *data)
{

	//180822 In the init function
	//if(!sim900_check_with_cmd(F("AT+CMGF=1\r\n"), "OK\r\n", CMD)) { // Set message mode to ASCII
    //    return false;
    //}
    //delay(500);
	
	sim900_flush_serial();
	sim900_send_cmd(F("AT+CMGS=\""));
	sim900_send_cmd(number);
    //sprintf(cmd,"AT+CMGS=\"%s\"\r\n", number);
	//snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number);
//    if(!sim900_check_with_cmd(cmd,">",CMD)) {
    if(!sim900_check_with_cmd(F("\"\r\n"),">",CMD)) {
        return false;
    }
    delay(1000);
    sim900_send_cmd(data);
    delay(500);
    sim900_send_End_Mark();
    return sim900_wait_for_resp("OK\r\n", CMD, 20);
}
boolean GPRS::send(const __FlashStringHelper* str)
{
    if(!sim900_check_with_cmd(F("AT+CIPSEND\r\n"),">",CMD)) {
        return false;
    }

    sim900_send_cmd(str);
    sim900_send_End_Mark();

    if(!sim900_wait_for_resp("SEND OK\r\n", DATA, DEFAULT_TIMEOUT, DEFAULT_INTERCHAR_TIMEOUT)) {
        return false;
    }

    return true;
}