ghrh

Committer:
lawliet
Date:
Thu Nov 14 09:39:58 2013 +0000
Revision:
0:a5ae94727346
Child:
1:642a8dbe076c
initial 0.1

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 0:a5ae94727346 23 #include <stdio.h>
lawliet 0:a5ae94727346 24 #include "mbed.h"
lawliet 0:a5ae94727346 25 #include "gprs.h"
lawliet 0:a5ae94727346 26
lawliet 0:a5ae94727346 27 #define TRUE 1
lawliet 0:a5ae94727346 28 #define FALSE 0
lawliet 0:a5ae94727346 29
lawliet 0:a5ae94727346 30 #define ERROR(x) pc.printf("ERROR:%s\r\n",x)
lawliet 0:a5ae94727346 31 #define DEBUG(x) pc.printf("DEBUG:%s\r\n",x);
lawliet 0:a5ae94727346 32
lawliet 0:a5ae94727346 33 #define DEFALUT_TIMEOUT 5
lawliet 0:a5ae94727346 34 #define PHONE_NUMBER "13925257382"
lawliet 0:a5ae94727346 35
lawliet 0:a5ae94727346 36 Serial pc(USBTX,USBRX);
lawliet 0:a5ae94727346 37 Serial gprsSerial(p9,p10); //software uart
lawliet 0:a5ae94727346 38 Timer timeCnt;
lawliet 0:a5ae94727346 39
lawliet 0:a5ae94727346 40 int gprs::init(void)
lawliet 0:a5ae94727346 41 {
lawliet 0:a5ae94727346 42 pc.baud(19200);
lawliet 0:a5ae94727346 43 gprsSerial.baud(19200);
lawliet 0:a5ae94727346 44 wait(0.5);
lawliet 0:a5ae94727346 45 if(0 != checkSIMStatus()){ //check SIM card status
lawliet 0:a5ae94727346 46 ERROR("checkSIMStatus");
lawliet 0:a5ae94727346 47 return -1;
lawliet 0:a5ae94727346 48 }
lawliet 0:a5ae94727346 49 if(checkSignalStrength()<1){ //check Signal Strength
lawliet 0:a5ae94727346 50 ERROR("Signal too weak");
lawliet 0:a5ae94727346 51 return -1;
lawliet 0:a5ae94727346 52 }
lawliet 0:a5ae94727346 53
lawliet 0:a5ae94727346 54 if(0 != networkInit()){
lawliet 0:a5ae94727346 55 ERROR("Network Init error");
lawliet 0:a5ae94727346 56 return -1;
lawliet 0:a5ae94727346 57 }
lawliet 0:a5ae94727346 58
lawliet 0:a5ae94727346 59 return 0;
lawliet 0:a5ae94727346 60 }
lawliet 0:a5ae94727346 61
lawliet 0:a5ae94727346 62 int gprs::readBuffer(char *buffer,int count)
lawliet 0:a5ae94727346 63 {
lawliet 0:a5ae94727346 64 int i = 0;
lawliet 0:a5ae94727346 65 timeCnt.start(); // start timer
lawliet 0:a5ae94727346 66 while(1){
lawliet 0:a5ae94727346 67 while (gprsSerial.readable()) {
lawliet 0:a5ae94727346 68 char c = gprsSerial.getc();
lawliet 0:a5ae94727346 69 if (c == '\r' || c == '\n') c = '$';
lawliet 0:a5ae94727346 70 buffer[i++] = c;
lawliet 0:a5ae94727346 71 pc.putc(c);
lawliet 0:a5ae94727346 72 if(i > count)break;
lawliet 0:a5ae94727346 73 }
lawliet 0:a5ae94727346 74 if(i > count)break;
lawliet 0:a5ae94727346 75 if(timeCnt.read() > DEFALUT_TIMEOUT){
lawliet 0:a5ae94727346 76 timeCnt.stop();
lawliet 0:a5ae94727346 77 timeCnt.reset();
lawliet 0:a5ae94727346 78 break;
lawliet 0:a5ae94727346 79 }
lawliet 0:a5ae94727346 80 }
lawliet 0:a5ae94727346 81 wait(0.5);
lawliet 0:a5ae94727346 82 while(gprsSerial.readable()){ // display the other thing..
lawliet 0:a5ae94727346 83 char c = gprsSerial.getc();
lawliet 0:a5ae94727346 84 pc.putc(c);
lawliet 0:a5ae94727346 85 }
lawliet 0:a5ae94727346 86 return 0;
lawliet 0:a5ae94727346 87 }
lawliet 0:a5ae94727346 88
lawliet 0:a5ae94727346 89 void cleanBuffer(char *buffer, int count)
lawliet 0:a5ae94727346 90 {
lawliet 0:a5ae94727346 91 for(int i=0; i < count; i++){
lawliet 0:a5ae94727346 92 buffer[i] = '\0';
lawliet 0:a5ae94727346 93 }
lawliet 0:a5ae94727346 94 }
lawliet 0:a5ae94727346 95
lawliet 0:a5ae94727346 96 void gprs::sendCmd(char *cmd)
lawliet 0:a5ae94727346 97 {
lawliet 0:a5ae94727346 98 gprsSerial.puts(cmd);
lawliet 0:a5ae94727346 99 }
lawliet 0:a5ae94727346 100
lawliet 0:a5ae94727346 101 int gprs::waitForResp(char *resp, int timeout)
lawliet 0:a5ae94727346 102 {
lawliet 0:a5ae94727346 103 #if 1
lawliet 0:a5ae94727346 104 int len = strlen(resp);
lawliet 0:a5ae94727346 105 int sum=0;
lawliet 0:a5ae94727346 106 timeCnt.start();
lawliet 0:a5ae94727346 107 #if 0
lawliet 0:a5ae94727346 108 while(1){
lawliet 0:a5ae94727346 109 if(gprsSerial.readable()){
lawliet 0:a5ae94727346 110 break;
lawliet 0:a5ae94727346 111 }
lawliet 0:a5ae94727346 112 wait(0.5);
lawliet 0:a5ae94727346 113 }
lawliet 0:a5ae94727346 114 #endif
lawliet 0:a5ae94727346 115 while(1){
lawliet 0:a5ae94727346 116 if(gprsSerial.readable()){
lawliet 0:a5ae94727346 117 char c = gprsSerial.getc();
lawliet 0:a5ae94727346 118 pc.putc(c);
lawliet 0:a5ae94727346 119 sum = (c==resp[sum]) ? sum+1 : 0;
lawliet 0:a5ae94727346 120 if(sum == len)break;
lawliet 0:a5ae94727346 121 }
lawliet 0:a5ae94727346 122 if(timeCnt.read() > timeout){ // time out
lawliet 0:a5ae94727346 123 timeCnt.stop();
lawliet 0:a5ae94727346 124 timeCnt.reset();
lawliet 0:a5ae94727346 125 ERROR("time out");
lawliet 0:a5ae94727346 126 return -1;
lawliet 0:a5ae94727346 127 }
lawliet 0:a5ae94727346 128 }
lawliet 0:a5ae94727346 129 timeCnt.stop(); // stop timer
lawliet 0:a5ae94727346 130 timeCnt.reset(); // clear timer
lawliet 0:a5ae94727346 131 while(gprsSerial.readable()){ // display the other thing..
lawliet 0:a5ae94727346 132 char c = gprsSerial.getc();
lawliet 0:a5ae94727346 133 pc.putc(c);
lawliet 0:a5ae94727346 134 }
lawliet 0:a5ae94727346 135 #endif
lawliet 0:a5ae94727346 136 return 0;
lawliet 0:a5ae94727346 137 }
lawliet 0:a5ae94727346 138
lawliet 0:a5ae94727346 139
lawliet 0:a5ae94727346 140
lawliet 0:a5ae94727346 141 int gprs::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
lawliet 0:a5ae94727346 142 {
lawliet 0:a5ae94727346 143 #if 0
lawliet 0:a5ae94727346 144 char gprsBuffer[32];
lawliet 0:a5ae94727346 145 cleanBuffer(gprsBuffer,32);
lawliet 0:a5ae94727346 146 sendCmd(cmd);
lawliet 0:a5ae94727346 147 readBuffer(gprsBuffer,4);
lawliet 0:a5ae94727346 148 if(0 == strncmp(resp,gprsBuffer,4)){
lawliet 0:a5ae94727346 149 return 0;
lawliet 0:a5ae94727346 150 }else{
lawliet 0:a5ae94727346 151 return -1;
lawliet 0:a5ae94727346 152 }
lawliet 0:a5ae94727346 153 #endif
lawliet 0:a5ae94727346 154 sendCmd(cmd);
lawliet 0:a5ae94727346 155 return waitForResp(resp,timeout);
lawliet 0:a5ae94727346 156 }
lawliet 0:a5ae94727346 157
lawliet 0:a5ae94727346 158 int gprs::checkSIMStatus(void)
lawliet 0:a5ae94727346 159 {
lawliet 0:a5ae94727346 160 char gprsBuffer[30];
lawliet 0:a5ae94727346 161 int count = 0;
lawliet 0:a5ae94727346 162 cleanBuffer(gprsBuffer,30);
lawliet 0:a5ae94727346 163 while(count < 3){
lawliet 0:a5ae94727346 164 sendCmd("AT+CPIN?\r\n");
lawliet 0:a5ae94727346 165 readBuffer(gprsBuffer,30);
lawliet 0:a5ae94727346 166 //DEBUG(gprsBuffer);
lawliet 0:a5ae94727346 167 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))){
lawliet 0:a5ae94727346 168 break;
lawliet 0:a5ae94727346 169 }
lawliet 0:a5ae94727346 170 count++;
lawliet 0:a5ae94727346 171 wait(1);
lawliet 0:a5ae94727346 172 }
lawliet 0:a5ae94727346 173
lawliet 0:a5ae94727346 174 if(count == 3){
lawliet 0:a5ae94727346 175 ERROR("Bad SIM Status");
lawliet 0:a5ae94727346 176 return -1;
lawliet 0:a5ae94727346 177 }
lawliet 0:a5ae94727346 178 return 0;
lawliet 0:a5ae94727346 179 }
lawliet 0:a5ae94727346 180
lawliet 0:a5ae94727346 181 int gprs::checkSignalStrength(void)
lawliet 0:a5ae94727346 182 {
lawliet 0:a5ae94727346 183 char gprsBuffer[100];
lawliet 0:a5ae94727346 184 int index,count = 0;
lawliet 0:a5ae94727346 185 cleanBuffer(gprsBuffer,100);
lawliet 0:a5ae94727346 186 while(count < 3){
lawliet 0:a5ae94727346 187 sendCmd("AT+CSQ\r\n");
lawliet 0:a5ae94727346 188 readBuffer(gprsBuffer,25);
lawliet 0:a5ae94727346 189 //DEBUG(gprsBuffer);
lawliet 0:a5ae94727346 190 if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0){
lawliet 0:a5ae94727346 191 break;
lawliet 0:a5ae94727346 192 }
lawliet 0:a5ae94727346 193 ERROR("checking signal strenght error, try again...");
lawliet 0:a5ae94727346 194 count++;
lawliet 0:a5ae94727346 195 wait(1);
lawliet 0:a5ae94727346 196 }
lawliet 0:a5ae94727346 197 if(count == 3){
lawliet 0:a5ae94727346 198 ERROR("AT+CSQ");
lawliet 0:a5ae94727346 199 return -1;
lawliet 0:a5ae94727346 200 }
lawliet 0:a5ae94727346 201 return index;
lawliet 0:a5ae94727346 202 }
lawliet 0:a5ae94727346 203
lawliet 0:a5ae94727346 204 int gprs::networkInit(void)
lawliet 0:a5ae94727346 205 {
lawliet 0:a5ae94727346 206 //for GPRS
lawliet 0:a5ae94727346 207 if(0 != sendCmdAndWaitForResp("AT+CGREG?\r\n","+CGREG: 0,1",DEFALUT_TIMEOUT)){ //Open GPRS
lawliet 0:a5ae94727346 208 ERROR("CGREG");
lawliet 0:a5ae94727346 209 return -1;
lawliet 0:a5ae94727346 210 }
lawliet 0:a5ae94727346 211 wait(1);
lawliet 0:a5ae94727346 212 if(0 != sendCmdAndWaitForResp("AT+CGATT?\r\n","+CGATT: 1",DEFALUT_TIMEOUT)){ //Set GPRS
lawliet 0:a5ae94727346 213 ERROR("CGATT");
lawliet 0:a5ae94727346 214 return -1;
lawliet 0:a5ae94727346 215 }
lawliet 0:a5ae94727346 216 return 0;
lawliet 0:a5ae94727346 217 }
lawliet 0:a5ae94727346 218
lawliet 0:a5ae94727346 219 int gprs::sendSMS(char *number, char *data)
lawliet 0:a5ae94727346 220 {
lawliet 0:a5ae94727346 221 char cmd[64];
lawliet 0:a5ae94727346 222 if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFALUT_TIMEOUT)){ // Set message mode to ASCII
lawliet 0:a5ae94727346 223 ERROR("CMGF");
lawliet 0:a5ae94727346 224 return -1;
lawliet 0:a5ae94727346 225 }
lawliet 0:a5ae94727346 226 wait(0.5);
lawliet 0:a5ae94727346 227 // Set the phone number
lawliet 0:a5ae94727346 228 snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number);
lawliet 0:a5ae94727346 229 pc.printf(cmd);
lawliet 0:a5ae94727346 230 if(0 != sendCmdAndWaitForResp(cmd,">",DEFALUT_TIMEOUT)){
lawliet 0:a5ae94727346 231 ERROR("CMGS");
lawliet 0:a5ae94727346 232 return -1;
lawliet 0:a5ae94727346 233 }
lawliet 0:a5ae94727346 234 wait(0.5);
lawliet 0:a5ae94727346 235 gprsSerial.puts(data);// Send Message
lawliet 0:a5ae94727346 236 wait(0.5);
lawliet 0:a5ae94727346 237 gprsSerial.putc(0x1A);//end mark
lawliet 0:a5ae94727346 238
lawliet 0:a5ae94727346 239 return 0;
lawliet 0:a5ae94727346 240 }
lawliet 0:a5ae94727346 241
lawliet 0:a5ae94727346 242 int gprs::sendSMS()
lawliet 0:a5ae94727346 243 {
lawliet 0:a5ae94727346 244 if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n","OK",DEFALUT_TIMEOUT)){
lawliet 0:a5ae94727346 245 ERROR("CMGF");
lawliet 0:a5ae94727346 246 return -1;
lawliet 0:a5ae94727346 247 }
lawliet 0:a5ae94727346 248 wait(1);
lawliet 0:a5ae94727346 249 if(0 != sendCmdAndWaitForResp("AT+CMGS=\"13925257382\"\r\n",">",DEFALUT_TIMEOUT)){
lawliet 0:a5ae94727346 250 ERROR("CMGS");
lawliet 0:a5ae94727346 251 return -1;
lawliet 0:a5ae94727346 252 }
lawliet 0:a5ae94727346 253 wait(1);
lawliet 0:a5ae94727346 254 gprsSerial.puts("lawiet test\n");
lawliet 0:a5ae94727346 255 wait(1);
lawliet 0:a5ae94727346 256 gprsSerial.putc(0x1A);
lawliet 0:a5ae94727346 257
lawliet 0:a5ae94727346 258 return 0;
lawliet 0:a5ae94727346 259 }
lawliet 0:a5ae94727346 260
lawliet 0:a5ae94727346 261 int gprs::readSMS(char *buffer, char *message, bool check)
lawliet 0:a5ae94727346 262 {
lawliet 0:a5ae94727346 263 int index,i = 0;
lawliet 0:a5ae94727346 264 char gprsBuffer[100];
lawliet 0:a5ae94727346 265 char *p,*s;
lawliet 0:a5ae94727346 266
lawliet 0:a5ae94727346 267 if(sscanf(buffer, "$$+CMTI: \"SM\",%d", &index)>0){
lawliet 0:a5ae94727346 268 DEBUG("Opening message...");
lawliet 0:a5ae94727346 269 gprsSerial.printf("AT+CMGR=%d\r\n", index);
lawliet 0:a5ae94727346 270 }else{
lawliet 0:a5ae94727346 271 ERROR("get Message");
lawliet 0:a5ae94727346 272 }
lawliet 0:a5ae94727346 273 cleanBuffer(gprsBuffer,100);
lawliet 0:a5ae94727346 274 readBuffer(gprsBuffer,100);
lawliet 0:a5ae94727346 275 //pc.printf(gprsBuffer); //AT+CMGR=17$$$$+CMGR: "REC UNREAD","+8613925257382","","13/11/11,10:36:13+32"$$Hello$$$$OK$$
lawliet 0:a5ae94727346 276 DEBUG(gprsBuffer);
lawliet 0:a5ae94727346 277 if(NULL == ( s = strstr(gprsBuffer,"+CMGR: \"REC UNREAD\""))){
lawliet 0:a5ae94727346 278 ERROR("get CMGR error");
lawliet 0:a5ae94727346 279 return -1;
lawliet 0:a5ae94727346 280 }
lawliet 0:a5ae94727346 281
lawliet 0:a5ae94727346 282 //check phone number
lawliet 0:a5ae94727346 283 if(check){
lawliet 0:a5ae94727346 284 char number[20];
lawliet 0:a5ae94727346 285 snprintf(number,sizeof(number),"\"+86%s\"",PHONE_NUMBER); //for China
lawliet 0:a5ae94727346 286 p = s + 20;
lawliet 0:a5ae94727346 287 if(0 != (strncmp(number,p,14))){
lawliet 0:a5ae94727346 288 ERROR("Phone Number error");
lawliet 0:a5ae94727346 289 return -1;
lawliet 0:a5ae94727346 290 }
lawliet 0:a5ae94727346 291 }
lawliet 0:a5ae94727346 292 p = s + 64;
lawliet 0:a5ae94727346 293 while(*p != '$'){
lawliet 0:a5ae94727346 294 message[i++] = *(p++);
lawliet 0:a5ae94727346 295 }
lawliet 0:a5ae94727346 296 message[i] = '\0';
lawliet 0:a5ae94727346 297
lawliet 0:a5ae94727346 298 DEBUG(message);
lawliet 0:a5ae94727346 299 return 0;
lawliet 0:a5ae94727346 300 }
lawliet 0:a5ae94727346 301
lawliet 0:a5ae94727346 302 int gprs::deleteSMS(int index)
lawliet 0:a5ae94727346 303 {
lawliet 0:a5ae94727346 304 char cmd[64];
lawliet 0:a5ae94727346 305 snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
lawliet 0:a5ae94727346 306 sendCmd(cmd);
lawliet 0:a5ae94727346 307 return 0;
lawliet 0:a5ae94727346 308 }
lawliet 0:a5ae94727346 309
lawliet 0:a5ae94727346 310 int gprs::callUp(char *number)
lawliet 0:a5ae94727346 311 {
lawliet 0:a5ae94727346 312 if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)){
lawliet 0:a5ae94727346 313 ERROR("COLP");
lawliet 0:a5ae94727346 314 return -1;
lawliet 0:a5ae94727346 315 }
lawliet 0:a5ae94727346 316 wait(1);
lawliet 0:a5ae94727346 317 gprsSerial.printf("\r\nATD%s;\r\n",NULL==number?PHONE_NUMBER:number);
lawliet 0:a5ae94727346 318 return 0;
lawliet 0:a5ae94727346 319 }
lawliet 0:a5ae94727346 320
lawliet 0:a5ae94727346 321 int gprs::answerWithCheck(char *gprsBuffer, bool check)
lawliet 0:a5ae94727346 322 {
lawliet 0:a5ae94727346 323 if(check){
lawliet 0:a5ae94727346 324 if(NULL == strstr(gprsBuffer,PHONE_NUMBER)){
lawliet 0:a5ae94727346 325 ERROR("phone number dismatch");
lawliet 0:a5ae94727346 326 return -1;
lawliet 0:a5ae94727346 327 }
lawliet 0:a5ae94727346 328 }
lawliet 0:a5ae94727346 329
lawliet 0:a5ae94727346 330 gprsSerial.printf("ATA\r\n");
lawliet 0:a5ae94727346 331 return 0;
lawliet 0:a5ae94727346 332 }
lawliet 0:a5ae94727346 333
lawliet 0:a5ae94727346 334 int gprs::loop(bool check)
lawliet 0:a5ae94727346 335 {
lawliet 0:a5ae94727346 336 char gprsBuffer[100];
lawliet 0:a5ae94727346 337 int i = 0;
lawliet 0:a5ae94727346 338 cleanBuffer(gprsBuffer,100);
lawliet 0:a5ae94727346 339 while(1){
lawliet 0:a5ae94727346 340 if(gprsSerial.readable()){
lawliet 0:a5ae94727346 341 break;
lawliet 0:a5ae94727346 342 }
lawliet 0:a5ae94727346 343 wait(1);
lawliet 0:a5ae94727346 344 }
lawliet 0:a5ae94727346 345
lawliet 0:a5ae94727346 346 timeCnt.start(); // start timer
lawliet 0:a5ae94727346 347 while(1){
lawliet 0:a5ae94727346 348 while (gprsSerial.readable()) {
lawliet 0:a5ae94727346 349 char c = gprsSerial.getc();
lawliet 0:a5ae94727346 350 if (c == '\r' || c == '\n') c = '$';
lawliet 0:a5ae94727346 351 gprsBuffer[i] = c;
lawliet 0:a5ae94727346 352 pc.putc(c);
lawliet 0:a5ae94727346 353 i++;
lawliet 0:a5ae94727346 354 if(i > 100){
lawliet 0:a5ae94727346 355 i = 0;
lawliet 0:a5ae94727346 356 break;
lawliet 0:a5ae94727346 357 }
lawliet 0:a5ae94727346 358 }
lawliet 0:a5ae94727346 359 if(timeCnt.read() > DEFALUT_TIMEOUT){ // time out
lawliet 0:a5ae94727346 360
lawliet 0:a5ae94727346 361 timeCnt.stop();
lawliet 0:a5ae94727346 362 timeCnt.reset();
lawliet 0:a5ae94727346 363 break;
lawliet 0:a5ae94727346 364 }
lawliet 0:a5ae94727346 365 }
lawliet 0:a5ae94727346 366
lawliet 0:a5ae94727346 367 pc.printf("\r\nDebug message is %s\r\n",gprsBuffer);
lawliet 0:a5ae94727346 368
lawliet 0:a5ae94727346 369 if(NULL != strstr(gprsBuffer,"$$RING$$$$+CLIP:")){ //it is Ringing
lawliet 0:a5ae94727346 370 if(0 != answerWithCheck(gprsBuffer, TRUE)){
lawliet 0:a5ae94727346 371 ERROR("answer");
lawliet 0:a5ae94727346 372 }
lawliet 0:a5ae94727346 373 }else if(NULL != strstr(gprsBuffer,"$$+CMTI: \"SM\"")){ //SMS: $$+CMTI: "SM",24$$
lawliet 0:a5ae94727346 374 char message[64];
lawliet 0:a5ae94727346 375 if(0 != readSMS(gprsBuffer, message, TRUE)){
lawliet 0:a5ae94727346 376 ERROR("readSMS");
lawliet 0:a5ae94727346 377 }
lawliet 0:a5ae94727346 378 pc.puts(message);
lawliet 0:a5ae94727346 379 }
lawliet 0:a5ae94727346 380 return 0;
lawliet 0:a5ae94727346 381 }
lawliet 0:a5ae94727346 382
lawliet 0:a5ae94727346 383
lawliet 0:a5ae94727346 384 /****************************************GPRS TCP CONNECT************************************/
lawliet 0:a5ae94727346 385
lawliet 0:a5ae94727346 386 int gprs::connectTCP(char *ip, char *port)
lawliet 0:a5ae94727346 387 {
lawliet 0:a5ae94727346 388 char cipstart[50];
lawliet 0:a5ae94727346 389 //char ipaddr[20];
lawliet 0:a5ae94727346 390 #if 0
lawliet 0:a5ae94727346 391 wait(1);
lawliet 0:a5ae94727346 392 if(0 != sendCmdAndWaitForResp("AT+CSTT\r\n","OK",20)){ //Set GPRS
lawliet 0:a5ae94727346 393 ERROR("CSTT");
lawliet 0:a5ae94727346 394 return -1;
lawliet 0:a5ae94727346 395 }
lawliet 0:a5ae94727346 396 wait(1);
lawliet 0:a5ae94727346 397 if(0 != sendCmdAndWaitForResp("AT+CIICR\r\n","OK",10)){ //Set APN
lawliet 0:a5ae94727346 398 ERROR("CIICR");
lawliet 0:a5ae94727346 399 return -1;
lawliet 0:a5ae94727346 400 }
lawliet 0:a5ae94727346 401 wait(2);
lawliet 0:a5ae94727346 402 sendCmd("AT+CIFSR\r\n");
lawliet 0:a5ae94727346 403 readBuffer(ipaddr,20);
lawliet 0:a5ae94727346 404 DEBUG("ipaddr=");
lawliet 0:a5ae94727346 405 DEBUG(ipaddr);
lawliet 0:a5ae94727346 406 wait(1);
lawliet 0:a5ae94727346 407 #endif
lawliet 0:a5ae94727346 408 #if 1
lawliet 0:a5ae94727346 409 sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r\n", ip, port);
lawliet 0:a5ae94727346 410 DEBUG(cipstart);
lawliet 0:a5ae94727346 411 if(0 != sendCmdAndWaitForResp(cipstart, "CONNECT OK", 10)){ // connect tcp
lawliet 0:a5ae94727346 412 ERROR("CIPSTART");
lawliet 0:a5ae94727346 413 return -1;
lawliet 0:a5ae94727346 414 }
lawliet 0:a5ae94727346 415 #endif
lawliet 0:a5ae94727346 416 return 0;
lawliet 0:a5ae94727346 417 }
lawliet 0:a5ae94727346 418 int gprs::sendTCPData(char *data)
lawliet 0:a5ae94727346 419 {
lawliet 0:a5ae94727346 420 char cmd[64];
lawliet 0:a5ae94727346 421 int len = strlen(data);
lawliet 0:a5ae94727346 422 snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
lawliet 0:a5ae94727346 423 DEBUG(cmd);
lawliet 0:a5ae94727346 424 if(0 != sendCmdAndWaitForResp(cmd,">",DEFALUT_TIMEOUT)){
lawliet 0:a5ae94727346 425 ERROR("CIPSEND");
lawliet 0:a5ae94727346 426 return -1;
lawliet 0:a5ae94727346 427 }
lawliet 0:a5ae94727346 428 if(0 != sendCmdAndWaitForResp(data,"SEND OK",DEFALUT_TIMEOUT)){
lawliet 0:a5ae94727346 429 ERROR("SendTCPData");
lawliet 0:a5ae94727346 430 return -1;
lawliet 0:a5ae94727346 431 }
lawliet 0:a5ae94727346 432 return 0;
lawliet 0:a5ae94727346 433 }
lawliet 0:a5ae94727346 434
lawliet 0:a5ae94727346 435 int gprs::closeTCP(void)
lawliet 0:a5ae94727346 436 {
lawliet 0:a5ae94727346 437 sendCmd("AT+CIPCLOSE\r\n");
lawliet 0:a5ae94727346 438 return 0;
lawliet 0:a5ae94727346 439 }
lawliet 0:a5ae94727346 440
lawliet 0:a5ae94727346 441 int gprs::shutTCP(void)
lawliet 0:a5ae94727346 442 {
lawliet 0:a5ae94727346 443 sendCmd("AT+CIPSHUT\r\n");
lawliet 0:a5ae94727346 444 return 0;
lawliet 0:a5ae94727346 445 }
lawliet 0:a5ae94727346 446
lawliet 0:a5ae94727346 447 /****************************************GPRS DEBUG******************************************/
lawliet 0:a5ae94727346 448
lawliet 0:a5ae94727346 449 void gprs::serialDebug(void)
lawliet 0:a5ae94727346 450 {
lawliet 0:a5ae94727346 451 char buffer[64];
lawliet 0:a5ae94727346 452 int count = 0;
lawliet 0:a5ae94727346 453 while(1){
lawliet 0:a5ae94727346 454 if(gprsSerial.readable()){
lawliet 0:a5ae94727346 455 while(gprsSerial.readable()){
lawliet 0:a5ae94727346 456 char c = gprsSerial.getc();
lawliet 0:a5ae94727346 457 buffer[count++] = c;
lawliet 0:a5ae94727346 458 if(count == 64) break;
lawliet 0:a5ae94727346 459 }
lawliet 0:a5ae94727346 460 pc.puts(buffer);
lawliet 0:a5ae94727346 461 for(int i = 0; i < count; i++){
lawliet 0:a5ae94727346 462 buffer[i] = NULL;
lawliet 0:a5ae94727346 463 }
lawliet 0:a5ae94727346 464 count = 0;
lawliet 0:a5ae94727346 465 }
lawliet 0:a5ae94727346 466
lawliet 0:a5ae94727346 467 if(pc.readable()){
lawliet 0:a5ae94727346 468 gprsSerial.putc(pc.getc());
lawliet 0:a5ae94727346 469 }
lawliet 0:a5ae94727346 470 }
lawliet 0:a5ae94727346 471 }
lawliet 0:a5ae94727346 472