Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
GSM.cpp
00001 /* 00002 GSM.cpp 00003 2013 Copyright (c) Seeed Technology Inc. All right reserved. 00004 00005 Author:lawliet.zou@gmail.com 00006 2013-11-14 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 #include "GSM.h" 00023 00024 void cleanBuffer(char *buffer, int count) 00025 { 00026 for(int i=0; i < count; i++) { 00027 buffer[i] = '\0'; 00028 } 00029 } 00030 00031 int GSM::unlock(char *PIN) 00032 { 00033 00034 char cmd[32]; 00035 char gprsBuffer[30]; 00036 int count = 0; 00037 cleanBuffer(gprsBuffer,30); 00038 snprintf(cmd, sizeof(cmd),"AT+CPIN=%s\r\n", PIN); 00039 wait(1); 00040 sendCmdAndWaitForResp(cmd, "OK"); 00041 while(count < 3) { 00042 //wait(1); 00043 sendCmd("AT+CPIN?\r\n"); 00044 readBuffer(gprsBuffer,30); 00045 //wait(1); 00046 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { 00047 return 0; 00048 } 00049 count++; 00050 wait(1); 00051 } 00052 return -1; // too many tries 00053 } 00054 00055 int GSM::powerOn(void) 00056 { 00057 int count = 0; 00058 while (0 != powerCheck()) { 00059 PWK = 1; 00060 count ++; 00061 wait(0.5); 00062 if (count > 3) return -1; 00063 } 00064 PWK = 0; 00065 return 0; 00066 } 00067 00068 int GSM::powerOff(void) 00069 { 00070 while(0 == powerCheck()) { 00071 sendCmdAndWaitForResp("AT+QPOWD=1\r", "OK"); 00072 wait(0.1); 00073 } 00074 return 0; 00075 } 00076 00077 00078 00079 int GSM::readBuffer(char *buffer,int count) 00080 { 00081 int i = 0; 00082 timeCnt.start(); // start timer 00083 while(1) { 00084 while (gprsSerial.readable()) { 00085 char c = gprsSerial.getc(); 00086 if (c == '\r' || c == '\n') c = '$'; 00087 buffer[i++] = c; 00088 if(i > count)break; 00089 } 00090 if(i > count)break; 00091 if(timeCnt.read() > DEFAULT_TIMEOUT) { 00092 timeCnt.stop(); 00093 timeCnt.reset(); 00094 break; 00095 } 00096 } 00097 wait(0.5); 00098 while(gprsSerial.readable()) { // display the other thing.. 00099 char c = gprsSerial.getc(); 00100 } 00101 return 0; 00102 } 00103 00104 void GSM::sendCmd(char *cmd) 00105 { 00106 gprsSerial.puts(cmd); 00107 } 00108 00109 int GSM::waitForResp(char *resp, int timeout) 00110 { 00111 int len = strlen(resp); 00112 int sum=0; 00113 timeCnt.start(); 00114 00115 while(1) { 00116 if(gprsSerial.readable()) { 00117 char c = gprsSerial.getc(); 00118 sum = (c==resp[sum]) ? sum+1 : 0; 00119 if(sum == len)break; 00120 } 00121 if(timeCnt.read() > timeout) { // time out 00122 timeCnt.stop(); 00123 timeCnt.reset(); 00124 return -1; 00125 } 00126 } 00127 timeCnt.stop(); // stop timer 00128 timeCnt.reset(); // clear timer 00129 while(gprsSerial.readable()) { // display the other thing.. 00130 char c = gprsSerial.getc(); 00131 } 00132 00133 return 0; 00134 } 00135 00136 int GSM::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout) 00137 { 00138 sendCmd(cmd); 00139 return waitForResp(resp,timeout); 00140 } 00141 00142 int GSM::powerCheck(void) 00143 { 00144 return sendCmdAndWaitForResp("AT\r\n", "OK", 2); 00145 } 00146 00147 int GSM::init(void) 00148 { 00149 for(int i = 0; i < 3; i++){ 00150 sendCmdAndWaitForResp("AT\r\n", "OK"); 00151 wait(0.5); 00152 } 00153 if(0 != checkSIMStatus()) { 00154 return -1; 00155 } 00156 if(checkSignalStrength()<1) { 00157 return -2; 00158 } 00159 if(0 != settingSMS()) { 00160 return -3; 00161 } 00162 return 0; 00163 } 00164 00165 int GSM::checkSIMStatus(void) 00166 { 00167 char gprsBuffer[30]; 00168 int count = 0; 00169 cleanBuffer(gprsBuffer,30); 00170 00171 while(count < 3) { 00172 sendCmd("AT+CPIN?\r\n"); 00173 readBuffer(gprsBuffer,30); 00174 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { 00175 break; 00176 } 00177 count++; 00178 wait(1); 00179 } 00180 00181 if(count == 3) { 00182 return -1; 00183 } 00184 return 0; 00185 } 00186 00187 int GSM::checkSignalStrength(void) 00188 { 00189 char gprsBuffer[100]; 00190 int index,count = 0; 00191 cleanBuffer(gprsBuffer,100); 00192 while(count < 3) { 00193 sendCmd("AT+CSQ\r\n"); 00194 readBuffer(gprsBuffer,25); 00195 if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { 00196 break; 00197 } 00198 count++; 00199 wait(1); 00200 } 00201 if(count == 10) { 00202 return -1; 00203 } 00204 return index; 00205 } 00206 00207 int GSM::settingSMS(void) 00208 { 00209 if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK")) { 00210 return -1; 00211 } 00212 if(0 != sendCmdAndWaitForResp("AT+CNMI=2,2\r\n", "OK")) { //AT+CNMI=2,2 00213 00214 return -2; 00215 } 00216 return 0; 00217 } 00218 00219 int GSM::sendSMS(char *number, char *data) 00220 { 00221 char cmd[64]; 00222 while(gprsSerial.readable()) { 00223 char c = gprsSerial.getc(); 00224 } 00225 snprintf(cmd, sizeof(cmd), "AT+CMGS=\"%s\"\r\n", number); 00226 if(0 != sendCmdAndWaitForResp(cmd, ">")) { 00227 return -1; 00228 } 00229 wait(1); 00230 gprsSerial.puts(data); 00231 gprsSerial.putc((char)0x1a); 00232 return 0; 00233 } 00234 00235 int GSM::readSMS(char *message, int index) 00236 { 00237 int i = 0; 00238 char gprsBuffer[100]; 00239 char *p,*s; 00240 gprsSerial.printf("AT+CMGR=%d\r\n",index); 00241 cleanBuffer(gprsBuffer,100); 00242 readBuffer(gprsBuffer,100); 00243 if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { 00244 return -1; 00245 } 00246 if(NULL != ( s = strstr(gprsBuffer,"+32"))) { 00247 p = s + 6; 00248 while((*p != '$')&&(i < SMS_MAX_LENGTH-1)) { 00249 message[i++] = *(p++); 00250 } 00251 message[i] = '\0'; 00252 } 00253 return 0; 00254 } 00255 00256 int GSM::deleteSMS(int index) 00257 { 00258 char cmd[32]; 00259 snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); 00260 sendCmd(cmd); 00261 return 0; 00262 } 00263 00264 int GSM::getSMS(char* message) 00265 { 00266 if(NULL != messageBuffer) { 00267 strncpy(message,messageBuffer,SMS_MAX_LENGTH); 00268 } 00269 return 0; 00270 } 00271 00272 int GSM::callUp(char *number) 00273 { 00274 if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) { 00275 return -1; 00276 } 00277 wait(1); 00278 gprsSerial.printf("\r\nATD%s;\r\n",NULL==number?phoneNumber:number); 00279 return 0; 00280 } 00281 00282 int GSM::answer(void) 00283 { 00284 gprsSerial.printf("ATA\r\n"); 00285 return 0; 00286 } 00287 00288 int GSM::loopHandle(void) 00289 { 00290 char gprsBuffer[100]; 00291 int i; 00292 char *s = NULL; 00293 while(gprsSerial.readable()) { 00294 char c = gprsSerial.getc(); 00295 } 00296 wait(0.5); 00297 START: 00298 cleanBuffer(gprsBuffer,100); 00299 i = 0; 00300 while(1) { 00301 if(gprsSerial.readable()) { 00302 timeCnt.start(); // start timer 00303 while(1) { 00304 while (gprsSerial.readable()) { 00305 char c = gprsSerial.getc(); 00306 if (c == '\r' || c == '\n') c = '$'; 00307 gprsBuffer[i] = c; 00308 i++; 00309 if(i > 100) { 00310 i = 0; 00311 break; 00312 } 00313 } 00314 if(timeCnt.read() > 2) { // time out 00315 timeCnt.stop(); 00316 timeCnt.reset(); 00317 break; 00318 } 00319 } 00320 break; 00321 } 00322 } 00323 if(NULL != strstr(gprsBuffer,"RING")) { 00324 return MESSAGE_RING; 00325 } else if(NULL != (s = strstr(gprsBuffer,"+CMT"))) { //SMS: $$+CMTI: "SM",24$$ 00326 if(NULL != (s = strstr(gprsBuffer,"+32"))) { 00327 s += 6; 00328 int i = 0; 00329 cleanBuffer(messageBuffer,SMS_MAX_LENGTH); 00330 while((*s != '$')&&(i < SMS_MAX_LENGTH-1)) { 00331 messageBuffer[i++] = *(s++); 00332 } 00333 messageBuffer[i] = '\0'; 00334 return MESSAGE_SMS; 00335 } else { 00336 goto START; 00337 } 00338 } else { 00339 goto START; 00340 } 00341 } 00342 00343 int GSM::networkInit(char* apn, char* userName, char* passWord) 00344 { 00345 char cstt[64]; 00346 snprintf(cstt,sizeof(cstt),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",apn,userName,passWord); 00347 if(0 != sendCmdAndWaitForResp(cstt, "OK")) { 00348 return -1; 00349 } 00350 return 0; 00351 } 00352 00353 int GSM::connectTCP(char *ip, char *port) 00354 { 00355 char cipstart[64]; 00356 #if 0 00357 if(0 != sendCmdAndWaitForResp("AT+CSTT=\"CMNET\",\"\",\"\"\r\n", "OK", 5)) { 00358 return -1; 00359 } 00360 #endif 00361 sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r\n", ip, port); 00362 if(0 != sendCmdAndWaitForResp(cipstart, "OK")) { 00363 return -1; 00364 } 00365 return 0; 00366 } 00367 int GSM::sendTCPData(char *data) 00368 { 00369 char cmd[64]; 00370 int len = strlen(data); 00371 snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len); 00372 if(0 != sendCmdAndWaitForResp(cmd,">")) { 00373 return -1; 00374 } 00375 if(0 != sendCmdAndWaitForResp(data,"OK")) { 00376 return -1; 00377 } 00378 return 0; 00379 } 00380 00381 int GSM::closeTCP(void) 00382 { 00383 sendCmd("AT+CIPCLOSE\r\n"); 00384 return 0; 00385 } 00386 00387 int GSM::shutTCP(void) 00388 { 00389 sendCmd("AT+CIPSHUT\r\n"); 00390 return 0; 00391 }
Generated on Fri Aug 5 2022 06:11:07 by
1.7.2