A library for the SIM900 module to enable calling, answering, sending and receiving SMS messages

Dependents:   Seeed_GPRS_Shield_GSM BluetoothNONIN HealthCare_Graduation

Fork of GSM by Components

Committer:
screamer
Date:
Tue May 13 14:32:33 2014 +0000
Revision:
9:2f5445178940
Parent:
gprs.cpp@7:1278e9dde24e
Parent:
gprs.cpp@8:1ee8ba65061a
Merge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:a5ae94727346 1 /*
lawliet 0:a5ae94727346 2 gprs.cpp
lawliet 0:a5ae94727346 3 2013 Copyright (c) Seeed Technology Inc. All right reserved.
lawliet 0:a5ae94727346 4
lawliet 0:a5ae94727346 5 Author:lawliet.zou@gmail.com
lawliet 0:a5ae94727346 6 2013-11-14
lawliet 0:a5ae94727346 7
lawliet 0:a5ae94727346 8 This library is free software; you can redistribute it and/or
lawliet 0:a5ae94727346 9 modify it under the terms of the GNU Lesser General Public
lawliet 0:a5ae94727346 10 License as published by the Free Software Foundation; either
lawliet 0:a5ae94727346 11 version 2.1 of the License, or (at your option) any later version.
lawliet 0:a5ae94727346 12
lawliet 0:a5ae94727346 13 This library is distributed in the hope that it will be useful,
lawliet 0:a5ae94727346 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
lawliet 0:a5ae94727346 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lawliet 0:a5ae94727346 16 Lesser General Public License for more details.
lawliet 0:a5ae94727346 17
lawliet 0:a5ae94727346 18 You should have received a copy of the GNU Lesser General Public
lawliet 0:a5ae94727346 19 License along with this library; if not, write to the Free Software
lawliet 0:a5ae94727346 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lawliet 0:a5ae94727346 21 */
screamer 8:1ee8ba65061a 22 #include "GPRS.h"
lawliet 0:a5ae94727346 23
lawliet 2:16985da3a446 24 int GPRS::readBuffer(char *buffer,int count)
lawliet 0:a5ae94727346 25 {
lawliet 1:642a8dbe076c 26 int i = 0;
lawliet 1:642a8dbe076c 27 timeCnt.start(); // start timer
lawliet 1:642a8dbe076c 28 while(1) {
lawliet 1:642a8dbe076c 29 while (gprsSerial.readable()) {
lawliet 1:642a8dbe076c 30 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 31 if (c == '\r' || c == '\n') c = '$';
lawliet 1:642a8dbe076c 32 buffer[i++] = c;
lawliet 1:642a8dbe076c 33 if(i > count)break;
lawliet 1:642a8dbe076c 34 }
lawliet 1:642a8dbe076c 35 if(i > count)break;
lawliet 3:48ee24a4b0f3 36 if(timeCnt.read() > DEFAULT_TIMEOUT) {
lawliet 1:642a8dbe076c 37 timeCnt.stop();
lawliet 1:642a8dbe076c 38 timeCnt.reset();
lawliet 1:642a8dbe076c 39 break;
lawliet 1:642a8dbe076c 40 }
lawliet 1:642a8dbe076c 41 }
lawliet 1:642a8dbe076c 42 wait(0.5);
lawliet 1:642a8dbe076c 43 while(gprsSerial.readable()) { // display the other thing..
lawliet 1:642a8dbe076c 44 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 45 }
lawliet 1:642a8dbe076c 46 return 0;
lawliet 0:a5ae94727346 47 }
lawliet 0:a5ae94727346 48
lawliet 0:a5ae94727346 49 void cleanBuffer(char *buffer, int count)
lawliet 0:a5ae94727346 50 {
lawliet 1:642a8dbe076c 51 for(int i=0; i < count; i++) {
lawliet 1:642a8dbe076c 52 buffer[i] = '\0';
lawliet 1:642a8dbe076c 53 }
lawliet 0:a5ae94727346 54 }
lawliet 0:a5ae94727346 55
lawliet 2:16985da3a446 56 void GPRS::sendCmd(char *cmd)
lawliet 0:a5ae94727346 57 {
lawliet 1:642a8dbe076c 58 gprsSerial.puts(cmd);
lawliet 0:a5ae94727346 59 }
lawliet 0:a5ae94727346 60
lawliet 2:16985da3a446 61 int GPRS::waitForResp(char *resp, int timeout)
lawliet 0:a5ae94727346 62 {
lawliet 1:642a8dbe076c 63 int len = strlen(resp);
lawliet 1:642a8dbe076c 64 int sum=0;
lawliet 1:642a8dbe076c 65 timeCnt.start();
lawliet 1:642a8dbe076c 66
lawliet 1:642a8dbe076c 67 while(1) {
lawliet 1:642a8dbe076c 68 if(gprsSerial.readable()) {
lawliet 1:642a8dbe076c 69 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 70 sum = (c==resp[sum]) ? sum+1 : 0;
lawliet 1:642a8dbe076c 71 if(sum == len)break;
lawliet 1:642a8dbe076c 72 }
lawliet 1:642a8dbe076c 73 if(timeCnt.read() > timeout) { // time out
lawliet 1:642a8dbe076c 74 timeCnt.stop();
lawliet 1:642a8dbe076c 75 timeCnt.reset();
lawliet 1:642a8dbe076c 76 return -1;
lawliet 1:642a8dbe076c 77 }
lawliet 1:642a8dbe076c 78 }
lawliet 1:642a8dbe076c 79 timeCnt.stop(); // stop timer
lawliet 1:642a8dbe076c 80 timeCnt.reset(); // clear timer
lawliet 1:642a8dbe076c 81 while(gprsSerial.readable()) { // display the other thing..
lawliet 1:642a8dbe076c 82 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 83 }
lawliet 1:642a8dbe076c 84
lawliet 1:642a8dbe076c 85 return 0;
lawliet 1:642a8dbe076c 86 }
lawliet 0:a5ae94727346 87
lawliet 2:16985da3a446 88 int GPRS::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
lawliet 0:a5ae94727346 89 {
lawliet 0:a5ae94727346 90 sendCmd(cmd);
lawliet 1:642a8dbe076c 91 return waitForResp(resp,timeout);
lawliet 0:a5ae94727346 92 }
lawliet 0:a5ae94727346 93
lawliet 7:1278e9dde24e 94 int GPRS::powerCheck(void)
lawliet 7:1278e9dde24e 95 {
lawliet 7:1278e9dde24e 96 return sendCmdAndWaitForResp("AT\r\n", "OK", 2);
lawliet 7:1278e9dde24e 97 }
lawliet 7:1278e9dde24e 98
lawliet 4:63a2619b423b 99 int GPRS::init(void)
lawliet 4:63a2619b423b 100 {
lawliet 7:1278e9dde24e 101 for(int i = 0; i < 3; i++){
lawliet 7:1278e9dde24e 102 sendCmdAndWaitForResp("AT\r\n", "OK", DEFAULT_TIMEOUT);
lawliet 7:1278e9dde24e 103 wait(0.5);
lawliet 7:1278e9dde24e 104 }
lawliet 4:63a2619b423b 105 if(0 != checkSIMStatus()) {
lawliet 4:63a2619b423b 106 return -1;
lawliet 4:63a2619b423b 107 }
lawliet 4:63a2619b423b 108 if(checkSignalStrength()<1) {
lawliet 4:63a2619b423b 109 return -1;
lawliet 4:63a2619b423b 110 }
lawliet 4:63a2619b423b 111 if(0 != settingSMS()) {
lawliet 4:63a2619b423b 112 return -1;
lawliet 4:63a2619b423b 113 }
lawliet 4:63a2619b423b 114 return 0;
lawliet 4:63a2619b423b 115 }
lawliet 4:63a2619b423b 116
lawliet 2:16985da3a446 117 int GPRS::checkSIMStatus(void)
lawliet 0:a5ae94727346 118 {
lawliet 1:642a8dbe076c 119 char gprsBuffer[30];
lawliet 1:642a8dbe076c 120 int count = 0;
lawliet 1:642a8dbe076c 121 cleanBuffer(gprsBuffer,30);
lawliet 1:642a8dbe076c 122 while(count < 3) {
lawliet 1:642a8dbe076c 123 sendCmd("AT+CPIN?\r\n");
lawliet 1:642a8dbe076c 124 readBuffer(gprsBuffer,30);
lawliet 1:642a8dbe076c 125 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
lawliet 1:642a8dbe076c 126 break;
lawliet 1:642a8dbe076c 127 }
lawliet 1:642a8dbe076c 128 count++;
lawliet 1:642a8dbe076c 129 wait(1);
lawliet 1:642a8dbe076c 130 }
lawliet 0:a5ae94727346 131
lawliet 1:642a8dbe076c 132 if(count == 3) {
lawliet 1:642a8dbe076c 133 return -1;
lawliet 1:642a8dbe076c 134 }
lawliet 1:642a8dbe076c 135 return 0;
lawliet 0:a5ae94727346 136 }
lawliet 0:a5ae94727346 137
lawliet 2:16985da3a446 138 int GPRS::checkSignalStrength(void)
lawliet 0:a5ae94727346 139 {
lawliet 1:642a8dbe076c 140 char gprsBuffer[100];
lawliet 1:642a8dbe076c 141 int index,count = 0;
lawliet 1:642a8dbe076c 142 cleanBuffer(gprsBuffer,100);
lawliet 1:642a8dbe076c 143 while(count < 3) {
lawliet 1:642a8dbe076c 144 sendCmd("AT+CSQ\r\n");
lawliet 1:642a8dbe076c 145 readBuffer(gprsBuffer,25);
lawliet 1:642a8dbe076c 146 if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) {
lawliet 1:642a8dbe076c 147 break;
lawliet 1:642a8dbe076c 148 }
lawliet 1:642a8dbe076c 149 count++;
lawliet 1:642a8dbe076c 150 wait(1);
lawliet 1:642a8dbe076c 151 }
lawliet 1:642a8dbe076c 152 if(count == 3) {
lawliet 1:642a8dbe076c 153 return -1;
lawliet 1:642a8dbe076c 154 }
lawliet 1:642a8dbe076c 155 return index;
lawliet 0:a5ae94727346 156 }
lawliet 0:a5ae94727346 157
lawliet 4:63a2619b423b 158 int GPRS::settingSMS(void)
lawliet 4:63a2619b423b 159 {
lawliet 4:63a2619b423b 160 if(0 != sendCmdAndWaitForResp("AT+CNMI=2,2\r\n", "OK", DEFAULT_TIMEOUT)) {
lawliet 4:63a2619b423b 161 return -1;
lawliet 4:63a2619b423b 162 }
lawliet 4:63a2619b423b 163 if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT)) {
lawliet 4:63a2619b423b 164 return -1;
lawliet 4:63a2619b423b 165 }
lawliet 4:63a2619b423b 166 return 0;
lawliet 4:63a2619b423b 167 }
lawliet 4:63a2619b423b 168
lawliet 2:16985da3a446 169 int GPRS::sendSMS(char *number, char *data)
lawliet 0:a5ae94727346 170 {
lawliet 1:642a8dbe076c 171 char cmd[64];
lawliet 4:63a2619b423b 172 while(gprsSerial.readable()) {
lawliet 4:63a2619b423b 173 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 174 }
lawliet 4:63a2619b423b 175 snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n",number);
lawliet 3:48ee24a4b0f3 176 if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
lawliet 1:642a8dbe076c 177 return -1;
lawliet 1:642a8dbe076c 178 }
lawliet 1:642a8dbe076c 179 wait(1);
lawliet 4:63a2619b423b 180 gprsSerial.puts(data);
lawliet 4:63a2619b423b 181 gprsSerial.putc((char)0x1a);
lawliet 1:642a8dbe076c 182 return 0;
lawliet 0:a5ae94727346 183 }
lawliet 0:a5ae94727346 184
lawliet 4:63a2619b423b 185 int GPRS::readSMS(char *message, int index)
lawliet 0:a5ae94727346 186 {
lawliet 4:63a2619b423b 187 int i = 0;
lawliet 1:642a8dbe076c 188 char gprsBuffer[100];
lawliet 1:642a8dbe076c 189 char *p,*s;
lawliet 4:63a2619b423b 190 gprsSerial.printf("AT+CMGR=%d\r\n",index);
lawliet 1:642a8dbe076c 191 cleanBuffer(gprsBuffer,100);
lawliet 1:642a8dbe076c 192 readBuffer(gprsBuffer,100);
lawliet 4:63a2619b423b 193 if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) {
lawliet 1:642a8dbe076c 194 return -1;
lawliet 1:642a8dbe076c 195 }
lawliet 4:63a2619b423b 196 if(NULL != ( s = strstr(gprsBuffer,"+32"))) {
lawliet 4:63a2619b423b 197 p = s + 6;
lawliet 4:63a2619b423b 198 while((*p != '$')&&(i < SMS_MAX_LENGTH-1)) {
lawliet 4:63a2619b423b 199 message[i++] = *(p++);
lawliet 1:642a8dbe076c 200 }
lawliet 4:63a2619b423b 201 message[i] = '\0';
lawliet 1:642a8dbe076c 202 }
lawliet 1:642a8dbe076c 203 return 0;
lawliet 0:a5ae94727346 204 }
lawliet 0:a5ae94727346 205
lawliet 2:16985da3a446 206 int GPRS::deleteSMS(int index)
lawliet 0:a5ae94727346 207 {
lawliet 4:63a2619b423b 208 char cmd[32];
lawliet 1:642a8dbe076c 209 snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
lawliet 1:642a8dbe076c 210 sendCmd(cmd);
lawliet 1:642a8dbe076c 211 return 0;
lawliet 0:a5ae94727346 212 }
lawliet 0:a5ae94727346 213
lawliet 4:63a2619b423b 214 int GPRS::getSMS(char* message)
lawliet 4:63a2619b423b 215 {
lawliet 4:63a2619b423b 216 if(NULL != messageBuffer) {
lawliet 4:63a2619b423b 217 strncpy(message,messageBuffer,SMS_MAX_LENGTH);
lawliet 4:63a2619b423b 218 }
lawliet 4:63a2619b423b 219 return 0;
lawliet 4:63a2619b423b 220 }
lawliet 4:63a2619b423b 221
lawliet 2:16985da3a446 222 int GPRS::callUp(char *number)
lawliet 0:a5ae94727346 223 {
lawliet 1:642a8dbe076c 224 if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) {
lawliet 1:642a8dbe076c 225 return -1;
lawliet 1:642a8dbe076c 226 }
lawliet 1:642a8dbe076c 227 wait(1);
lawliet 1:642a8dbe076c 228 gprsSerial.printf("\r\nATD%s;\r\n",NULL==number?phoneNumber:number);
lawliet 1:642a8dbe076c 229 return 0;
lawliet 0:a5ae94727346 230 }
lawliet 0:a5ae94727346 231
lawliet 2:16985da3a446 232 int GPRS::answer(void)
lawliet 0:a5ae94727346 233 {
lawliet 1:642a8dbe076c 234 gprsSerial.printf("ATA\r\n");
lawliet 1:642a8dbe076c 235 return 0;
lawliet 0:a5ae94727346 236 }
lawliet 0:a5ae94727346 237
lawliet 4:63a2619b423b 238 int GPRS::loopHandle(void)
lawliet 0:a5ae94727346 239 {
lawliet 1:642a8dbe076c 240 char gprsBuffer[100];
lawliet 7:1278e9dde24e 241 int i;
lawliet 4:63a2619b423b 242 char *s = NULL;
lawliet 4:63a2619b423b 243 while(gprsSerial.readable()) {
lawliet 4:63a2619b423b 244 char c = gprsSerial.getc();
lawliet 4:63a2619b423b 245 }
lawliet 7:1278e9dde24e 246 wait(0.5);
lawliet 4:63a2619b423b 247 START:
lawliet 7:1278e9dde24e 248 cleanBuffer(gprsBuffer,100);
lawliet 7:1278e9dde24e 249 i = 0;
lawliet 1:642a8dbe076c 250 while(1) {
lawliet 1:642a8dbe076c 251 if(gprsSerial.readable()) {
lawliet 7:1278e9dde24e 252 timeCnt.start(); // start timer
lawliet 7:1278e9dde24e 253 while(1) {
lawliet 7:1278e9dde24e 254 while (gprsSerial.readable()) {
lawliet 7:1278e9dde24e 255 char c = gprsSerial.getc();
lawliet 7:1278e9dde24e 256 if (c == '\r' || c == '\n') c = '$';
lawliet 7:1278e9dde24e 257 gprsBuffer[i] = c;
lawliet 7:1278e9dde24e 258 i++;
lawliet 7:1278e9dde24e 259 if(i > 100) {
lawliet 7:1278e9dde24e 260 i = 0;
lawliet 7:1278e9dde24e 261 break;
lawliet 7:1278e9dde24e 262 }
lawliet 7:1278e9dde24e 263 }
lawliet 7:1278e9dde24e 264 if(timeCnt.read() > 2) { // time out
lawliet 7:1278e9dde24e 265 timeCnt.stop();
lawliet 7:1278e9dde24e 266 timeCnt.reset();
lawliet 7:1278e9dde24e 267 break;
lawliet 7:1278e9dde24e 268 }
lawliet 7:1278e9dde24e 269 }
lawliet 1:642a8dbe076c 270 break;
lawliet 1:642a8dbe076c 271 }
lawliet 1:642a8dbe076c 272 }
lawliet 1:642a8dbe076c 273 if(NULL != strstr(gprsBuffer,"RING")) {
lawliet 4:63a2619b423b 274 return MESSAGE_RING;
lawliet 4:63a2619b423b 275 } else if(NULL != (s = strstr(gprsBuffer,"+CMT"))) { //SMS: $$+CMTI: "SM",24$$
lawliet 4:63a2619b423b 276 if(NULL != (s = strstr(gprsBuffer,"+32"))) {
lawliet 4:63a2619b423b 277 s += 6;
lawliet 4:63a2619b423b 278 int i = 0;
lawliet 4:63a2619b423b 279 cleanBuffer(messageBuffer,SMS_MAX_LENGTH);
lawliet 4:63a2619b423b 280 while((*s != '$')&&(i < SMS_MAX_LENGTH-1)) {
lawliet 4:63a2619b423b 281 messageBuffer[i++] = *(s++);
lawliet 4:63a2619b423b 282 }
lawliet 4:63a2619b423b 283 messageBuffer[i] = '\0';
lawliet 4:63a2619b423b 284 return MESSAGE_SMS;
lawliet 4:63a2619b423b 285 } else {
lawliet 4:63a2619b423b 286 goto START;
lawliet 1:642a8dbe076c 287 }
lawliet 4:63a2619b423b 288 } else {
lawliet 4:63a2619b423b 289 goto START;
lawliet 1:642a8dbe076c 290 }
lawliet 0:a5ae94727346 291 }
lawliet 0:a5ae94727346 292
lawliet 5:ac2342f162fa 293 int GPRS::networkInit(char* apn, char* userName, char* passWord)
lawliet 5:ac2342f162fa 294 {
lawliet 5:ac2342f162fa 295 char cstt[64];
lawliet 5:ac2342f162fa 296 snprintf(cstt,sizeof(cstt),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",apn,userName,passWord);
lawliet 5:ac2342f162fa 297 if(0 != sendCmdAndWaitForResp(cstt, "OK", DEFAULT_TIMEOUT)) {
lawliet 5:ac2342f162fa 298 return -1;
lawliet 5:ac2342f162fa 299 }
lawliet 5:ac2342f162fa 300 return 0;
lawliet 5:ac2342f162fa 301 }
lawliet 0:a5ae94727346 302
lawliet 2:16985da3a446 303 int GPRS::connectTCP(char *ip, char *port)
lawliet 0:a5ae94727346 304 {
lawliet 5:ac2342f162fa 305 char cipstart[64];
lawliet 5:ac2342f162fa 306 #if 0
lawliet 3:48ee24a4b0f3 307 if(0 != sendCmdAndWaitForResp("AT+CSTT=\"CMNET\",\"\",\"\"\r\n", "OK", 5)) {
lawliet 1:642a8dbe076c 308 return -1;
lawliet 1:642a8dbe076c 309 }
lawliet 5:ac2342f162fa 310 #endif
lawliet 1:642a8dbe076c 311 sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r\n", ip, port);
lawliet 5:ac2342f162fa 312 if(0 != sendCmdAndWaitForResp(cipstart, "OK", DEFAULT_TIMEOUT)) {
lawliet 1:642a8dbe076c 313 return -1;
lawliet 1:642a8dbe076c 314 }
lawliet 1:642a8dbe076c 315 return 0;
lawliet 0:a5ae94727346 316 }
lawliet 2:16985da3a446 317 int GPRS::sendTCPData(char *data)
lawliet 0:a5ae94727346 318 {
lawliet 1:642a8dbe076c 319 char cmd[64];
lawliet 1:642a8dbe076c 320 int len = strlen(data);
lawliet 1:642a8dbe076c 321 snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
lawliet 3:48ee24a4b0f3 322 if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
lawliet 1:642a8dbe076c 323 return -1;
lawliet 1:642a8dbe076c 324 }
lawliet 3:48ee24a4b0f3 325 if(0 != sendCmdAndWaitForResp(data,"OK",DEFAULT_TIMEOUT)) {
lawliet 1:642a8dbe076c 326 return -1;
lawliet 1:642a8dbe076c 327 }
lawliet 1:642a8dbe076c 328 return 0;
lawliet 0:a5ae94727346 329 }
lawliet 0:a5ae94727346 330
lawliet 2:16985da3a446 331 int GPRS::closeTCP(void)
lawliet 0:a5ae94727346 332 {
lawliet 1:642a8dbe076c 333 sendCmd("AT+CIPCLOSE\r\n");
lawliet 1:642a8dbe076c 334 return 0;
lawliet 0:a5ae94727346 335 }
lawliet 0:a5ae94727346 336
lawliet 2:16985da3a446 337 int GPRS::shutTCP(void)
lawliet 0:a5ae94727346 338 {
lawliet 1:642a8dbe076c 339 sendCmd("AT+CIPSHUT\r\n");
lawliet 1:642a8dbe076c 340 return 0;
lawliet 0:a5ae94727346 341 }