![](/media/cache/group/SAM_2404.JPG.50x50_q85.jpg)
works through pushing box to log data to google spreadsheet
Dependencies: MBed_Adafruit-GPS-Library SDFileSystem mbed
Fork of GPR_Interface by
GPRS/GPRS.cpp@11:045cb766d9a5, 2015-10-21 (annotated)
- Committer:
- DeWayneDennis
- Date:
- Wed Oct 21 19:41:42 2015 +0000
- Revision:
- 11:045cb766d9a5
- Parent:
- 10:8c55dfcc9a7c
Changed GPR Interface to work with pushingbox
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:8ccbd963e74d | 1 | /* |
lawliet | 0:8ccbd963e74d | 2 | GPRS.cpp |
lawliet | 0:8ccbd963e74d | 3 | 2014 Copyright (c) Seeed Technology Inc. All right reserved. |
lawliet | 0:8ccbd963e74d | 4 | |
lawliet | 0:8ccbd963e74d | 5 | Author:lawliet zou(lawliet.zou@gmail.com) |
lawliet | 0:8ccbd963e74d | 6 | 2014-2-24 |
lawliet | 0:8ccbd963e74d | 7 | |
lawliet | 0:8ccbd963e74d | 8 | This library is free software; you can redistribute it and/or |
lawliet | 0:8ccbd963e74d | 9 | modify it under the terms of the GNU Lesser General Public |
lawliet | 0:8ccbd963e74d | 10 | License as published by the Free Software Foundation; either |
lawliet | 0:8ccbd963e74d | 11 | version 2.1 of the License, or (at your option) any later version. |
lawliet | 0:8ccbd963e74d | 12 | |
lawliet | 0:8ccbd963e74d | 13 | This library is distributed in the hope that it will be useful, |
lawliet | 0:8ccbd963e74d | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
lawliet | 0:8ccbd963e74d | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
lawliet | 0:8ccbd963e74d | 16 | Lesser General Public License for more details. |
lawliet | 0:8ccbd963e74d | 17 | |
lawliet | 0:8ccbd963e74d | 18 | You should have received a copy of the GNU Lesser General Public |
lawliet | 0:8ccbd963e74d | 19 | License along with this library; if not, write to the Free Software |
lawliet | 0:8ccbd963e74d | 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
lawliet | 0:8ccbd963e74d | 21 | */ |
lawliet | 0:8ccbd963e74d | 22 | |
lawliet | 0:8ccbd963e74d | 23 | #include "mbed.h" |
lawliet | 0:8ccbd963e74d | 24 | #include "GPRS.h" |
lawliet | 0:8ccbd963e74d | 25 | |
lawliet | 0:8ccbd963e74d | 26 | GPRS* GPRS::inst; |
lawliet | 0:8ccbd963e74d | 27 | |
lawliet | 0:8ccbd963e74d | 28 | GPRS::GPRS(PinName tx, PinName rx, int baudRate, const char* apn, const char* userName, const char* passWord) : Modem(tx,rx,baudRate) |
lawliet | 0:8ccbd963e74d | 29 | { |
lawliet | 0:8ccbd963e74d | 30 | inst = this; |
lawliet | 0:8ccbd963e74d | 31 | _apn = apn; |
lawliet | 0:8ccbd963e74d | 32 | _userName = userName; |
lawliet | 0:8ccbd963e74d | 33 | _passWord = passWord; |
lawliet | 0:8ccbd963e74d | 34 | socketID = -1; |
DeWayneDennis | 11:045cb766d9a5 | 35 | _phoneNumber = "12345678910"; |
lawliet | 0:8ccbd963e74d | 36 | } |
lawliet | 0:8ccbd963e74d | 37 | |
lawliet | 0:8ccbd963e74d | 38 | bool GPRS::preInit() |
lawliet | 0:8ccbd963e74d | 39 | { |
lawliet | 0:8ccbd963e74d | 40 | for(int i = 0; i < 2; i++) { |
lawliet | 0:8ccbd963e74d | 41 | sendCmd("AT\r\n"); |
lawliet | 0:8ccbd963e74d | 42 | wait(1); |
lawliet | 0:8ccbd963e74d | 43 | } |
lawliet | 0:8ccbd963e74d | 44 | return checkSIMStatus(); |
lawliet | 0:8ccbd963e74d | 45 | } |
noahmilam | 10:8c55dfcc9a7c | 46 | // added by Noah Milam |
noahmilam | 10:8c55dfcc9a7c | 47 | void GPRS::start_server() |
noahmilam | 10:8c55dfcc9a7c | 48 | { |
noahmilam | 10:8c55dfcc9a7c | 49 | sendCmdResp("AT+CGATT?\r\n"); |
noahmilam | 10:8c55dfcc9a7c | 50 | |
noahmilam | 10:8c55dfcc9a7c | 51 | sendCmdResp("AT+CIPSERVER=1,8080\r\n"); |
noahmilam | 10:8c55dfcc9a7c | 52 | listen_server(); |
noahmilam | 10:8c55dfcc9a7c | 53 | } |
noahmilam | 10:8c55dfcc9a7c | 54 | void GPRS::listen_server() |
noahmilam | 10:8c55dfcc9a7c | 55 | { |
noahmilam | 10:8c55dfcc9a7c | 56 | gprs_response(); |
noahmilam | 10:8c55dfcc9a7c | 57 | } |
noahmilam | 10:8c55dfcc9a7c | 58 | |
noahmilam | 10:8c55dfcc9a7c | 59 | void GPRS::send_SMS(char* IPAdress) |
noahmilam | 10:8c55dfcc9a7c | 60 | { |
noahmilam | 10:8c55dfcc9a7c | 61 | printf("sending at command\n"); |
noahmilam | 10:8c55dfcc9a7c | 62 | sendCmdAndWaitForResp("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD); |
noahmilam | 10:8c55dfcc9a7c | 63 | wait(2); |
noahmilam | 10:8c55dfcc9a7c | 64 | //printf("\032\n"); |
DeWayneDennis | 11:045cb766d9a5 | 65 | sendCmdAndWaitForResp("AT+CMGS=\"+17066311506\"\r\n",">",DEFAULT_TIMEOUT,CMD); |
noahmilam | 10:8c55dfcc9a7c | 66 | printf("sent at cmgf\n"); |
noahmilam | 10:8c55dfcc9a7c | 67 | wait(2); |
noahmilam | 10:8c55dfcc9a7c | 68 | //printf("032"); |
noahmilam | 10:8c55dfcc9a7c | 69 | sendCmd(IPAdress); // sends the address |
noahmilam | 10:8c55dfcc9a7c | 70 | sendCmd("\x1a"); // this is like pressing control - z to end the send command |
DeWayneDennis | 11:045cb766d9a5 | 71 | wait(10); // giving the send enough time to do its thing |
noahmilam | 10:8c55dfcc9a7c | 72 | printf("should have been received"); |
noahmilam | 10:8c55dfcc9a7c | 73 | } |
lawliet | 0:8ccbd963e74d | 74 | |
noahmilam | 10:8c55dfcc9a7c | 75 | char* GPRS::read_SMS() |
noahmilam | 10:8c55dfcc9a7c | 76 | { |
noahmilam | 10:8c55dfcc9a7c | 77 | wait(1); |
noahmilam | 10:8c55dfcc9a7c | 78 | sendCmd("AT+CMGF=1\r\n"); // sms mode |
noahmilam | 10:8c55dfcc9a7c | 79 | wait(2); |
noahmilam | 10:8c55dfcc9a7c | 80 | sendCmd("AT+CMGD=1\r\n"); // delete the frist message so incoming message is spot 1 |
noahmilam | 10:8c55dfcc9a7c | 81 | wait(1); |
noahmilam | 10:8c55dfcc9a7c | 82 | wait_for_sms(); |
noahmilam | 10:8c55dfcc9a7c | 83 | wait(10); |
noahmilam | 10:8c55dfcc9a7c | 84 | sendCmd("AT+CMGR=1\r\n"); |
noahmilam | 10:8c55dfcc9a7c | 85 | storeResp(); |
noahmilam | 10:8c55dfcc9a7c | 86 | return get_server_IP(); |
noahmilam | 10:8c55dfcc9a7c | 87 | } |
noahmilam | 10:8c55dfcc9a7c | 88 | |
noahmilam | 10:8c55dfcc9a7c | 89 | // end of what Noah Milam added |
lawliet | 0:8ccbd963e74d | 90 | bool GPRS::checkSIMStatus(void) |
lawliet | 0:8ccbd963e74d | 91 | { |
DeWayneDennis | 11:045cb766d9a5 | 92 | printf("Checking SIM Status...\r\n"); |
lawliet | 0:8ccbd963e74d | 93 | char gprsBuffer[32]; |
lawliet | 0:8ccbd963e74d | 94 | int count = 0; |
DeWayneDennis | 11:045cb766d9a5 | 95 | |
lawliet | 0:8ccbd963e74d | 96 | while(count < 3) { |
DeWayneDennis | 11:045cb766d9a5 | 97 | cleanBuffer(gprsBuffer,32); |
lawliet | 0:8ccbd963e74d | 98 | sendCmd("AT+CPIN?\r\n"); |
lawliet | 0:8ccbd963e74d | 99 | readBuffer(gprsBuffer,32,DEFAULT_TIMEOUT); |
lawliet | 0:8ccbd963e74d | 100 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { |
lawliet | 0:8ccbd963e74d | 101 | break; |
lawliet | 0:8ccbd963e74d | 102 | } |
DeWayneDennis | 11:045cb766d9a5 | 103 | printf("SIM Not Ready..Try Again\r\n"); |
lawliet | 0:8ccbd963e74d | 104 | count++; |
lawliet | 0:8ccbd963e74d | 105 | wait(1); |
lawliet | 0:8ccbd963e74d | 106 | } |
lawliet | 0:8ccbd963e74d | 107 | if(count == 3) { |
lawliet | 0:8ccbd963e74d | 108 | return false; |
lawliet | 0:8ccbd963e74d | 109 | } |
DeWayneDennis | 11:045cb766d9a5 | 110 | printf("SIM Status GOOD!\r\n"); |
lawliet | 0:8ccbd963e74d | 111 | return true; |
lawliet | 0:8ccbd963e74d | 112 | } |
lawliet | 0:8ccbd963e74d | 113 | |
lawliet | 0:8ccbd963e74d | 114 | bool GPRS::join() |
lawliet | 0:8ccbd963e74d | 115 | { |
lawliet | 0:8ccbd963e74d | 116 | char cmd[64]; |
lawliet | 0:8ccbd963e74d | 117 | char ipAddr[32]; |
DeWayneDennis | 11:045cb766d9a5 | 118 | char resp[96]; |
DeWayneDennis | 11:045cb766d9a5 | 119 | //get the phone number |
DeWayneDennis | 11:045cb766d9a5 | 120 | printf(">>>>AT+CNUM\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 121 | sendCmd("AT+CNUM\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 122 | cleanBuffer(resp,96); |
DeWayneDennis | 11:045cb766d9a5 | 123 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 124 | if(NULL != strstr(resp,"+CNUM:")) { |
DeWayneDennis | 11:045cb766d9a5 | 125 | //if(storePhoneNumber(resp)){ |
DeWayneDennis | 11:045cb766d9a5 | 126 | printf("Successfull stored the phone number!\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 127 | //} |
DeWayneDennis | 11:045cb766d9a5 | 128 | } |
DeWayneDennis | 11:045cb766d9a5 | 129 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 130 | return false; |
DeWayneDennis | 11:045cb766d9a5 | 131 | } |
DeWayneDennis | 11:045cb766d9a5 | 132 | |
DeWayneDennis | 11:045cb766d9a5 | 133 | printf(">>>>AT+CREG\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 134 | sendCmd("AT+CREG?\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 135 | cleanBuffer(resp,96); |
DeWayneDennis | 11:045cb766d9a5 | 136 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 137 | if(NULL != strstr(resp,"+CREG:")) { |
DeWayneDennis | 11:045cb766d9a5 | 138 | } |
DeWayneDennis | 11:045cb766d9a5 | 139 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 140 | return false; |
DeWayneDennis | 11:045cb766d9a5 | 141 | } |
DeWayneDennis | 11:045cb766d9a5 | 142 | //close any existing connections |
DeWayneDennis | 11:045cb766d9a5 | 143 | printf(">>>>AT+CIPSHUT\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 144 | sendCmd("AT+CIPSHUT\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 145 | cleanBuffer(resp,96); |
DeWayneDennis | 11:045cb766d9a5 | 146 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 147 | if(NULL != strstr(resp,"OK")) { |
DeWayneDennis | 11:045cb766d9a5 | 148 | } |
DeWayneDennis | 11:045cb766d9a5 | 149 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 150 | return false; |
DeWayneDennis | 11:045cb766d9a5 | 151 | } |
lawliet | 0:8ccbd963e74d | 152 | //Select multiple connection |
DeWayneDennis | 11:045cb766d9a5 | 153 | printf(">>>>AT+CIPMUX=0\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 154 | sendCmd("AT+CIPMUX=0\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 155 | cleanBuffer(resp,96); |
DeWayneDennis | 11:045cb766d9a5 | 156 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 157 | if(NULL != strstr(resp,"CIPMUX")) { |
DeWayneDennis | 11:045cb766d9a5 | 158 | } |
DeWayneDennis | 11:045cb766d9a5 | 159 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 160 | return false; |
DeWayneDennis | 11:045cb766d9a5 | 161 | } |
DeWayneDennis | 11:045cb766d9a5 | 162 | //get signal strength |
DeWayneDennis | 11:045cb766d9a5 | 163 | printf(">>>>AT+CSQ\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 164 | sendCmd("AT+CSQ\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 165 | cleanBuffer(resp,96); |
DeWayneDennis | 11:045cb766d9a5 | 166 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 167 | if(NULL != strstr(resp,"+CSQ:")) { |
DeWayneDennis | 11:045cb766d9a5 | 168 | } |
DeWayneDennis | 11:045cb766d9a5 | 169 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 170 | return false; |
DeWayneDennis | 11:045cb766d9a5 | 171 | } |
DeWayneDennis | 11:045cb766d9a5 | 172 | |
DeWayneDennis | 11:045cb766d9a5 | 173 | //attach the device |
DeWayneDennis | 11:045cb766d9a5 | 174 | printf(">>>>AT+CGATT=1\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 175 | sendCmd("AT+CGATT=1\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 176 | cleanBuffer(resp,96); |
DeWayneDennis | 11:045cb766d9a5 | 177 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 178 | if(NULL != strstr(resp,"OK")) { |
DeWayneDennis | 11:045cb766d9a5 | 179 | } |
DeWayneDennis | 11:045cb766d9a5 | 180 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 181 | return false; |
DeWayneDennis | 11:045cb766d9a5 | 182 | } |
DeWayneDennis | 11:045cb766d9a5 | 183 | |
lawliet | 0:8ccbd963e74d | 184 | //set APN |
lawliet | 0:8ccbd963e74d | 185 | snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord); |
DeWayneDennis | 11:045cb766d9a5 | 186 | printf(">>>>%s",cmd); |
DeWayneDennis | 11:045cb766d9a5 | 187 | //sendCmdAndWaitForRes p(cmd, "OK", DEFAULT_TIMEOUT,CMD); |
DeWayneDennis | 11:045cb766d9a5 | 188 | sendCmd(cmd); |
DeWayneDennis | 11:045cb766d9a5 | 189 | cleanBuffer(resp,96); |
DeWayneDennis | 11:045cb766d9a5 | 190 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 191 | if(NULL != strstr(resp,"OK")) { |
DeWayneDennis | 11:045cb766d9a5 | 192 | } |
DeWayneDennis | 11:045cb766d9a5 | 193 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 194 | return false; |
DeWayneDennis | 11:045cb766d9a5 | 195 | } |
lawliet | 0:8ccbd963e74d | 196 | //Brings up wireless connection |
DeWayneDennis | 11:045cb766d9a5 | 197 | printf(">>>>AT+CIICR\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 198 | sendCmd("AT+CIICR\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 199 | cleanBuffer(resp,96); |
DeWayneDennis | 11:045cb766d9a5 | 200 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 201 | if(NULL != strstr(resp,"OK")) { |
DeWayneDennis | 11:045cb766d9a5 | 202 | } |
DeWayneDennis | 11:045cb766d9a5 | 203 | else{ |
DeWayneDennis | 11:045cb766d9a5 | 204 | return false; |
DeWayneDennis | 11:045cb766d9a5 | 205 | } |
DeWayneDennis | 11:045cb766d9a5 | 206 | |
lawliet | 0:8ccbd963e74d | 207 | //Get local IP address |
DeWayneDennis | 11:045cb766d9a5 | 208 | printf(">>>>AT+CIFSR\r\n"); |
lawliet | 0:8ccbd963e74d | 209 | sendCmd("AT+CIFSR\r\n"); |
lawliet | 0:8ccbd963e74d | 210 | readBuffer(ipAddr,32,2); |
DeWayneDennis | 11:045cb766d9a5 | 211 | printf(">>>>AT+CIFSR returns: %s\r\n", ipAddr); |
lawliet | 0:8ccbd963e74d | 212 | if(NULL != strstr(ipAddr,"AT+CIFSR")) { |
lawliet | 0:8ccbd963e74d | 213 | _ip = str_to_ip(ipAddr+12); |
lawliet | 0:8ccbd963e74d | 214 | if(_ip != 0) { |
DeWayneDennis | 11:045cb766d9a5 | 215 | //set to quick sending mode |
DeWayneDennis | 11:045cb766d9a5 | 216 | printf(">>>>AT+CIPQSEND=1\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 217 | sendCmd("AT+CIPQSEND=1\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 218 | cleanBuffer(resp,96); |
DeWayneDennis | 11:045cb766d9a5 | 219 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 220 | if(NULL != strstr(resp,"OK")) { |
DeWayneDennis | 11:045cb766d9a5 | 221 | return true; |
DeWayneDennis | 11:045cb766d9a5 | 222 | } |
lawliet | 0:8ccbd963e74d | 223 | } |
lawliet | 0:8ccbd963e74d | 224 | } |
DeWayneDennis | 11:045cb766d9a5 | 225 | |
lawliet | 0:8ccbd963e74d | 226 | return false; |
lawliet | 0:8ccbd963e74d | 227 | } |
lawliet | 0:8ccbd963e74d | 228 | |
lawliet | 0:8ccbd963e74d | 229 | bool GPRS::setProtocol(int socket, Protocol p) |
lawliet | 0:8ccbd963e74d | 230 | { |
lawliet | 0:8ccbd963e74d | 231 | if (socket < 0 || socket > MAX_SOCK_NUM-1) { |
lawliet | 0:8ccbd963e74d | 232 | return false; |
lawliet | 0:8ccbd963e74d | 233 | } |
lawliet | 0:8ccbd963e74d | 234 | //ToDo: setProtocol |
lawliet | 0:8ccbd963e74d | 235 | return true; |
lawliet | 0:8ccbd963e74d | 236 | } |
lawliet | 0:8ccbd963e74d | 237 | |
lawliet | 0:8ccbd963e74d | 238 | bool GPRS::connect(int socket, Protocol ptl,const char * host, int port, int timeout) |
lawliet | 0:8ccbd963e74d | 239 | { |
lawliet | 0:8ccbd963e74d | 240 | char cmd[64]; |
lawliet | 0:8ccbd963e74d | 241 | char resp[96]; |
lawliet | 0:8ccbd963e74d | 242 | if (socket < 0 || socket > MAX_SOCK_NUM-1) { |
lawliet | 0:8ccbd963e74d | 243 | return false; |
lawliet | 0:8ccbd963e74d | 244 | } |
lawliet | 0:8ccbd963e74d | 245 | if(ptl == TCP) { |
DeWayneDennis | 11:045cb766d9a5 | 246 | sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n", host, port); |
lawliet | 0:8ccbd963e74d | 247 | } else if(ptl == UDP) { |
lawliet | 0:8ccbd963e74d | 248 | sprintf(cmd, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d\r\n",socket, host, port); |
lawliet | 0:8ccbd963e74d | 249 | } else { |
lawliet | 0:8ccbd963e74d | 250 | return false; |
lawliet | 0:8ccbd963e74d | 251 | } |
DeWayneDennis | 11:045cb766d9a5 | 252 | |
lawliet | 0:8ccbd963e74d | 253 | sendCmd(cmd); |
DeWayneDennis | 11:045cb766d9a5 | 254 | readBuffer(resp,96,DEFAULT_TIMEOUT); |
lawliet | 0:8ccbd963e74d | 255 | if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK |
lawliet | 0:8ccbd963e74d | 256 | return true; |
lawliet | 0:8ccbd963e74d | 257 | } |
lawliet | 0:8ccbd963e74d | 258 | return false;//ERROR |
lawliet | 0:8ccbd963e74d | 259 | } |
lawliet | 0:8ccbd963e74d | 260 | |
lawliet | 0:8ccbd963e74d | 261 | bool GPRS::gethostbyname(const char* host, uint32_t* ip) |
lawliet | 0:8ccbd963e74d | 262 | { |
lawliet | 0:8ccbd963e74d | 263 | uint32_t addr = str_to_ip(host); |
lawliet | 0:8ccbd963e74d | 264 | char buf[17]; |
lawliet | 0:8ccbd963e74d | 265 | snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); |
lawliet | 0:8ccbd963e74d | 266 | if (strcmp(buf, host) == 0) { |
lawliet | 0:8ccbd963e74d | 267 | *ip = addr; |
lawliet | 0:8ccbd963e74d | 268 | return true; |
lawliet | 0:8ccbd963e74d | 269 | } |
lawliet | 0:8ccbd963e74d | 270 | return false; |
lawliet | 0:8ccbd963e74d | 271 | } |
lawliet | 0:8ccbd963e74d | 272 | |
lawliet | 0:8ccbd963e74d | 273 | bool GPRS::disconnect() |
lawliet | 0:8ccbd963e74d | 274 | { |
lawliet | 0:8ccbd963e74d | 275 | sendCmd("AT+CIPSHUT\r\n"); |
lawliet | 0:8ccbd963e74d | 276 | return true; |
lawliet | 0:8ccbd963e74d | 277 | } |
lawliet | 0:8ccbd963e74d | 278 | |
lawliet | 0:8ccbd963e74d | 279 | bool GPRS::is_connected(int socket) |
lawliet | 0:8ccbd963e74d | 280 | { |
DeWayneDennis | 11:045cb766d9a5 | 281 | |
lawliet | 0:8ccbd963e74d | 282 | char cmd[16]; |
lawliet | 3:acf2ea413e72 | 283 | char resp[96]; |
DeWayneDennis | 11:045cb766d9a5 | 284 | while(1){ |
DeWayneDennis | 11:045cb766d9a5 | 285 | snprintf(cmd,16,"AT+CIPSTATUS\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 286 | sendCmd(cmd); |
DeWayneDennis | 11:045cb766d9a5 | 287 | readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT); |
DeWayneDennis | 11:045cb766d9a5 | 288 | if(NULL != strstr(resp,"CONNECTED")) { |
DeWayneDennis | 11:045cb766d9a5 | 289 | return true; |
DeWayneDennis | 11:045cb766d9a5 | 290 | } else { |
DeWayneDennis | 11:045cb766d9a5 | 291 | return false; |
DeWayneDennis | 11:045cb766d9a5 | 292 | } |
lawliet | 0:8ccbd963e74d | 293 | } |
lawliet | 0:8ccbd963e74d | 294 | } |
lawliet | 0:8ccbd963e74d | 295 | |
lawliet | 0:8ccbd963e74d | 296 | void GPRS::reset() |
lawliet | 0:8ccbd963e74d | 297 | { |
lawliet | 0:8ccbd963e74d | 298 | } |
lawliet | 0:8ccbd963e74d | 299 | |
lawliet | 0:8ccbd963e74d | 300 | bool GPRS::close(int socket) |
lawliet | 0:8ccbd963e74d | 301 | { |
lawliet | 0:8ccbd963e74d | 302 | char cmd[16]; |
lawliet | 0:8ccbd963e74d | 303 | char resp[16]; |
lawliet | 0:8ccbd963e74d | 304 | |
lawliet | 0:8ccbd963e74d | 305 | if (socket < 0 || socket > MAX_SOCK_NUM-1) { |
lawliet | 0:8ccbd963e74d | 306 | return false; |
lawliet | 0:8ccbd963e74d | 307 | } |
lawliet | 0:8ccbd963e74d | 308 | // if not connected, return |
lawliet | 0:8ccbd963e74d | 309 | if (is_connected(socket) == false) { |
lawliet | 0:8ccbd963e74d | 310 | return true; |
lawliet | 0:8ccbd963e74d | 311 | } |
lawliet | 4:1142bdd07989 | 312 | snprintf(cmd, sizeof(cmd),"AT+CIPCLOSE=%d\r\n",socket); |
lawliet | 0:8ccbd963e74d | 313 | snprintf(resp,sizeof(resp),"%d, CLOSE OK",socket); |
lawliet | 0:8ccbd963e74d | 314 | if(0 != sendCmdAndWaitForResp(cmd, resp, DEFAULT_TIMEOUT,CMD)) { |
lawliet | 0:8ccbd963e74d | 315 | return false; |
lawliet | 0:8ccbd963e74d | 316 | } |
lawliet | 0:8ccbd963e74d | 317 | return true; |
lawliet | 0:8ccbd963e74d | 318 | } |
lawliet | 0:8ccbd963e74d | 319 | |
lawliet | 0:8ccbd963e74d | 320 | bool GPRS::readable(void) |
lawliet | 0:8ccbd963e74d | 321 | { |
lawliet | 0:8ccbd963e74d | 322 | return readable(); |
lawliet | 0:8ccbd963e74d | 323 | } |
lawliet | 0:8ccbd963e74d | 324 | |
lawliet | 0:8ccbd963e74d | 325 | int GPRS::wait_readable(int socket, int wait_time) |
lawliet | 0:8ccbd963e74d | 326 | { |
lawliet | 0:8ccbd963e74d | 327 | if (socket < 0 || socket > MAX_SOCK_NUM-1) { |
lawliet | 0:8ccbd963e74d | 328 | return -1; |
lawliet | 0:8ccbd963e74d | 329 | } |
lawliet | 0:8ccbd963e74d | 330 | char resp[16]; |
lawliet | 0:8ccbd963e74d | 331 | snprintf(resp,sizeof(resp),"\r\n\r\n+RECEIVE,%d",socket);//"+RECEIVE:<socketID>,<length>" |
lawliet | 0:8ccbd963e74d | 332 | int len = strlen(resp); |
lawliet | 0:8ccbd963e74d | 333 | |
lawliet | 0:8ccbd963e74d | 334 | if(false == respCmp(resp,len,wait_time)) { |
lawliet | 0:8ccbd963e74d | 335 | return -1; |
lawliet | 0:8ccbd963e74d | 336 | } |
lawliet | 0:8ccbd963e74d | 337 | char c = readByte();//',' |
lawliet | 0:8ccbd963e74d | 338 | char dataLen[4]; |
lawliet | 0:8ccbd963e74d | 339 | int i = 0; |
lawliet | 0:8ccbd963e74d | 340 | c = readByte(); |
lawliet | 0:8ccbd963e74d | 341 | while((c >= '0') && (c <= '9')) { |
lawliet | 0:8ccbd963e74d | 342 | dataLen[i++] = c; |
lawliet | 0:8ccbd963e74d | 343 | c = readByte(); |
lawliet | 0:8ccbd963e74d | 344 | } |
lawliet | 0:8ccbd963e74d | 345 | c = readByte();//'\n' |
lawliet | 0:8ccbd963e74d | 346 | len = atoi(dataLen); |
lawliet | 0:8ccbd963e74d | 347 | return len; |
lawliet | 0:8ccbd963e74d | 348 | } |
lawliet | 0:8ccbd963e74d | 349 | |
lawliet | 0:8ccbd963e74d | 350 | int GPRS::wait_writeable(int socket, int req_size) |
lawliet | 0:8ccbd963e74d | 351 | { |
lawliet | 0:8ccbd963e74d | 352 | if (socket < 0 || socket > MAX_SOCK_NUM-1) { |
lawliet | 0:8ccbd963e74d | 353 | return -1; |
lawliet | 0:8ccbd963e74d | 354 | } |
lawliet | 0:8ccbd963e74d | 355 | return req_size>256?256:req_size+1; |
lawliet | 0:8ccbd963e74d | 356 | } |
lawliet | 0:8ccbd963e74d | 357 | |
lawliet | 0:8ccbd963e74d | 358 | int GPRS::send(int socket, const char * str, int len) |
lawliet | 0:8ccbd963e74d | 359 | { |
lawliet | 0:8ccbd963e74d | 360 | if (socket < 0 || socket > MAX_SOCK_NUM-1) { |
lawliet | 0:8ccbd963e74d | 361 | return -1; |
lawliet | 0:8ccbd963e74d | 362 | } |
lawliet | 0:8ccbd963e74d | 363 | char cmd[32]; |
lawliet | 0:8ccbd963e74d | 364 | wait(1); |
lawliet | 3:acf2ea413e72 | 365 | if(len > 0){ |
DeWayneDennis | 11:045cb766d9a5 | 366 | snprintf(cmd,sizeof(cmd),"AT+CIPSEND\r\n"); |
DeWayneDennis | 11:045cb766d9a5 | 367 | if(0 != sendCmdAndWaitForResp(cmd,">",1,CMD)) { |
DeWayneDennis | 11:045cb766d9a5 | 368 | |
lawliet | 3:acf2ea413e72 | 369 | return false; |
lawliet | 3:acf2ea413e72 | 370 | } |
lawliet | 8:180feb3ebe62 | 371 | sendCmd(str); |
DeWayneDennis | 11:045cb766d9a5 | 372 | printf("Sending: %s\r\n", str); |
lawliet | 8:180feb3ebe62 | 373 | serialModem.putc((char)0x1a); |
DeWayneDennis | 11:045cb766d9a5 | 374 | |
lawliet | 0:8ccbd963e74d | 375 | } |
lawliet | 0:8ccbd963e74d | 376 | return len; |
lawliet | 0:8ccbd963e74d | 377 | } |
lawliet | 0:8ccbd963e74d | 378 | |
lawliet | 0:8ccbd963e74d | 379 | int GPRS::recv(int socket, char* buf, int len) |
lawliet | 0:8ccbd963e74d | 380 | { |
lawliet | 0:8ccbd963e74d | 381 | if (socket < 0 || socket > MAX_SOCK_NUM-1) { |
lawliet | 0:8ccbd963e74d | 382 | return -1; |
lawliet | 0:8ccbd963e74d | 383 | } |
lawliet | 0:8ccbd963e74d | 384 | cleanBuffer(buf,len); |
lawliet | 0:8ccbd963e74d | 385 | readBuffer(buf,len,DEFAULT_TIMEOUT/2); |
lawliet | 0:8ccbd963e74d | 386 | return len; |
lawliet | 0:8ccbd963e74d | 387 | //return strlen(buf); |
lawliet | 0:8ccbd963e74d | 388 | } |
lawliet | 0:8ccbd963e74d | 389 | |
lawliet | 0:8ccbd963e74d | 390 | int GPRS::new_socket() |
lawliet | 0:8ccbd963e74d | 391 | { |
lawliet | 7:1bdcfd6da2d0 | 392 | socketID = 0; //we only support one socket. |
lawliet | 7:1bdcfd6da2d0 | 393 | return socketID; |
lawliet | 0:8ccbd963e74d | 394 | } |
lawliet | 0:8ccbd963e74d | 395 | |
lawliet | 0:8ccbd963e74d | 396 | uint16_t GPRS::new_port() |
lawliet | 0:8ccbd963e74d | 397 | { |
lawliet | 0:8ccbd963e74d | 398 | uint16_t port = rand(); |
lawliet | 0:8ccbd963e74d | 399 | port |= 49152; |
lawliet | 0:8ccbd963e74d | 400 | return port; |
lawliet | 0:8ccbd963e74d | 401 | } |
lawliet | 0:8ccbd963e74d | 402 | |
lawliet | 0:8ccbd963e74d | 403 | uint32_t GPRS::str_to_ip(const char* str) |
lawliet | 0:8ccbd963e74d | 404 | { |
lawliet | 0:8ccbd963e74d | 405 | uint32_t ip = 0; |
lawliet | 0:8ccbd963e74d | 406 | char* p = (char*)str; |
lawliet | 0:8ccbd963e74d | 407 | for(int i = 0; i < 4; i++) { |
lawliet | 0:8ccbd963e74d | 408 | ip |= atoi(p); |
lawliet | 0:8ccbd963e74d | 409 | p = strchr(p, '.'); |
lawliet | 0:8ccbd963e74d | 410 | if (p == NULL) { |
lawliet | 0:8ccbd963e74d | 411 | break; |
lawliet | 0:8ccbd963e74d | 412 | } |
lawliet | 0:8ccbd963e74d | 413 | ip <<= 8; |
lawliet | 0:8ccbd963e74d | 414 | p++; |
lawliet | 0:8ccbd963e74d | 415 | } |
lawliet | 0:8ccbd963e74d | 416 | return ip; |
lawliet | 0:8ccbd963e74d | 417 | } |
DeWayneDennis | 11:045cb766d9a5 | 418 | bool GPRS::storePhoneNumber(char* atCNumValue){ |
DeWayneDennis | 11:045cb766d9a5 | 419 | char* leftDelimiter = "+"; |
DeWayneDennis | 11:045cb766d9a5 | 420 | char* rightDelimiter = "\""; |
DeWayneDennis | 11:045cb766d9a5 | 421 | // find the left delimiter and use it as the beginning of the substring |
DeWayneDennis | 11:045cb766d9a5 | 422 | const char* beginning = strstr(atCNumValue, leftDelimiter); |
DeWayneDennis | 11:045cb766d9a5 | 423 | if(beginning == NULL) |
DeWayneDennis | 11:045cb766d9a5 | 424 | return 1; // left delimiter not found |
DeWayneDennis | 11:045cb766d9a5 | 425 | |
DeWayneDennis | 11:045cb766d9a5 | 426 | // find the right delimiter |
DeWayneDennis | 11:045cb766d9a5 | 427 | const char* end = strstr(atCNumValue, rightDelimiter); |
DeWayneDennis | 11:045cb766d9a5 | 428 | if(end == NULL) |
DeWayneDennis | 11:045cb766d9a5 | 429 | return 2; // right delimiter not found |
DeWayneDennis | 11:045cb766d9a5 | 430 | |
DeWayneDennis | 11:045cb766d9a5 | 431 | // offset the beginning by the length of the left delimiter, so beginning points _after_ the left delimiter |
DeWayneDennis | 11:045cb766d9a5 | 432 | beginning += strlen(leftDelimiter); |
DeWayneDennis | 11:045cb766d9a5 | 433 | |
DeWayneDennis | 11:045cb766d9a5 | 434 | // get the length of the substring |
DeWayneDennis | 11:045cb766d9a5 | 435 | ptrdiff_t segmentLength = end - beginning; |
DeWayneDennis | 11:045cb766d9a5 | 436 | |
DeWayneDennis | 11:045cb766d9a5 | 437 | // allocate memory and copy the substring there |
DeWayneDennis | 11:045cb766d9a5 | 438 | //*_phoneNumber = (char*)malloc(segmentLength + 1); |
DeWayneDennis | 11:045cb766d9a5 | 439 | strncpy(_phoneNumber, beginning, segmentLength); |
DeWayneDennis | 11:045cb766d9a5 | 440 | (_phoneNumber)[segmentLength] = 0; |
DeWayneDennis | 11:045cb766d9a5 | 441 | |
DeWayneDennis | 11:045cb766d9a5 | 442 | printf("Phone Number: %s\r\n", _phoneNumber); |
DeWayneDennis | 11:045cb766d9a5 | 443 | return 0; // success! |
DeWayneDennis | 11:045cb766d9a5 | 444 | } |