ghrh

Committer:
lawliet
Date:
Fri Jan 10 05:59:36 2014 +0000
Revision:
3:48ee24a4b0f3
Parent:
2:16985da3a446
Child:
4:63a2619b423b
Version 1.3(fix some bugs)

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 */
lawliet 0:a5ae94727346 22
lawliet 3:48ee24a4b0f3 23
lawliet 0:a5ae94727346 24 #include "gprs.h"
lawliet 0:a5ae94727346 25
lawliet 2:16985da3a446 26 int GPRS::init(void)
lawliet 0:a5ae94727346 27 {
lawliet 1:642a8dbe076c 28 wait(0.5);
lawliet 3:48ee24a4b0f3 29 sendCmd("AT+CFUN=1\r\n");
lawliet 3:48ee24a4b0f3 30 wait(1);
lawliet 1:642a8dbe076c 31 if(0 != checkSIMStatus()) { //check SIM card status
lawliet 1:642a8dbe076c 32 return -1;
lawliet 1:642a8dbe076c 33 }
lawliet 1:642a8dbe076c 34 if(checkSignalStrength()<1) { //check Signal Strength
lawliet 1:642a8dbe076c 35 return -1;
lawliet 1:642a8dbe076c 36 }
lawliet 1:642a8dbe076c 37 return 0;
lawliet 0:a5ae94727346 38 }
lawliet 0:a5ae94727346 39
lawliet 2:16985da3a446 40 int GPRS::readBuffer(char *buffer,int count)
lawliet 0:a5ae94727346 41 {
lawliet 1:642a8dbe076c 42 int i = 0;
lawliet 1:642a8dbe076c 43 timeCnt.start(); // start timer
lawliet 1:642a8dbe076c 44 while(1) {
lawliet 1:642a8dbe076c 45 while (gprsSerial.readable()) {
lawliet 1:642a8dbe076c 46 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 47 if (c == '\r' || c == '\n') c = '$';
lawliet 1:642a8dbe076c 48 buffer[i++] = c;
lawliet 1:642a8dbe076c 49 if(i > count)break;
lawliet 1:642a8dbe076c 50 }
lawliet 1:642a8dbe076c 51 if(i > count)break;
lawliet 3:48ee24a4b0f3 52 if(timeCnt.read() > DEFAULT_TIMEOUT) {
lawliet 1:642a8dbe076c 53 timeCnt.stop();
lawliet 1:642a8dbe076c 54 timeCnt.reset();
lawliet 1:642a8dbe076c 55 break;
lawliet 1:642a8dbe076c 56 }
lawliet 1:642a8dbe076c 57 }
lawliet 1:642a8dbe076c 58 wait(0.5);
lawliet 1:642a8dbe076c 59 while(gprsSerial.readable()) { // display the other thing..
lawliet 1:642a8dbe076c 60 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 61 }
lawliet 1:642a8dbe076c 62 return 0;
lawliet 0:a5ae94727346 63 }
lawliet 0:a5ae94727346 64
lawliet 0:a5ae94727346 65 void cleanBuffer(char *buffer, int count)
lawliet 0:a5ae94727346 66 {
lawliet 1:642a8dbe076c 67 for(int i=0; i < count; i++) {
lawliet 1:642a8dbe076c 68 buffer[i] = '\0';
lawliet 1:642a8dbe076c 69 }
lawliet 0:a5ae94727346 70 }
lawliet 0:a5ae94727346 71
lawliet 2:16985da3a446 72 void GPRS::sendCmd(char *cmd)
lawliet 0:a5ae94727346 73 {
lawliet 1:642a8dbe076c 74 gprsSerial.puts(cmd);
lawliet 0:a5ae94727346 75 }
lawliet 0:a5ae94727346 76
lawliet 2:16985da3a446 77 int GPRS::waitForResp(char *resp, int timeout)
lawliet 0:a5ae94727346 78 {
lawliet 1:642a8dbe076c 79 int len = strlen(resp);
lawliet 1:642a8dbe076c 80 int sum=0;
lawliet 1:642a8dbe076c 81 timeCnt.start();
lawliet 1:642a8dbe076c 82
lawliet 1:642a8dbe076c 83 while(1) {
lawliet 1:642a8dbe076c 84 if(gprsSerial.readable()) {
lawliet 1:642a8dbe076c 85 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 86 sum = (c==resp[sum]) ? sum+1 : 0;
lawliet 1:642a8dbe076c 87 if(sum == len)break;
lawliet 1:642a8dbe076c 88 }
lawliet 1:642a8dbe076c 89 if(timeCnt.read() > timeout) { // time out
lawliet 1:642a8dbe076c 90 timeCnt.stop();
lawliet 1:642a8dbe076c 91 timeCnt.reset();
lawliet 1:642a8dbe076c 92 return -1;
lawliet 1:642a8dbe076c 93 }
lawliet 1:642a8dbe076c 94 }
lawliet 1:642a8dbe076c 95 timeCnt.stop(); // stop timer
lawliet 1:642a8dbe076c 96 timeCnt.reset(); // clear timer
lawliet 1:642a8dbe076c 97 while(gprsSerial.readable()) { // display the other thing..
lawliet 1:642a8dbe076c 98 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 99 }
lawliet 1:642a8dbe076c 100
lawliet 1:642a8dbe076c 101 return 0;
lawliet 1:642a8dbe076c 102 }
lawliet 0:a5ae94727346 103
lawliet 0:a5ae94727346 104
lawliet 0:a5ae94727346 105
lawliet 2:16985da3a446 106 int GPRS::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
lawliet 0:a5ae94727346 107 {
lawliet 0:a5ae94727346 108 sendCmd(cmd);
lawliet 1:642a8dbe076c 109 return waitForResp(resp,timeout);
lawliet 0:a5ae94727346 110 }
lawliet 0:a5ae94727346 111
lawliet 2:16985da3a446 112 int GPRS::checkSIMStatus(void)
lawliet 0:a5ae94727346 113 {
lawliet 1:642a8dbe076c 114 char gprsBuffer[30];
lawliet 1:642a8dbe076c 115 int count = 0;
lawliet 1:642a8dbe076c 116 cleanBuffer(gprsBuffer,30);
lawliet 1:642a8dbe076c 117 while(count < 3) {
lawliet 1:642a8dbe076c 118 sendCmd("AT+CPIN?\r\n");
lawliet 1:642a8dbe076c 119 readBuffer(gprsBuffer,30);
lawliet 1:642a8dbe076c 120 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
lawliet 1:642a8dbe076c 121 break;
lawliet 1:642a8dbe076c 122 }
lawliet 1:642a8dbe076c 123 count++;
lawliet 1:642a8dbe076c 124 wait(1);
lawliet 1:642a8dbe076c 125 }
lawliet 0:a5ae94727346 126
lawliet 1:642a8dbe076c 127 if(count == 3) {
lawliet 1:642a8dbe076c 128 return -1;
lawliet 1:642a8dbe076c 129 }
lawliet 1:642a8dbe076c 130 return 0;
lawliet 0:a5ae94727346 131 }
lawliet 0:a5ae94727346 132
lawliet 2:16985da3a446 133 int GPRS::checkSignalStrength(void)
lawliet 0:a5ae94727346 134 {
lawliet 1:642a8dbe076c 135 char gprsBuffer[100];
lawliet 1:642a8dbe076c 136 int index,count = 0;
lawliet 1:642a8dbe076c 137 cleanBuffer(gprsBuffer,100);
lawliet 1:642a8dbe076c 138 while(count < 3) {
lawliet 1:642a8dbe076c 139 sendCmd("AT+CSQ\r\n");
lawliet 1:642a8dbe076c 140 readBuffer(gprsBuffer,25);
lawliet 1:642a8dbe076c 141 if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) {
lawliet 1:642a8dbe076c 142 break;
lawliet 1:642a8dbe076c 143 }
lawliet 1:642a8dbe076c 144 count++;
lawliet 1:642a8dbe076c 145 wait(1);
lawliet 1:642a8dbe076c 146 }
lawliet 1:642a8dbe076c 147 if(count == 3) {
lawliet 1:642a8dbe076c 148 return -1;
lawliet 1:642a8dbe076c 149 }
lawliet 1:642a8dbe076c 150 return index;
lawliet 0:a5ae94727346 151 }
lawliet 0:a5ae94727346 152
lawliet 2:16985da3a446 153 int GPRS::sendSMS(char *number, char *data)
lawliet 0:a5ae94727346 154 {
lawliet 1:642a8dbe076c 155 char cmd[64];
lawliet 3:48ee24a4b0f3 156 if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT)) { // Set message mode to ASCII
lawliet 1:642a8dbe076c 157 return -1;
lawliet 1:642a8dbe076c 158 }
lawliet 1:642a8dbe076c 159 wait(0.5);
lawliet 1:642a8dbe076c 160 // Set the phone number
lawliet 1:642a8dbe076c 161 snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number);
lawliet 3:48ee24a4b0f3 162 if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
lawliet 1:642a8dbe076c 163 return -1;
lawliet 1:642a8dbe076c 164 }
lawliet 1:642a8dbe076c 165 wait(1);
lawliet 1:642a8dbe076c 166 gprsSerial.puts(data);// Send Message
lawliet 1:642a8dbe076c 167 wait(0.5);
lawliet 1:642a8dbe076c 168 gprsSerial.putc(0x1A);//end mark
lawliet 1:642a8dbe076c 169 return 0;
lawliet 0:a5ae94727346 170 }
lawliet 0:a5ae94727346 171
lawliet 2:16985da3a446 172 int GPRS::readSMS(char *buffer, char *message, bool check)
lawliet 0:a5ae94727346 173 {
lawliet 1:642a8dbe076c 174 int index,i = 0;
lawliet 1:642a8dbe076c 175 char gprsBuffer[100];
lawliet 1:642a8dbe076c 176 char *p,*s;
lawliet 0:a5ae94727346 177
lawliet 1:642a8dbe076c 178 if(sscanf(buffer, "$$+CMTI: \"SM\",%d", &index)>0) {
lawliet 1:642a8dbe076c 179 gprsSerial.printf("AT+CMGR=%d\r\n", index);
lawliet 1:642a8dbe076c 180 } else {
lawliet 3:48ee24a4b0f3 181 return -1;
lawliet 1:642a8dbe076c 182 }
lawliet 1:642a8dbe076c 183 cleanBuffer(gprsBuffer,100);
lawliet 1:642a8dbe076c 184 readBuffer(gprsBuffer,100);
lawliet 1:642a8dbe076c 185 if(NULL == ( s = strstr(gprsBuffer,"+CMGR: \"REC UNREAD\""))) {
lawliet 1:642a8dbe076c 186 return -1;
lawliet 1:642a8dbe076c 187 }
lawliet 1:642a8dbe076c 188
lawliet 1:642a8dbe076c 189 //check phone number
lawliet 1:642a8dbe076c 190 if(check) {
lawliet 1:642a8dbe076c 191 char number[20];
lawliet 1:642a8dbe076c 192 snprintf(number,sizeof(number),"\"+86%s\"",phoneNumber); //for China
lawliet 1:642a8dbe076c 193 p = s + 20;
lawliet 1:642a8dbe076c 194 if(0 != (strncmp(number,p,14))) {
lawliet 1:642a8dbe076c 195 return -1;
lawliet 1:642a8dbe076c 196 }
lawliet 1:642a8dbe076c 197 }
lawliet 1:642a8dbe076c 198 p = s + 64;
lawliet 1:642a8dbe076c 199 while(*p != '$') {
lawliet 1:642a8dbe076c 200 message[i++] = *(p++);
lawliet 1:642a8dbe076c 201 }
lawliet 1:642a8dbe076c 202 message[i] = '\0';
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 1:642a8dbe076c 208 char cmd[64];
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 2:16985da3a446 214 int GPRS::callUp(char *number)
lawliet 0:a5ae94727346 215 {
lawliet 1:642a8dbe076c 216 if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) {
lawliet 1:642a8dbe076c 217 return -1;
lawliet 1:642a8dbe076c 218 }
lawliet 1:642a8dbe076c 219 wait(1);
lawliet 1:642a8dbe076c 220 gprsSerial.printf("\r\nATD%s;\r\n",NULL==number?phoneNumber:number);
lawliet 1:642a8dbe076c 221 return 0;
lawliet 0:a5ae94727346 222 }
lawliet 0:a5ae94727346 223
lawliet 2:16985da3a446 224 int GPRS::answer(void)
lawliet 0:a5ae94727346 225 {
lawliet 1:642a8dbe076c 226 gprsSerial.printf("ATA\r\n");
lawliet 1:642a8dbe076c 227 return 0;
lawliet 0:a5ae94727346 228 }
lawliet 0:a5ae94727346 229
lawliet 2:16985da3a446 230 int GPRS::loop(bool check)
lawliet 0:a5ae94727346 231 {
lawliet 1:642a8dbe076c 232 char gprsBuffer[100];
lawliet 1:642a8dbe076c 233 int i = 0;
lawliet 1:642a8dbe076c 234 cleanBuffer(gprsBuffer,100);
lawliet 1:642a8dbe076c 235 while(1) {
lawliet 1:642a8dbe076c 236 if(gprsSerial.readable()) {
lawliet 1:642a8dbe076c 237 break;
lawliet 1:642a8dbe076c 238 }
lawliet 1:642a8dbe076c 239 wait(1);
lawliet 1:642a8dbe076c 240 }
lawliet 1:642a8dbe076c 241
lawliet 1:642a8dbe076c 242 timeCnt.start(); // start timer
lawliet 1:642a8dbe076c 243 while(1) {
lawliet 1:642a8dbe076c 244 while (gprsSerial.readable()) {
lawliet 1:642a8dbe076c 245 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 246 if (c == '\r' || c == '\n') c = '$';
lawliet 1:642a8dbe076c 247 gprsBuffer[i] = c;
lawliet 1:642a8dbe076c 248 i++;
lawliet 1:642a8dbe076c 249 if(i > 100) {
lawliet 1:642a8dbe076c 250 i = 0;
lawliet 1:642a8dbe076c 251 break;
lawliet 1:642a8dbe076c 252 }
lawliet 1:642a8dbe076c 253 }
lawliet 1:642a8dbe076c 254 if(timeCnt.read() > 5) { // time out
lawliet 1:642a8dbe076c 255 timeCnt.stop();
lawliet 1:642a8dbe076c 256 timeCnt.reset();
lawliet 1:642a8dbe076c 257 break;
lawliet 1:642a8dbe076c 258 }
lawliet 1:642a8dbe076c 259 }
lawliet 1:642a8dbe076c 260
lawliet 1:642a8dbe076c 261 if(NULL != strstr(gprsBuffer,"RING")) {
lawliet 1:642a8dbe076c 262 if(0 != answer()) {
lawliet 1:642a8dbe076c 263 }
lawliet 1:642a8dbe076c 264 } else if(NULL != strstr(gprsBuffer,"$$+CMTI: \"SM\"")) { //SMS: $$+CMTI: "SM",24$$
lawliet 1:642a8dbe076c 265 char message[64];
lawliet 1:642a8dbe076c 266 if(0 != readSMS(gprsBuffer, message, check)) {
lawliet 1:642a8dbe076c 267 }
lawliet 1:642a8dbe076c 268 }
lawliet 1:642a8dbe076c 269 return 0;
lawliet 0:a5ae94727346 270 }
lawliet 0:a5ae94727346 271
lawliet 0:a5ae94727346 272
lawliet 0:a5ae94727346 273 /****************************************GPRS TCP CONNECT************************************/
lawliet 0:a5ae94727346 274
lawliet 2:16985da3a446 275 int GPRS::connectTCP(char *ip, char *port)
lawliet 0:a5ae94727346 276 {
lawliet 1:642a8dbe076c 277 char cipstart[50];
lawliet 3:48ee24a4b0f3 278 if(0 != sendCmdAndWaitForResp("AT+CSTT=\"CMNET\",\"\",\"\"\r\n", "OK", 5)) {
lawliet 1:642a8dbe076c 279 return -1;
lawliet 1:642a8dbe076c 280 }
lawliet 1:642a8dbe076c 281 sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r\n", ip, port);
lawliet 3:48ee24a4b0f3 282 if(0 != sendCmdAndWaitForResp(cipstart, "OK", 5)) {
lawliet 1:642a8dbe076c 283 return -1;
lawliet 1:642a8dbe076c 284 }
lawliet 1:642a8dbe076c 285 return 0;
lawliet 0:a5ae94727346 286 }
lawliet 2:16985da3a446 287 int GPRS::sendTCPData(char *data)
lawliet 0:a5ae94727346 288 {
lawliet 1:642a8dbe076c 289 char cmd[64];
lawliet 1:642a8dbe076c 290 int len = strlen(data);
lawliet 1:642a8dbe076c 291 snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
lawliet 3:48ee24a4b0f3 292 if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
lawliet 1:642a8dbe076c 293 return -1;
lawliet 1:642a8dbe076c 294 }
lawliet 3:48ee24a4b0f3 295 if(0 != sendCmdAndWaitForResp(data,"OK",DEFAULT_TIMEOUT)) {
lawliet 1:642a8dbe076c 296 return -1;
lawliet 1:642a8dbe076c 297 }
lawliet 1:642a8dbe076c 298 return 0;
lawliet 0:a5ae94727346 299 }
lawliet 0:a5ae94727346 300
lawliet 2:16985da3a446 301 int GPRS::closeTCP(void)
lawliet 0:a5ae94727346 302 {
lawliet 1:642a8dbe076c 303 sendCmd("AT+CIPCLOSE\r\n");
lawliet 1:642a8dbe076c 304 return 0;
lawliet 0:a5ae94727346 305 }
lawliet 0:a5ae94727346 306
lawliet 2:16985da3a446 307 int GPRS::shutTCP(void)
lawliet 0:a5ae94727346 308 {
lawliet 1:642a8dbe076c 309 sendCmd("AT+CIPSHUT\r\n");
lawliet 1:642a8dbe076c 310 return 0;
lawliet 0:a5ae94727346 311 }
lawliet 0:a5ae94727346 312
lawliet 0:a5ae94727346 313 /****************************************GPRS DEBUG******************************************/
lawliet 2:16985da3a446 314 void GPRS::serialDebug(PinName tx, PinName rx)
lawliet 1:642a8dbe076c 315 {
lawliet 1:642a8dbe076c 316 char buffer[64];
lawliet 1:642a8dbe076c 317 int count = 0;
lawliet 1:642a8dbe076c 318 Serial pc(tx,rx);
lawliet 1:642a8dbe076c 319 while(1) {
lawliet 1:642a8dbe076c 320 if(gprsSerial.readable()) {
lawliet 1:642a8dbe076c 321 while(gprsSerial.readable()) {
lawliet 1:642a8dbe076c 322 char c = gprsSerial.getc();
lawliet 1:642a8dbe076c 323 buffer[count++] = c;
lawliet 1:642a8dbe076c 324 if(count == 64) break;
lawliet 1:642a8dbe076c 325 }
lawliet 1:642a8dbe076c 326 pc.puts(buffer);
lawliet 1:642a8dbe076c 327 for(int i = 0; i < count; i++) {
lawliet 1:642a8dbe076c 328 buffer[i] = NULL;
lawliet 1:642a8dbe076c 329 }
lawliet 1:642a8dbe076c 330 count = 0;
lawliet 1:642a8dbe076c 331 }
lawliet 0:a5ae94727346 332
lawliet 1:642a8dbe076c 333 if(pc.readable()) {
lawliet 1:642a8dbe076c 334 gprsSerial.putc(pc.getc());
lawliet 1:642a8dbe076c 335 }
lawliet 1:642a8dbe076c 336 }
lawliet 0:a5ae94727346 337 }