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