  * This function request the GPS information from SIM808, parse information in global 
  * variables and finally print the information at Serial interface for debugging.
  ****************************************************************************************/
-void GetGPSLocation(){
+int GetGPSLocation(){
 char infoLocation[BUFFER_SIZE];  
 posBuf=0;
 int i = 0;
-
-  //Get position
-  while(GPRS.available()){
-    GPRS.read();
-  } 
-  GPRS.println("AT+CGPSINF=2");
-   
-  //Read response of SIM808 at Serial Port, Uno buffer all data without other activities
-  //otherwise it may lose some bytes 
-  //after send the command to GPS, it takes about 100ms to all serial information arrives UNO   
+int j=0;
+
+//  //waking up GPS after sleep
+  sim808_check_with_cmd("AT+CGPSPWR=1\r\n","OK\r\n",CMD); 
+  delay(1000);
+  sim808_check_with_cmd("AT+CGPSRST=1\r\n","OK\r\n",CMD); 
+  delay(1000);
+
+  if(!wait_GPS_connect(25)){ //timeout of 25 seconds to acquire signal  
+    sim808_check_with_cmd("AT+CGPSPWR=0\r\n","OK\r\n",CMD); 
+    return NO_GPS_DATA_AVAILABLE;
+  }
+
+
+  sim808_clean_buffer(infoLocation,BUFFER_SIZE);
+  
+  sim808_send_cmd("AT+CGPSINF=2\r\n");
   delay(50);
-   
-
-  //for(int i=0;i<7;i++){
-  while((posBuf<=99)&&(i<15)){ 
-    while((GPRS.available()>0)){
-      infoLocation[posBuf++]=GPRS.read();
-    }
-    delay(10);
-    i++;
-    //delay(15); 
-  }
-  GPSAnalyzer(infoLocation);
-
-  //Get Speed of SIM808 from GPS
+  if(sim808.readable()) {
+    sim808_read_buffer(infoLocation,BUFFER_SIZE,DEFAULT_TIMEOUT);
+  }
+  Serial.println("buffer2:");
+  Serial.println(infoLocation);
+  
+  
+//  while(GPRS.available()){
+//    GPRS.read();
+//  } 
+//  GPRS.println("AT+CGPSINF=2");
+//   
+//  //Read response of SIM808 at Serial Port, Uno buffer all data without other activities
+//  //otherwise it may lose some bytes 
+//  //after send the command to GPS, it takes about 100ms to all serial information arrives UNO   
+//  delay(50);
+   
+
+//  //for(int i=0;i<7;i++){
+//  while((posBuf<=99)&&(i<15)){ 
+//    while((GPRS.available()>0)){
+//      infoLocation[posBuf++]=GPRS.read();
+//    }
+//    delay(10);
+//    i++;
+//    //delay(15); 
+//  }
+
+  if(!GPSAnalyzer(infoLocation)){
+    //sim808_clean_buffer(infoLocation,BUFFER_SIZE);
+    sim808_check_with_cmd("AT+CGPSPWR=0\r\n","OK\r\n",CMD);     
+    return NO_GPS_DATA_AVAILABLE;
+  }
+
+
+  //debug until here
+
+
+
+
+  sim808_clean_buffer(infoLocation,BUFFER_SIZE);
+   //Get Speed of SIM808 from GPS
   while(GPRS.available()){
     GPRS.read();
   } 
   GPRS.println("AT+CGPSINF=32");
-   
-  //Read response of SIM808 at Serial Port, Uno buffer all data without other activities
-  //otherwise it may lose some bytes 
-  //after send the command to GPS, it takes about 100ms to all serial information arrives UNO   
   delay(50);
-   
-
-/*  posBuf=0;
-  i=0;
-  //for(int i=0;i<7;i++){
-  while((posBuf<=99)&&(i<15)){ 
-    while((GPRS.available()>0)){
-      infoLocation[posBuf++]=GPRS.read();
+  
+  sim808_read_buffer(infoLocation,BUFFER_SIZE,DEFAULT_TIMEOUT); 
+  GPSAnalyzer32(infoLocation);
+
+  //Send collected data to mysql server 
+  Serial.println("SendError:");
+  Serial.println(SendGPSLocation());
+  //createSMSMessage(smsMessage,latitude, longitude, MSL_altitude, Speed);
+
+  //put GPS to sleep
+  sim808_check_with_cmd("AT+CGPSPWR=0\r\n","OK\r\n",CMD);     
+  //delay(5000);
+  return 0; //success 
+}
+
+
+/****************************************************************************
+ * wait_GPS_connect(int seconds)
+ * wait GPS acquire signal
+ ****************************************************************************/
+boolean wait_GPS_connect(int timeout){
+  unsigned long timerStart;    
+  timerStart = millis();
+
+  sim808_flush_serial();
+  delay(1000);
+  
+  while (1){
+    sim808_clean_buffer(gprsBuffer,64); 
+    sim808_send_cmd("AT+CGPSSTATUS?\r\n");
+    if(sim808.readable()) {
+      sim808_read_buffer(gprsBuffer,64,DEFAULT_TIMEOUT);
     }
-    delay(10);
-    i++;
-    //delay(15); 
-  }*/
-  
-  sim808_clean_buffer(infoLocation,sizeof(infoLocation));
-  sim808_read_buffer(infoLocation,sizeof(infoLocation),DEFAULT_TIMEOUT,DEFAULT_INTERCHAR_TIMEOUT); 
-  GPSAnalyzer32(infoLocation);
-
-  //Send collected data to mysql server 
-  SendGPSLocation();
-  //createSMSMessage(smsMessage,latitude, longitude, MSL_altitude, Speed);
-}
-
+
+    Serial.println(gprsBuffer);
+    if(strstr(gprsBuffer,"3D Fix")){
+      sim808_clean_buffer(gprsBuffer,64); 
+      sim808_flush_serial();
+      return true;
+     }
+    if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
+      sim808_clean_buffer(gprsBuffer,64); 
+      sim808_flush_serial();
+      return false;
+    }
+
+    delay(10000);
+    //sim808_flush_serial();
+  }
+ }
+ 
 
 /*void resetBuffer() 
 {

  if(sim808.readable()) {
    sim808_read_buffer(gprsBuffer,64,DEFAULT_TIMEOUT);
    //Serial.print(gprsBuffer);
    if(currentRadioSig <= 7) {
         Serial.println("NOGPRSsig");
         sim808_send_cmd("AT+CDNSGIP=\"ardugps.hopto.org\"\r\n");
         previousRadioSig = currentRadioSig;                
         return NO_GPRS_SIGNAL;            //error of weak signal or DNS failed                     
    }else{                                 //radio signal > 7
      if(previousRadioSig > 7){            //signal still good
        previousRadioSig = currentRadioSig;                
      }else {                              //signal recovered
        error = InitGPRS();
        previousRadioSig = currentRadioSig;
        if(error > 0) { //initGPRS failed
          Serial.print("GPRSfail: ");
          Serial.println(error);
          return error;        
        } 
          Serial.println("GprsInitOK");       
      }            
    }
  }

    
  //get the local ip number
  //GPRS.println("AT+CIFSR");
  sim808_send_cmd("AT+CIFSR\r\n");  
  sim808_send_cmd("AT+CDNSGIP=\"ardugps.hopto.org\"\r\n");
  
  //start a connection to TCP to URL and port 
  if (prot == TCP){
    if(!SendCIPSTART(TCP)){
      delay(1);
      //Serial.println("AT+CIPSTART => FAIL"); 
    } else {
      delay(1);
      //Serial.println("AT+CIPSTART => OK");
    }
  }else {
    if(!SendCIPSTART(UDP)){
      delay(1);
      //Serial.println("AT+CIPSTART => FAIL"); 
    } else {
      delay(1);
      //Serial.println("AT+CIPSTART => OK");    
    }
  }
  

  

  
//  //Receiving call 
//  if(sim808.readable()){
//    sim808_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
//    //Serial.print("LoopBuf:");
//    //Serial.println(gprsBuffer);
//  
//    //**** Detect the current state of the telephone or SMS ****************
//    if(NULL != strstr(gprsBuffer,"RING")) {
//        sim808.answer(); 
//        createSMSMessage(smsMessage,latitude, longitude, MSL_altitude, Speed);
//        if(extractCallingNumber(gprsBuffer)){
//          sim808.sendSMS(phone,smsMessage);
//        }else{
//          //Serial.println("No CLIP to send SMS");
//        }
//        delay(2000);
//        sim808_send_cmd("AT+CTTS=2,\"hello,欢迎使用语音合系统\"\r\n");            
//  }
//
//  //check if GPRS signal still good
//  if(NULL != (s = strstr(gprsBuffer,"CDNSGIP: 0,8"))) {
//    Serial.println("No signal or DNS fault loop");
//    DNS_OK = false; 
//  }        
//  

  //Receive SMS message   
//  if(NULL != (s = strstr(gprsBuffer,"+CMTI: \"SM\""))) { //SMS: $$+CMTI: "SM",24$$
