works through pushing box to log data to google spreadsheet

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed

Fork of GPR_Interface by DCS_TEAM

Revision:
11:045cb766d9a5
Parent:
10:8c55dfcc9a7c
--- a/GPRS/GPRS.cpp	Fri Sep 25 04:10:56 2015 +0000
+++ b/GPRS/GPRS.cpp	Wed Oct 21 19:41:42 2015 +0000
@@ -32,6 +32,7 @@
     _userName = userName;
     _passWord = passWord;
     socketID = -1;
+    _phoneNumber = "12345678910";
 }
 
 bool GPRS::preInit()
@@ -61,13 +62,13 @@
     sendCmdAndWaitForResp("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
     wait(2);
      //printf("\032\n");
-    sendCmdAndWaitForResp("AT+CMGS=\"+18183977481\"\r\n",">",DEFAULT_TIMEOUT,CMD);
+    sendCmdAndWaitForResp("AT+CMGS=\"+17066311506\"\r\n",">",DEFAULT_TIMEOUT,CMD);
     printf("sent at cmgf\n");
     wait(2);
     //printf("032");
     sendCmd(IPAdress); // sends the address 
     sendCmd("\x1a");  // this is like pressing control - z to end the send command
-    wait(10);  // giving the send enough time to do its thing
+        wait(10);  // giving the send enough time to do its thing
     printf("should have been received");
 }
 
@@ -82,28 +83,31 @@
     wait(10);  
     sendCmd("AT+CMGR=1\r\n");
     storeResp();
-    char temp[30];
     return get_server_IP();
 }
 
 // end of what Noah Milam added
 bool GPRS::checkSIMStatus(void)
 {
+    printf("Checking SIM Status...\r\n");
     char gprsBuffer[32];
     int count = 0;
-    cleanBuffer(gprsBuffer,32);
+    
     while(count < 3) {
+        cleanBuffer(gprsBuffer,32);
         sendCmd("AT+CPIN?\r\n");
         readBuffer(gprsBuffer,32,DEFAULT_TIMEOUT);
         if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
             break;
         }
+        printf("SIM Not Ready..Try Again\r\n");
         count++;
         wait(1);
     }
     if(count == 3) {
         return false;
     }
+    printf("SIM Status GOOD!\r\n");
     return true;
 }
 
@@ -111,26 +115,114 @@
 {
     char cmd[64];
     char ipAddr[32];
+    char resp[96];
+    //get the phone number
+    printf(">>>>AT+CNUM\r\n");
+    sendCmd("AT+CNUM\r\n");
+    cleanBuffer(resp,96);
+    readBuffer(resp,96,DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"+CNUM:")) {
+        //if(storePhoneNumber(resp)){
+            printf("Successfull stored the phone number!\r\n");     
+        //}   
+    }
+    else{
+        return false;    
+    }
+    
+    printf(">>>>AT+CREG\r\n");
+    sendCmd("AT+CREG?\r\n");
+    cleanBuffer(resp,96);
+    readBuffer(resp,96,DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"+CREG:")) {
+    }
+    else{
+        return false;    
+    }
+    //close any existing connections
+    printf(">>>>AT+CIPSHUT\r\n");
+    sendCmd("AT+CIPSHUT\r\n");
+    cleanBuffer(resp,96);
+    readBuffer(resp,96,DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"OK")) {
+    }
+    else{
+        return false;    
+    }
     //Select multiple connection
-    sendCmdAndWaitForResp("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
-
+    printf(">>>>AT+CIPMUX=0\r\n");
+    sendCmd("AT+CIPMUX=0\r\n");
+    cleanBuffer(resp,96);
+    readBuffer(resp,96,DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"CIPMUX")) {
+    }
+    else{
+        return false;    
+    }
+    //get signal strength
+    printf(">>>>AT+CSQ\r\n");
+    sendCmd("AT+CSQ\r\n");
+    cleanBuffer(resp,96);
+    readBuffer(resp,96,DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"+CSQ:")) {
+    }
+    else{
+        return false;    
+    }
+    
+    //attach the device
+    printf(">>>>AT+CGATT=1\r\n");
+    sendCmd("AT+CGATT=1\r\n");
+    cleanBuffer(resp,96);
+    readBuffer(resp,96,DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"OK")) {
+    }
+    else{
+        return false;    
+    }
+    
     //set APN
     snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
-    sendCmdAndWaitForResp(cmd, "OK", DEFAULT_TIMEOUT,CMD);
-
+    printf(">>>>%s",cmd);
+    //sendCmdAndWaitForRes  p(cmd, "OK", DEFAULT_TIMEOUT,CMD);
+    sendCmd(cmd);
+    cleanBuffer(resp,96);
+    readBuffer(resp,96,DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"OK")) {
+    }
+    else{
+        return false;    
+    }
     //Brings up wireless connection
-    sendCmdAndWaitForResp("AT+CIICR\r\n","OK",DEFAULT_TIMEOUT,CMD);
-
+    printf(">>>>AT+CIICR\r\n");
+    sendCmd("AT+CIICR\r\n");
+    cleanBuffer(resp,96);
+    readBuffer(resp,96,DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"OK")) {
+    }
+    else{
+        return false;    
+    } 
+    
     //Get local IP address
+    printf(">>>>AT+CIFSR\r\n");
     sendCmd("AT+CIFSR\r\n");
     readBuffer(ipAddr,32,2);
-
+    printf(">>>>AT+CIFSR returns: %s\r\n", ipAddr);
     if(NULL != strstr(ipAddr,"AT+CIFSR")) {
         _ip = str_to_ip(ipAddr+12);
         if(_ip != 0) {
-            return true;
+            //set to quick sending mode
+            printf(">>>>AT+CIPQSEND=1\r\n");
+            sendCmd("AT+CIPQSEND=1\r\n");
+            cleanBuffer(resp,96);
+            readBuffer(resp,96,DEFAULT_TIMEOUT);
+            if(NULL != strstr(resp,"OK")) {
+                return true;
+            }
         }
     }
+    
     return false;
 }
 
@@ -151,14 +243,15 @@
         return false;
     }
     if(ptl == TCP) {
-        sprintf(cmd, "AT+CIPSTART=%d,\"TCP\",\"%s\",%d\r\n",socket, host, port);
+        sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n", host, port);
     } else if(ptl == UDP) {
         sprintf(cmd, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d\r\n",socket, host, port);
     } else {
         return false;
     }
+    
     sendCmd(cmd);
-    readBuffer(resp,96,2*DEFAULT_TIMEOUT);
+    readBuffer(resp,96,DEFAULT_TIMEOUT);
     if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
         return true;
     }
@@ -185,24 +278,23 @@
 
 bool GPRS::is_connected(int socket)
 {
+    
     char cmd[16];
     char resp[96];
-    snprintf(cmd,16,"AT+CIPSTATUS=%d\r\n",socket);
-    sendCmd(cmd);
-    readBuffer(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;
+    while(1){
+        snprintf(cmd,16,"AT+CIPSTATUS\r\n");
+        sendCmd(cmd);
+        readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
+        if(NULL != strstr(resp,"CONNECTED")) {
+            return true;
+        } else {
+            return false;
+        }
     }
 }
 
 void GPRS::reset()
 {
-
 }
 
 bool GPRS::close(int socket)
@@ -271,12 +363,15 @@
     char cmd[32];
     wait(1);
     if(len > 0){
-        snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",socket);
-        if(0 != sendCmdAndWaitForResp(cmd,">",2,CMD)) {
+        snprintf(cmd,sizeof(cmd),"AT+CIPSEND\r\n");
+        if(0 != sendCmdAndWaitForResp(cmd,">",1,CMD)) {
+            
             return false;
         }
         sendCmd(str);
+        printf("Sending: %s\r\n", str);
         serialModem.putc((char)0x1a);
+        
     }
     return len;
 }
@@ -320,3 +415,30 @@
     }
     return ip;
 }
+bool GPRS::storePhoneNumber(char* atCNumValue){
+    char* leftDelimiter = "+";
+    char* rightDelimiter = "\"";
+    // find the left delimiter and use it as the beginning of the substring
+    const char* beginning = strstr(atCNumValue, leftDelimiter);
+    if(beginning == NULL)
+        return 1; // left delimiter not found
+
+    // find the right delimiter
+    const char* end = strstr(atCNumValue, rightDelimiter);
+    if(end == NULL)
+        return 2; // right delimiter not found
+
+    // offset the beginning by the length of the left delimiter, so beginning points _after_ the left delimiter
+    beginning += strlen(leftDelimiter);
+
+    // get the length of the substring
+    ptrdiff_t segmentLength = end - beginning;
+
+    // allocate memory and copy the substring there
+    //*_phoneNumber = (char*)malloc(segmentLength + 1);
+    strncpy(_phoneNumber, beginning, segmentLength);
+    (_phoneNumber)[segmentLength] = 0;
+    
+    printf("Phone Number: %s\r\n", _phoneNumber);
+    return 0; // success!
+}
\ No newline at end of file