Final Code 12/19/2015
Dependencies: Chemical_Sensor_DMA GPRS DPG_FINAL MBed_Adafruit-GPS-Library SDFileSystem Socket mbed
Diff: GPRS/GPRS.cpp
- Revision:
- 21:3d922bea5d77
- Parent:
- 19:404594768414
- Child:
- 22:f46576c40722
--- a/GPRS/GPRS.cpp Wed Oct 21 19:46:32 2015 +0000 +++ b/GPRS/GPRS.cpp Fri Nov 06 20:53:59 2015 +0000 @@ -22,6 +22,7 @@ #include "mbed.h" #include "GPRS.h" +#include "GSMLibrary.h" GPRS* GPRS::inst; char* numbers[100]; @@ -106,7 +107,7 @@ if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { break; } - printf("SIM Not Ready..Try Again\r\n--%s--\r\n", gprsBuffer); + printf("SIM Not Ready..Try Again--%s--\r\n", gprsBuffer); count++; wait(1); } @@ -114,7 +115,28 @@ return false; } printf("SIM Status GOOD!\r\n"); + + //get the phone number + char resp[96]; + printf(">>>>AT+CNUM\r\n"); + sendCmd("AT+CNUM\r\n"); + cleanBuffer(resp,96); + readBuffer(resp,96,DEFAULT_TIMEOUT); + if(NULL != strstr(resp,"+CNUM:")) { + printf("Response: -%s-", resp); + const char *p1 = strstr(resp, ",\"")+2; + const char *p2 = strstr(p1, "\""); + size_t len = p2-p1; + phoneNumber = (char*)malloc(sizeof(char)*(len+1)); + strncpy(phoneNumber, p1, len); + phoneNumber[len] = '\0'; + } + else{ + printf("Response: -%s-", resp); + return false; + } return true; + } bool GPRS::join() @@ -123,13 +145,28 @@ char ipAddr[32]; char resp[96]; - printf(">>>>AT+CREG\r\n"); + printf(">>>>AT+CREG?\r\n"); sendCmd("AT+CREG?\r\n"); cleanBuffer(resp,96); readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"+CREG:")) { + if(NULL != strstr(resp,"+CREG: 0,1")) { + printf("Response: -%s-", resp); } else{ + printf("Response: -%s-", resp); + return false; + } + + //check if need to attach the device + printf(">>>>AT+CGATT?\r\n"); + sendCmd("AT+CGATT?\r\n"); + cleanBuffer(resp,96); + readBuffer(resp,96,DEFAULT_TIMEOUT); + if(NULL != strstr(resp,"+CGATT: 1")) { + printf("Response: -%s-", resp); + } + else{ + printf("Response: -%s-", resp); return false; } //close any existing connections @@ -142,91 +179,113 @@ else{ return false; } - //Select multi connection (it can be a server or client) - 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"); + //check the ip Status + printf(">>>>AT+CIPSTATUS\r\n"); + sendCmd("AT+CIPSTATUS\r\n"); cleanBuffer(resp,96); readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"+CSQ:")) { + if(NULL != strstr(resp,"GPRSACT")) { + //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,".")) { + _ip = str_to_ip(ipAddr+12); + if(_ip != 0) { + return true; + } + } + } + else if(NULL != strstr(resp,"INITIAL")){ + printf("Response: -%s-", resp); + + } + else if(NULL != strstr(resp,"CONNECT OK")){ + printf("Response: -%s-", resp); + + } + else if(NULL != strstr(resp,"START")){ + + //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,".")) { + _ip = str_to_ip(ipAddr+12); + if(_ip != 0) { + return true; + } + } } else{ + printf("Response: -%s-", resp); return false; } - //check if device is attached - printf(">>>>AT+CGATT?\r\n"); - sendCmd("AT+CGATT?\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"+CGATT:")) { - } - else{ - return false; - } - //attach the device - printf(">>>>AT+CGATT=1\r\n"); - sendCmd("AT+CGATT=1\r\n"); + + //single connection + printf(">>>>AT+CIPMUX=0\r\n"); + sendCmd("AT+CIPMUX=0\r\n"); cleanBuffer(resp,96); readBuffer(resp,96,DEFAULT_TIMEOUT); if(NULL != strstr(resp,"OK")) { + printf("Response: -%s-", resp); } else{ - printf("Response: %s\r\n", resp); + printf("Response: -%s-", resp); return false; } //set APN snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord); printf(">>>>%s",cmd); - //sendCmdAndWaitForRes p(cmd, "OK", DEFAULT_TIMEOUT,CMD); + //sendCmdAndWaitForResp(cmd, "OK", DEFAULT_TIMEOUT,CMD); sendCmd(cmd); cleanBuffer(resp,96); readBuffer(resp,96,DEFAULT_TIMEOUT); if(NULL != strstr(resp,"OK")) { + printf("Response: -%s-", resp); } else{ + printf("Response: -%s-", resp); return false; } //Brings up wireless connection printf(">>>>AT+CIICR\r\n"); sendCmd("AT+CIICR\r\n"); cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); + readBuffer(resp,96,DEFAULT_TIMEOUT*3); if(NULL != strstr(resp,"OK")) { + printf("Response: -%s-", resp); } else{ + printf("Response: -%s-", resp); return false; } - printf(">>>>AT+CIPHEAD\r\n"); - sendCmd("AT+CIPHEAD=1\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")) { + if(NULL != strstr(ipAddr,".")) { _ip = str_to_ip(ipAddr+12); if(_ip != 0) { - //possibly send ip address to server for future TCP connections return true; } } + 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")) { + printf("Response: -%s-", resp); + } + else{ + printf("Response: -%s-", resp); + return false; + } return false; } @@ -253,13 +312,17 @@ } else { return false; } - + + printf("CONNECT: %s", cmd); sendCmd(cmd); + cleanBuffer(resp,96); readBuffer(resp,96,DEFAULT_TIMEOUT); if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK - printf("Response: %s\r\n", resp); + printf("SUCCESS: %s\r\n", resp); + return true; } + printf("FAILURE: %s\r\n", resp); return false;//ERROR } @@ -277,7 +340,17 @@ bool GPRS::disconnect() { + char resp[96]; + printf(">>>AT+CIPSHUT\r\n"); sendCmd("AT+CIPSHUT\r\n"); + readBuffer(resp,96,DEFAULT_TIMEOUT); + if(NULL != strstr(resp,"SHUT OK")) { //ALREADY CONNECT or CONNECT OK + printf("Response: %s\r\n", resp); + } + else{ + printf("Response: %s\r\n", resp); + return false; + } return true; } @@ -286,15 +359,15 @@ char cmd[16]; char resp[96]; - while(1){ - snprintf(cmd,16,"AT+CIPSTATUS=%d\r\n",socket); - sendCmd(cmd); - readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"CONNECTED")) { - return true; - } else { - return false; - } + snprintf(cmd,16,"AT+CIPSTATUS\r\n"); + sendCmd(cmd); + readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT); + if(NULL != strstr(resp,"CONNECT OK")) { + printf("Response -%s-\r\n", resp); + return true; + } else { + printf("Fail Response -%s-\r\n", resp); + return false; } } @@ -366,16 +439,17 @@ return -1; } char cmd[32]; - wait(1); + //wait(1); if(len > 0){ printf("Sending data: %s\r\n", str); snprintf(cmd,sizeof(cmd),"AT+CIPSEND\r\n"); - if(0 != sendCmdAndWaitForResp(cmd,">",2,CMD)) { - printf("Couldn't send data!!\r\n"); + if(0 != sendCmdAndWaitForResp(cmd,">",1,CMD)) { + printf("Couldn't send data!!"); return false; } sendCmd(str); serialModem.putc((char)0x1a); + } return len; }