tes sim 5360
SIM5360.cpp@0:bf27ed7867b7, 2020-03-17 (annotated)
- Committer:
- irsanjul
- Date:
- Tue Mar 17 09:22:08 2020 +0000
- Revision:
- 0:bf27ed7867b7
tes sim 5360
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
irsanjul | 0:bf27ed7867b7 | 1 | #include "SIM5360.h" |
irsanjul | 0:bf27ed7867b7 | 2 | |
irsanjul | 0:bf27ed7867b7 | 3 | int GSM::readBuffer(char *buffer,int count) |
irsanjul | 0:bf27ed7867b7 | 4 | { |
irsanjul | 0:bf27ed7867b7 | 5 | int i = 0; |
irsanjul | 0:bf27ed7867b7 | 6 | timeCnt.start(); // start timer |
irsanjul | 0:bf27ed7867b7 | 7 | while(1) { |
irsanjul | 0:bf27ed7867b7 | 8 | while (gprsSerial.readable()) { |
irsanjul | 0:bf27ed7867b7 | 9 | char c = gprsSerial.getc(); |
irsanjul | 0:bf27ed7867b7 | 10 | if (c == '\r' || c == '\n') c = '$'; |
irsanjul | 0:bf27ed7867b7 | 11 | buffer[i++] = c; |
irsanjul | 0:bf27ed7867b7 | 12 | if(i > count)break; |
irsanjul | 0:bf27ed7867b7 | 13 | } |
irsanjul | 0:bf27ed7867b7 | 14 | if(i > count)break; |
irsanjul | 0:bf27ed7867b7 | 15 | if(timeCnt.read() > DEFAULT_TIMEOUT) { |
irsanjul | 0:bf27ed7867b7 | 16 | timeCnt.stop(); |
irsanjul | 0:bf27ed7867b7 | 17 | timeCnt.reset(); |
irsanjul | 0:bf27ed7867b7 | 18 | break; |
irsanjul | 0:bf27ed7867b7 | 19 | } |
irsanjul | 0:bf27ed7867b7 | 20 | } |
irsanjul | 0:bf27ed7867b7 | 21 | wait(0.5); |
irsanjul | 0:bf27ed7867b7 | 22 | while(gprsSerial.readable()) { // display the other thing.. |
irsanjul | 0:bf27ed7867b7 | 23 | char c = gprsSerial.getc(); |
irsanjul | 0:bf27ed7867b7 | 24 | } |
irsanjul | 0:bf27ed7867b7 | 25 | return 0; |
irsanjul | 0:bf27ed7867b7 | 26 | } |
irsanjul | 0:bf27ed7867b7 | 27 | |
irsanjul | 0:bf27ed7867b7 | 28 | void cleanBuffer(char *buffer, int count) |
irsanjul | 0:bf27ed7867b7 | 29 | { |
irsanjul | 0:bf27ed7867b7 | 30 | for(int i=0; i < count; i++) { |
irsanjul | 0:bf27ed7867b7 | 31 | buffer[i] = '\0'; |
irsanjul | 0:bf27ed7867b7 | 32 | } |
irsanjul | 0:bf27ed7867b7 | 33 | } |
irsanjul | 0:bf27ed7867b7 | 34 | |
irsanjul | 0:bf27ed7867b7 | 35 | void GSM::purge(void) |
irsanjul | 0:bf27ed7867b7 | 36 | { |
irsanjul | 0:bf27ed7867b7 | 37 | while(gprsSerial.readable()) |
irsanjul | 0:bf27ed7867b7 | 38 | { |
irsanjul | 0:bf27ed7867b7 | 39 | gprsSerial.getc(); |
irsanjul | 0:bf27ed7867b7 | 40 | } |
irsanjul | 0:bf27ed7867b7 | 41 | } |
irsanjul | 0:bf27ed7867b7 | 42 | |
irsanjul | 0:bf27ed7867b7 | 43 | void GSM::sendCmd(char *cmd) |
irsanjul | 0:bf27ed7867b7 | 44 | { |
irsanjul | 0:bf27ed7867b7 | 45 | gprsSerial.puts(cmd); |
irsanjul | 0:bf27ed7867b7 | 46 | } |
irsanjul | 0:bf27ed7867b7 | 47 | |
irsanjul | 0:bf27ed7867b7 | 48 | int GSM::waitForResp(char *resp, int timeout) |
irsanjul | 0:bf27ed7867b7 | 49 | { |
irsanjul | 0:bf27ed7867b7 | 50 | int len = strlen(resp); |
irsanjul | 0:bf27ed7867b7 | 51 | int sum=0; |
irsanjul | 0:bf27ed7867b7 | 52 | timeCnt.start(); |
irsanjul | 0:bf27ed7867b7 | 53 | |
irsanjul | 0:bf27ed7867b7 | 54 | while(1) { |
irsanjul | 0:bf27ed7867b7 | 55 | if(gprsSerial.readable()) { |
irsanjul | 0:bf27ed7867b7 | 56 | char c = gprsSerial.getc(); |
irsanjul | 0:bf27ed7867b7 | 57 | sum = (c==resp[sum]) ? sum+1 : 0; |
irsanjul | 0:bf27ed7867b7 | 58 | if(sum == len)break; |
irsanjul | 0:bf27ed7867b7 | 59 | } |
irsanjul | 0:bf27ed7867b7 | 60 | if(timeCnt.read() > timeout) { // time out |
irsanjul | 0:bf27ed7867b7 | 61 | timeCnt.stop(); |
irsanjul | 0:bf27ed7867b7 | 62 | timeCnt.reset(); |
irsanjul | 0:bf27ed7867b7 | 63 | return -1; |
irsanjul | 0:bf27ed7867b7 | 64 | } |
irsanjul | 0:bf27ed7867b7 | 65 | } |
irsanjul | 0:bf27ed7867b7 | 66 | timeCnt.stop(); // stop timer |
irsanjul | 0:bf27ed7867b7 | 67 | timeCnt.reset(); // clear timer |
irsanjul | 0:bf27ed7867b7 | 68 | while(gprsSerial.readable()) { // display the other thing.. |
irsanjul | 0:bf27ed7867b7 | 69 | char c = gprsSerial.getc(); |
irsanjul | 0:bf27ed7867b7 | 70 | } |
irsanjul | 0:bf27ed7867b7 | 71 | |
irsanjul | 0:bf27ed7867b7 | 72 | return 0; |
irsanjul | 0:bf27ed7867b7 | 73 | } |
irsanjul | 0:bf27ed7867b7 | 74 | |
irsanjul | 0:bf27ed7867b7 | 75 | int GSM::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout) |
irsanjul | 0:bf27ed7867b7 | 76 | { |
irsanjul | 0:bf27ed7867b7 | 77 | sendCmd(cmd); |
irsanjul | 0:bf27ed7867b7 | 78 | return waitForResp(resp,timeout); |
irsanjul | 0:bf27ed7867b7 | 79 | } |
irsanjul | 0:bf27ed7867b7 | 80 | |
irsanjul | 0:bf27ed7867b7 | 81 | int GSM::powerCheck(void) |
irsanjul | 0:bf27ed7867b7 | 82 | { |
irsanjul | 0:bf27ed7867b7 | 83 | return sendCmdAndWaitForResp("AT\r\n", "OK", 2); |
irsanjul | 0:bf27ed7867b7 | 84 | } |
irsanjul | 0:bf27ed7867b7 | 85 | |
irsanjul | 0:bf27ed7867b7 | 86 | int GSM::init(void) |
irsanjul | 0:bf27ed7867b7 | 87 | { |
irsanjul | 0:bf27ed7867b7 | 88 | int i=10; |
irsanjul | 0:bf27ed7867b7 | 89 | |
irsanjul | 0:bf27ed7867b7 | 90 | while (i--) { |
irsanjul | 0:bf27ed7867b7 | 91 | // purge any messages |
irsanjul | 0:bf27ed7867b7 | 92 | purge(); |
irsanjul | 0:bf27ed7867b7 | 93 | |
irsanjul | 0:bf27ed7867b7 | 94 | // check interface |
irsanjul | 0:bf27ed7867b7 | 95 | int r = sendCmdAndWaitForResp("AT\r\n", "OK", DEFAULT_TIMEOUT); |
irsanjul | 0:bf27ed7867b7 | 96 | wait_ms(100); |
irsanjul | 0:bf27ed7867b7 | 97 | if(r == 0) break; |
irsanjul | 0:bf27ed7867b7 | 98 | } |
irsanjul | 0:bf27ed7867b7 | 99 | if (i < 0) { |
irsanjul | 0:bf27ed7867b7 | 100 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 101 | } |
irsanjul | 0:bf27ed7867b7 | 102 | |
irsanjul | 0:bf27ed7867b7 | 103 | if(0 != sendCmdAndWaitForResp("ATE0\r\n", "OK", DEFAULT_TIMEOUT)) |
irsanjul | 0:bf27ed7867b7 | 104 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 105 | |
irsanjul | 0:bf27ed7867b7 | 106 | if(0 != sendCmdAndWaitForResp("AT+CMEE=2\r\n", "OK", DEFAULT_TIMEOUT)) |
irsanjul | 0:bf27ed7867b7 | 107 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 108 | |
irsanjul | 0:bf27ed7867b7 | 109 | if(0 != checkSIMStatus()) { |
irsanjul | 0:bf27ed7867b7 | 110 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 111 | } |
irsanjul | 0:bf27ed7867b7 | 112 | |
irsanjul | 0:bf27ed7867b7 | 113 | if(0 != sendCmdAndWaitForResp("AT+CGREG=1\r\n", "OK", DEFAULT_TIMEOUT)){ |
irsanjul | 0:bf27ed7867b7 | 114 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 115 | } |
irsanjul | 0:bf27ed7867b7 | 116 | |
irsanjul | 0:bf27ed7867b7 | 117 | if(0 != sendCmdAndWaitForResp("AT+CREG=1\r\n", "OK", DEFAULT_TIMEOUT)){ |
irsanjul | 0:bf27ed7867b7 | 118 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 119 | } |
irsanjul | 0:bf27ed7867b7 | 120 | |
irsanjul | 0:bf27ed7867b7 | 121 | if(0 != settingSMS()){ |
irsanjul | 0:bf27ed7867b7 | 122 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 123 | } |
irsanjul | 0:bf27ed7867b7 | 124 | |
irsanjul | 0:bf27ed7867b7 | 125 | return 0; |
irsanjul | 0:bf27ed7867b7 | 126 | |
irsanjul | 0:bf27ed7867b7 | 127 | failure: |
irsanjul | 0:bf27ed7867b7 | 128 | return -1; |
irsanjul | 0:bf27ed7867b7 | 129 | } |
irsanjul | 0:bf27ed7867b7 | 130 | |
irsanjul | 0:bf27ed7867b7 | 131 | int GSM::registerNet(void) |
irsanjul | 0:bf27ed7867b7 | 132 | { |
irsanjul | 0:bf27ed7867b7 | 133 | int ret = -1; |
irsanjul | 0:bf27ed7867b7 | 134 | int i = 10; |
irsanjul | 0:bf27ed7867b7 | 135 | while(i--){ |
irsanjul | 0:bf27ed7867b7 | 136 | int r = checkNetStatus(); |
irsanjul | 0:bf27ed7867b7 | 137 | if(r == 0){ |
irsanjul | 0:bf27ed7867b7 | 138 | ret = 0; |
irsanjul | 0:bf27ed7867b7 | 139 | break; |
irsanjul | 0:bf27ed7867b7 | 140 | } |
irsanjul | 0:bf27ed7867b7 | 141 | wait_ms(1000); |
irsanjul | 0:bf27ed7867b7 | 142 | } |
irsanjul | 0:bf27ed7867b7 | 143 | |
irsanjul | 0:bf27ed7867b7 | 144 | if(checkSignalStrength()<1) { |
irsanjul | 0:bf27ed7867b7 | 145 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 146 | } |
irsanjul | 0:bf27ed7867b7 | 147 | |
irsanjul | 0:bf27ed7867b7 | 148 | failure: |
irsanjul | 0:bf27ed7867b7 | 149 | return ret; |
irsanjul | 0:bf27ed7867b7 | 150 | } |
irsanjul | 0:bf27ed7867b7 | 151 | |
irsanjul | 0:bf27ed7867b7 | 152 | int GSM::checkNetStatus(void) |
irsanjul | 0:bf27ed7867b7 | 153 | { |
irsanjul | 0:bf27ed7867b7 | 154 | int netReg = -1; |
irsanjul | 0:bf27ed7867b7 | 155 | int gprsReg = -1; |
irsanjul | 0:bf27ed7867b7 | 156 | char gprsBuffer[30]; |
irsanjul | 0:bf27ed7867b7 | 157 | cleanBuffer(gprsBuffer,30); |
irsanjul | 0:bf27ed7867b7 | 158 | |
irsanjul | 0:bf27ed7867b7 | 159 | sendCmd("AT+CREG?\r\n"); |
irsanjul | 0:bf27ed7867b7 | 160 | readBuffer(gprsBuffer,30); |
irsanjul | 0:bf27ed7867b7 | 161 | if((NULL != strstr(gprsBuffer,"+CREG: 0,1"))) { |
irsanjul | 0:bf27ed7867b7 | 162 | netReg = 0; |
irsanjul | 0:bf27ed7867b7 | 163 | } |
irsanjul | 0:bf27ed7867b7 | 164 | |
irsanjul | 0:bf27ed7867b7 | 165 | wait_ms(100); |
irsanjul | 0:bf27ed7867b7 | 166 | sendCmd("AT+CREG?\r\n"); |
irsanjul | 0:bf27ed7867b7 | 167 | readBuffer(gprsBuffer,30); |
irsanjul | 0:bf27ed7867b7 | 168 | if((NULL != strstr(gprsBuffer,"+CGREG: 0,1"))) { |
irsanjul | 0:bf27ed7867b7 | 169 | gprsReg = 0; |
irsanjul | 0:bf27ed7867b7 | 170 | } |
irsanjul | 0:bf27ed7867b7 | 171 | |
irsanjul | 0:bf27ed7867b7 | 172 | return netReg | gprsReg; |
irsanjul | 0:bf27ed7867b7 | 173 | } |
irsanjul | 0:bf27ed7867b7 | 174 | |
irsanjul | 0:bf27ed7867b7 | 175 | int GSM::checkSIMStatus(void) |
irsanjul | 0:bf27ed7867b7 | 176 | { |
irsanjul | 0:bf27ed7867b7 | 177 | char gprsBuffer[30]; |
irsanjul | 0:bf27ed7867b7 | 178 | int count = 0; |
irsanjul | 0:bf27ed7867b7 | 179 | cleanBuffer(gprsBuffer,30); |
irsanjul | 0:bf27ed7867b7 | 180 | while(count < 3) { |
irsanjul | 0:bf27ed7867b7 | 181 | sendCmd("AT+CPIN?\r\n"); |
irsanjul | 0:bf27ed7867b7 | 182 | readBuffer(gprsBuffer,30); |
irsanjul | 0:bf27ed7867b7 | 183 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { |
irsanjul | 0:bf27ed7867b7 | 184 | break; |
irsanjul | 0:bf27ed7867b7 | 185 | } |
irsanjul | 0:bf27ed7867b7 | 186 | count++; |
irsanjul | 0:bf27ed7867b7 | 187 | wait(1); |
irsanjul | 0:bf27ed7867b7 | 188 | } |
irsanjul | 0:bf27ed7867b7 | 189 | |
irsanjul | 0:bf27ed7867b7 | 190 | if(count == 3) { |
irsanjul | 0:bf27ed7867b7 | 191 | return -1; |
irsanjul | 0:bf27ed7867b7 | 192 | } |
irsanjul | 0:bf27ed7867b7 | 193 | return 0; |
irsanjul | 0:bf27ed7867b7 | 194 | } |
irsanjul | 0:bf27ed7867b7 | 195 | |
irsanjul | 0:bf27ed7867b7 | 196 | int GSM::checkSignalStrength(void) |
irsanjul | 0:bf27ed7867b7 | 197 | { |
irsanjul | 0:bf27ed7867b7 | 198 | char gprsBuffer[100]; |
irsanjul | 0:bf27ed7867b7 | 199 | int index,count = 0; |
irsanjul | 0:bf27ed7867b7 | 200 | cleanBuffer(gprsBuffer,100); |
irsanjul | 0:bf27ed7867b7 | 201 | while(count < 3) { |
irsanjul | 0:bf27ed7867b7 | 202 | sendCmd("AT+CSQ\r\n"); |
irsanjul | 0:bf27ed7867b7 | 203 | readBuffer(gprsBuffer,25); |
irsanjul | 0:bf27ed7867b7 | 204 | if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { |
irsanjul | 0:bf27ed7867b7 | 205 | break; |
irsanjul | 0:bf27ed7867b7 | 206 | } |
irsanjul | 0:bf27ed7867b7 | 207 | count++; |
irsanjul | 0:bf27ed7867b7 | 208 | wait(1); |
irsanjul | 0:bf27ed7867b7 | 209 | } |
irsanjul | 0:bf27ed7867b7 | 210 | if(count == 3) { |
irsanjul | 0:bf27ed7867b7 | 211 | return -1; |
irsanjul | 0:bf27ed7867b7 | 212 | } |
irsanjul | 0:bf27ed7867b7 | 213 | return index; |
irsanjul | 0:bf27ed7867b7 | 214 | } |
irsanjul | 0:bf27ed7867b7 | 215 | |
irsanjul | 0:bf27ed7867b7 | 216 | int GSM::settingSMS(void) |
irsanjul | 0:bf27ed7867b7 | 217 | { |
irsanjul | 0:bf27ed7867b7 | 218 | if(0 != sendCmdAndWaitForResp("AT+CNMI=2,2\r\n", "OK", DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 219 | return -1; |
irsanjul | 0:bf27ed7867b7 | 220 | } |
irsanjul | 0:bf27ed7867b7 | 221 | if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 222 | return -1; |
irsanjul | 0:bf27ed7867b7 | 223 | } |
irsanjul | 0:bf27ed7867b7 | 224 | return 0; |
irsanjul | 0:bf27ed7867b7 | 225 | } |
irsanjul | 0:bf27ed7867b7 | 226 | |
irsanjul | 0:bf27ed7867b7 | 227 | int GSM::sendSMS(char *number, char *data) |
irsanjul | 0:bf27ed7867b7 | 228 | { |
irsanjul | 0:bf27ed7867b7 | 229 | char cmd[64]; |
irsanjul | 0:bf27ed7867b7 | 230 | while(gprsSerial.readable()) { |
irsanjul | 0:bf27ed7867b7 | 231 | char c = gprsSerial.getc(); |
irsanjul | 0:bf27ed7867b7 | 232 | } |
irsanjul | 0:bf27ed7867b7 | 233 | snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n",number); |
irsanjul | 0:bf27ed7867b7 | 234 | if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 235 | return -1; |
irsanjul | 0:bf27ed7867b7 | 236 | } |
irsanjul | 0:bf27ed7867b7 | 237 | wait(1); |
irsanjul | 0:bf27ed7867b7 | 238 | gprsSerial.puts(data); |
irsanjul | 0:bf27ed7867b7 | 239 | gprsSerial.putc((char)0x1a); |
irsanjul | 0:bf27ed7867b7 | 240 | return 0; |
irsanjul | 0:bf27ed7867b7 | 241 | } |
irsanjul | 0:bf27ed7867b7 | 242 | |
irsanjul | 0:bf27ed7867b7 | 243 | int GSM::readSMS(char *message, int index) |
irsanjul | 0:bf27ed7867b7 | 244 | { |
irsanjul | 0:bf27ed7867b7 | 245 | int i = 0; |
irsanjul | 0:bf27ed7867b7 | 246 | char gprsBuffer[100]; |
irsanjul | 0:bf27ed7867b7 | 247 | char *p,*s; |
irsanjul | 0:bf27ed7867b7 | 248 | gprsSerial.printf("AT+CMGR=%d\r\n",index); |
irsanjul | 0:bf27ed7867b7 | 249 | cleanBuffer(gprsBuffer,100); |
irsanjul | 0:bf27ed7867b7 | 250 | readBuffer(gprsBuffer,100); |
irsanjul | 0:bf27ed7867b7 | 251 | if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { |
irsanjul | 0:bf27ed7867b7 | 252 | return -1; |
irsanjul | 0:bf27ed7867b7 | 253 | } |
irsanjul | 0:bf27ed7867b7 | 254 | if(NULL != ( s = strstr(gprsBuffer,"+32"))) { |
irsanjul | 0:bf27ed7867b7 | 255 | p = s + 6; |
irsanjul | 0:bf27ed7867b7 | 256 | while((*p != '$')&&(i < SMS_MAX_LENGTH-1)) { |
irsanjul | 0:bf27ed7867b7 | 257 | message[i++] = *(p++); |
irsanjul | 0:bf27ed7867b7 | 258 | } |
irsanjul | 0:bf27ed7867b7 | 259 | message[i] = '\0'; |
irsanjul | 0:bf27ed7867b7 | 260 | } |
irsanjul | 0:bf27ed7867b7 | 261 | return 0; |
irsanjul | 0:bf27ed7867b7 | 262 | } |
irsanjul | 0:bf27ed7867b7 | 263 | |
irsanjul | 0:bf27ed7867b7 | 264 | int GSM::deleteSMS(int index) |
irsanjul | 0:bf27ed7867b7 | 265 | { |
irsanjul | 0:bf27ed7867b7 | 266 | char cmd[32]; |
irsanjul | 0:bf27ed7867b7 | 267 | snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); |
irsanjul | 0:bf27ed7867b7 | 268 | sendCmd(cmd); |
irsanjul | 0:bf27ed7867b7 | 269 | return 0; |
irsanjul | 0:bf27ed7867b7 | 270 | } |
irsanjul | 0:bf27ed7867b7 | 271 | |
irsanjul | 0:bf27ed7867b7 | 272 | int GSM::getSMS(char* message) |
irsanjul | 0:bf27ed7867b7 | 273 | { |
irsanjul | 0:bf27ed7867b7 | 274 | if(NULL != messageBuffer) { |
irsanjul | 0:bf27ed7867b7 | 275 | strncpy(message,messageBuffer,SMS_MAX_LENGTH); |
irsanjul | 0:bf27ed7867b7 | 276 | } |
irsanjul | 0:bf27ed7867b7 | 277 | return 0; |
irsanjul | 0:bf27ed7867b7 | 278 | } |
irsanjul | 0:bf27ed7867b7 | 279 | |
irsanjul | 0:bf27ed7867b7 | 280 | int GSM::callUp(char *number) |
irsanjul | 0:bf27ed7867b7 | 281 | { |
irsanjul | 0:bf27ed7867b7 | 282 | if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) { |
irsanjul | 0:bf27ed7867b7 | 283 | return -1; |
irsanjul | 0:bf27ed7867b7 | 284 | } |
irsanjul | 0:bf27ed7867b7 | 285 | wait(1); |
irsanjul | 0:bf27ed7867b7 | 286 | gprsSerial.printf("\r\nATD%s;\r\n",NULL==number?phoneNumber:number); |
irsanjul | 0:bf27ed7867b7 | 287 | return 0; |
irsanjul | 0:bf27ed7867b7 | 288 | } |
irsanjul | 0:bf27ed7867b7 | 289 | |
irsanjul | 0:bf27ed7867b7 | 290 | int GSM::answer(void) |
irsanjul | 0:bf27ed7867b7 | 291 | { |
irsanjul | 0:bf27ed7867b7 | 292 | gprsSerial.printf("ATA\r\n"); |
irsanjul | 0:bf27ed7867b7 | 293 | return 0; |
irsanjul | 0:bf27ed7867b7 | 294 | } |
irsanjul | 0:bf27ed7867b7 | 295 | |
irsanjul | 0:bf27ed7867b7 | 296 | int GSM::join(char* apn, char* userName, char* passWord) |
irsanjul | 0:bf27ed7867b7 | 297 | { |
irsanjul | 0:bf27ed7867b7 | 298 | char gprsBuffer[64]; |
irsanjul | 0:bf27ed7867b7 | 299 | int ret = 0; |
irsanjul | 0:bf27ed7867b7 | 300 | |
irsanjul | 0:bf27ed7867b7 | 301 | cleanBuffer(gprsBuffer,64); |
irsanjul | 0:bf27ed7867b7 | 302 | snprintf(gprsBuffer,sizeof(gprsBuffer),"AT+CGSOCKCONT=1,\"IP\",\"%s\"\r\n", apn); |
irsanjul | 0:bf27ed7867b7 | 303 | if(0 != sendCmdAndWaitForResp(gprsBuffer, "OK", DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 304 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 305 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 306 | } |
irsanjul | 0:bf27ed7867b7 | 307 | |
irsanjul | 0:bf27ed7867b7 | 308 | cleanBuffer(gprsBuffer,64); |
irsanjul | 0:bf27ed7867b7 | 309 | snprintf(gprsBuffer,sizeof(gprsBuffer),"AT+CSOCKAUTH=1,1,\"%s\",\"%s\"\r\n", passWord, userName); |
irsanjul | 0:bf27ed7867b7 | 310 | if(0 != sendCmdAndWaitForResp(gprsBuffer, "OK", DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 311 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 312 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 313 | } |
irsanjul | 0:bf27ed7867b7 | 314 | |
irsanjul | 0:bf27ed7867b7 | 315 | if(0 != sendCmdAndWaitForResp("AT+CSOCKSETPN=1\r\n", "OK", DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 316 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 317 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 318 | } |
irsanjul | 0:bf27ed7867b7 | 319 | |
irsanjul | 0:bf27ed7867b7 | 320 | if(0 != sendCmdAndWaitForResp("AT+CIPMODE=0\r\n", "OK", DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 321 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 322 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 323 | } |
irsanjul | 0:bf27ed7867b7 | 324 | |
irsanjul | 0:bf27ed7867b7 | 325 | if(0 != sendCmdAndWaitForResp("AT+NETOPEN\r\n", "OK", DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 326 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 327 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 328 | } |
irsanjul | 0:bf27ed7867b7 | 329 | |
irsanjul | 0:bf27ed7867b7 | 330 | failure: |
irsanjul | 0:bf27ed7867b7 | 331 | return ret; |
irsanjul | 0:bf27ed7867b7 | 332 | |
irsanjul | 0:bf27ed7867b7 | 333 | } |
irsanjul | 0:bf27ed7867b7 | 334 | |
irsanjul | 0:bf27ed7867b7 | 335 | int GSM::SetBlocking(int netopen_to, int cipopen_to, int cipsend_to) |
irsanjul | 0:bf27ed7867b7 | 336 | { |
irsanjul | 0:bf27ed7867b7 | 337 | int ret = 0; |
irsanjul | 0:bf27ed7867b7 | 338 | char gprsBuffer[64]; |
irsanjul | 0:bf27ed7867b7 | 339 | |
irsanjul | 0:bf27ed7867b7 | 340 | cleanBuffer(gprsBuffer,64); |
irsanjul | 0:bf27ed7867b7 | 341 | snprintf(gprsBuffer,sizeof(gprsBuffer),"AT+CIPTIMEOUT=%d,%d,%d\r\n", netopen_to, cipopen_to, cipsend_to); |
irsanjul | 0:bf27ed7867b7 | 342 | if(0 != sendCmdAndWaitForResp(gprsBuffer, "OK", DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 343 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 344 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 345 | } |
irsanjul | 0:bf27ed7867b7 | 346 | |
irsanjul | 0:bf27ed7867b7 | 347 | failure: |
irsanjul | 0:bf27ed7867b7 | 348 | return ret; |
irsanjul | 0:bf27ed7867b7 | 349 | } |
irsanjul | 0:bf27ed7867b7 | 350 | |
irsanjul | 0:bf27ed7867b7 | 351 | int GSM::connectTCP(char *ip, int port) |
irsanjul | 0:bf27ed7867b7 | 352 | { |
irsanjul | 0:bf27ed7867b7 | 353 | int ret = 0; |
irsanjul | 0:bf27ed7867b7 | 354 | char cipstart[64]; |
irsanjul | 0:bf27ed7867b7 | 355 | #if 0 |
irsanjul | 0:bf27ed7867b7 | 356 | if(0 != sendCmdAndWaitForResp("AT+CIPOPEN=0,\"TCP\",\"\",\r\n", "OK", 5)) { |
irsanjul | 0:bf27ed7867b7 | 357 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 358 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 359 | } |
irsanjul | 0:bf27ed7867b7 | 360 | #endif |
irsanjul | 0:bf27ed7867b7 | 361 | sprintf(cipstart, "AT+CIPOPEN=0,\"TCP\",\"%s\",%d\r\n", ip, port); |
irsanjul | 0:bf27ed7867b7 | 362 | if(0 != sendCmdAndWaitForResp(cipstart, "OK", DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 363 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 364 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 365 | } |
irsanjul | 0:bf27ed7867b7 | 366 | |
irsanjul | 0:bf27ed7867b7 | 367 | purge(); |
irsanjul | 0:bf27ed7867b7 | 368 | |
irsanjul | 0:bf27ed7867b7 | 369 | failure: |
irsanjul | 0:bf27ed7867b7 | 370 | return ret; |
irsanjul | 0:bf27ed7867b7 | 371 | } |
irsanjul | 0:bf27ed7867b7 | 372 | |
irsanjul | 0:bf27ed7867b7 | 373 | int GSM::sendTCPData(char *data, int len) |
irsanjul | 0:bf27ed7867b7 | 374 | { |
irsanjul | 0:bf27ed7867b7 | 375 | int ret = 0; |
irsanjul | 0:bf27ed7867b7 | 376 | char cmd[64]; |
irsanjul | 0:bf27ed7867b7 | 377 | |
irsanjul | 0:bf27ed7867b7 | 378 | snprintf(cmd,sizeof(cmd),"AT+CIPSEND=0,%d\r\n",len); |
irsanjul | 0:bf27ed7867b7 | 379 | if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 380 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 381 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 382 | } |
irsanjul | 0:bf27ed7867b7 | 383 | |
irsanjul | 0:bf27ed7867b7 | 384 | if(0 != sendCmdAndWaitForResp(data,"OK",DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 385 | ret = -1; |
irsanjul | 0:bf27ed7867b7 | 386 | goto failure; |
irsanjul | 0:bf27ed7867b7 | 387 | } |
irsanjul | 0:bf27ed7867b7 | 388 | |
irsanjul | 0:bf27ed7867b7 | 389 | failure: |
irsanjul | 0:bf27ed7867b7 | 390 | return ret; |
irsanjul | 0:bf27ed7867b7 | 391 | } |
irsanjul | 0:bf27ed7867b7 | 392 | |
irsanjul | 0:bf27ed7867b7 | 393 | int GSM::receivedTCPData(char *buff, int len) |
irsanjul | 0:bf27ed7867b7 | 394 | { |
irsanjul | 0:bf27ed7867b7 | 395 | readBuffer(buff, len); |
irsanjul | 0:bf27ed7867b7 | 396 | |
irsanjul | 0:bf27ed7867b7 | 397 | if(strlen(buff) <= 0) return -1; |
irsanjul | 0:bf27ed7867b7 | 398 | |
irsanjul | 0:bf27ed7867b7 | 399 | return 0; |
irsanjul | 0:bf27ed7867b7 | 400 | } |
irsanjul | 0:bf27ed7867b7 | 401 | |
irsanjul | 0:bf27ed7867b7 | 402 | int GSM::closeTCP(void) |
irsanjul | 0:bf27ed7867b7 | 403 | { |
irsanjul | 0:bf27ed7867b7 | 404 | if(0 != sendCmdAndWaitForResp("AT+CIPCLOSE\r\n","OK",DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 405 | return -1; |
irsanjul | 0:bf27ed7867b7 | 406 | } |
irsanjul | 0:bf27ed7867b7 | 407 | return 0; |
irsanjul | 0:bf27ed7867b7 | 408 | } |
irsanjul | 0:bf27ed7867b7 | 409 | |
irsanjul | 0:bf27ed7867b7 | 410 | int GSM::disconnect(void) |
irsanjul | 0:bf27ed7867b7 | 411 | { |
irsanjul | 0:bf27ed7867b7 | 412 | if(0 != sendCmdAndWaitForResp("AT+NETCLOSE\r\n","OK",DEFAULT_TIMEOUT)) { |
irsanjul | 0:bf27ed7867b7 | 413 | return -1; |
irsanjul | 0:bf27ed7867b7 | 414 | } |
irsanjul | 0:bf27ed7867b7 | 415 | return 0; |
irsanjul | 0:bf27ed7867b7 | 416 | } |