ghrh
Diff: GSM.cpp
- Revision:
- 11:c93524a12e92
- Parent:
- 10:24671d8aa0c9
--- a/GSM.cpp Tue May 13 14:43:57 2014 +0000 +++ b/GSM.cpp Sat Oct 03 14:43:26 2020 +0000 @@ -21,6 +21,61 @@ */ #include "GSM.h" +void cleanBuffer(char *buffer, int count) +{ + for(int i=0; i < count; i++) { + buffer[i] = '\0'; + } +} + +int GSM::unlock(char *PIN) +{ + + char cmd[32]; + char gprsBuffer[30]; + int count = 0; + cleanBuffer(gprsBuffer,30); + snprintf(cmd, sizeof(cmd),"AT+CPIN=%s\r\n", PIN); + wait(1); + sendCmdAndWaitForResp(cmd, "OK"); + while(count < 3) { + //wait(1); + sendCmd("AT+CPIN?\r\n"); + readBuffer(gprsBuffer,30); + //wait(1); + if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { + return 0; + } + count++; + wait(1); + } + return -1; // too many tries +} + +int GSM::powerOn(void) +{ + int count = 0; + while (0 != powerCheck()) { + PWK = 1; + count ++; + wait(0.5); + if (count > 3) return -1; + } + PWK = 0; + return 0; +} + +int GSM::powerOff(void) +{ + while(0 == powerCheck()) { + sendCmdAndWaitForResp("AT+QPOWD=1\r", "OK"); + wait(0.1); + } + return 0; +} + + + int GSM::readBuffer(char *buffer,int count) { int i = 0; @@ -40,19 +95,12 @@ } } wait(0.5); - while(gprsSerial.readable()) { // display the other thing.. + while(gprsSerial.readable()) { // display the other thing.. char c = gprsSerial.getc(); } return 0; } -void cleanBuffer(char *buffer, int count) -{ - for(int i=0; i < count; i++) { - buffer[i] = '\0'; - } -} - void GSM::sendCmd(char *cmd) { gprsSerial.puts(cmd); @@ -76,7 +124,7 @@ return -1; } } - timeCnt.stop(); // stop timer + timeCnt.stop(); // stop timer timeCnt.reset(); // clear timer while(gprsSerial.readable()) { // display the other thing.. char c = gprsSerial.getc(); @@ -93,23 +141,23 @@ int GSM::powerCheck(void) { - return sendCmdAndWaitForResp("AT\r\n", "OK", 2); + return sendCmdAndWaitForResp("AT\r\n", "OK", 2); } int GSM::init(void) { for(int i = 0; i < 3; i++){ - sendCmdAndWaitForResp("AT\r\n", "OK", DEFAULT_TIMEOUT); + sendCmdAndWaitForResp("AT\r\n", "OK"); wait(0.5); } if(0 != checkSIMStatus()) { return -1; } if(checkSignalStrength()<1) { - return -1; + return -2; } if(0 != settingSMS()) { - return -1; + return -3; } return 0; } @@ -119,6 +167,7 @@ char gprsBuffer[30]; int count = 0; cleanBuffer(gprsBuffer,30); + while(count < 3) { sendCmd("AT+CPIN?\r\n"); readBuffer(gprsBuffer,30); @@ -149,7 +198,7 @@ count++; wait(1); } - if(count == 3) { + if(count == 10) { return -1; } return index; @@ -157,11 +206,12 @@ int GSM::settingSMS(void) { - if(0 != sendCmdAndWaitForResp("AT+CNMI=2,2\r\n", "OK", DEFAULT_TIMEOUT)) { + if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK")) { return -1; } - if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT)) { - return -1; + if(0 != sendCmdAndWaitForResp("AT+CNMI=2,2\r\n", "OK")) { //AT+CNMI=2,2 + + return -2; } return 0; } @@ -172,8 +222,8 @@ while(gprsSerial.readable()) { char c = gprsSerial.getc(); } - snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n",number); - if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) { + snprintf(cmd, sizeof(cmd), "AT+CMGS=\"%s\"\r\n", number); + if(0 != sendCmdAndWaitForResp(cmd, ">")) { return -1; } wait(1); @@ -294,7 +344,7 @@ { char cstt[64]; snprintf(cstt,sizeof(cstt),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",apn,userName,passWord); - if(0 != sendCmdAndWaitForResp(cstt, "OK", DEFAULT_TIMEOUT)) { + if(0 != sendCmdAndWaitForResp(cstt, "OK")) { return -1; } return 0; @@ -309,7 +359,7 @@ } #endif sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r\n", ip, port); - if(0 != sendCmdAndWaitForResp(cipstart, "OK", DEFAULT_TIMEOUT)) { + if(0 != sendCmdAndWaitForResp(cipstart, "OK")) { return -1; } return 0; @@ -319,10 +369,10 @@ char cmd[64]; int len = strlen(data); snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len); - if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) { + if(0 != sendCmdAndWaitForResp(cmd,">")) { return -1; } - if(0 != sendCmdAndWaitForResp(data,"OK",DEFAULT_TIMEOUT)) { + if(0 != sendCmdAndWaitForResp(data,"OK")) { return -1; } return 0;