works through pushing box to log data to google spreadsheet

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed

Fork of GPR_Interface by DCS_TEAM

Committer:
noahmilam
Date:
Fri Sep 25 04:10:56 2015 +0000
Revision:
10:8c55dfcc9a7c
Parent:
8:180feb3ebe62
Child:
11:045cb766d9a5
ff

Who changed what in which revision?

UserRevisionLine numberNew 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;
lawliet 0:8ccbd963e74d 35 }
lawliet 0:8ccbd963e74d 36
lawliet 0:8ccbd963e74d 37 bool GPRS::preInit()
lawliet 0:8ccbd963e74d 38 {
lawliet 0:8ccbd963e74d 39 for(int i = 0; i < 2; i++) {
lawliet 0:8ccbd963e74d 40 sendCmd("AT\r\n");
lawliet 0:8ccbd963e74d 41 wait(1);
lawliet 0:8ccbd963e74d 42 }
lawliet 0:8ccbd963e74d 43 return checkSIMStatus();
lawliet 0:8ccbd963e74d 44 }
noahmilam 10:8c55dfcc9a7c 45 // added by Noah Milam
noahmilam 10:8c55dfcc9a7c 46 void GPRS::start_server()
noahmilam 10:8c55dfcc9a7c 47 {
noahmilam 10:8c55dfcc9a7c 48 sendCmdResp("AT+CGATT?\r\n");
noahmilam 10:8c55dfcc9a7c 49
noahmilam 10:8c55dfcc9a7c 50 sendCmdResp("AT+CIPSERVER=1,8080\r\n");
noahmilam 10:8c55dfcc9a7c 51 listen_server();
noahmilam 10:8c55dfcc9a7c 52 }
noahmilam 10:8c55dfcc9a7c 53 void GPRS::listen_server()
noahmilam 10:8c55dfcc9a7c 54 {
noahmilam 10:8c55dfcc9a7c 55 gprs_response();
noahmilam 10:8c55dfcc9a7c 56 }
noahmilam 10:8c55dfcc9a7c 57
noahmilam 10:8c55dfcc9a7c 58 void GPRS::send_SMS(char* IPAdress)
noahmilam 10:8c55dfcc9a7c 59 {
noahmilam 10:8c55dfcc9a7c 60 printf("sending at command\n");
noahmilam 10:8c55dfcc9a7c 61 sendCmdAndWaitForResp("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
noahmilam 10:8c55dfcc9a7c 62 wait(2);
noahmilam 10:8c55dfcc9a7c 63 //printf("\032\n");
noahmilam 10:8c55dfcc9a7c 64 sendCmdAndWaitForResp("AT+CMGS=\"+18183977481\"\r\n",">",DEFAULT_TIMEOUT,CMD);
noahmilam 10:8c55dfcc9a7c 65 printf("sent at cmgf\n");
noahmilam 10:8c55dfcc9a7c 66 wait(2);
noahmilam 10:8c55dfcc9a7c 67 //printf("032");
noahmilam 10:8c55dfcc9a7c 68 sendCmd(IPAdress); // sends the address
noahmilam 10:8c55dfcc9a7c 69 sendCmd("\x1a"); // this is like pressing control - z to end the send command
noahmilam 10:8c55dfcc9a7c 70 wait(10); // giving the send enough time to do its thing
noahmilam 10:8c55dfcc9a7c 71 printf("should have been received");
noahmilam 10:8c55dfcc9a7c 72 }
lawliet 0:8ccbd963e74d 73
noahmilam 10:8c55dfcc9a7c 74 char* GPRS::read_SMS()
noahmilam 10:8c55dfcc9a7c 75 {
noahmilam 10:8c55dfcc9a7c 76 wait(1);
noahmilam 10:8c55dfcc9a7c 77 sendCmd("AT+CMGF=1\r\n"); // sms mode
noahmilam 10:8c55dfcc9a7c 78 wait(2);
noahmilam 10:8c55dfcc9a7c 79 sendCmd("AT+CMGD=1\r\n"); // delete the frist message so incoming message is spot 1
noahmilam 10:8c55dfcc9a7c 80 wait(1);
noahmilam 10:8c55dfcc9a7c 81 wait_for_sms();
noahmilam 10:8c55dfcc9a7c 82 wait(10);
noahmilam 10:8c55dfcc9a7c 83 sendCmd("AT+CMGR=1\r\n");
noahmilam 10:8c55dfcc9a7c 84 storeResp();
noahmilam 10:8c55dfcc9a7c 85 char temp[30];
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 {
lawliet 0:8ccbd963e74d 92 char gprsBuffer[32];
lawliet 0:8ccbd963e74d 93 int count = 0;
lawliet 0:8ccbd963e74d 94 cleanBuffer(gprsBuffer,32);
lawliet 0:8ccbd963e74d 95 while(count < 3) {
lawliet 0:8ccbd963e74d 96 sendCmd("AT+CPIN?\r\n");
lawliet 0:8ccbd963e74d 97 readBuffer(gprsBuffer,32,DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 98 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
lawliet 0:8ccbd963e74d 99 break;
lawliet 0:8ccbd963e74d 100 }
lawliet 0:8ccbd963e74d 101 count++;
lawliet 0:8ccbd963e74d 102 wait(1);
lawliet 0:8ccbd963e74d 103 }
lawliet 0:8ccbd963e74d 104 if(count == 3) {
lawliet 0:8ccbd963e74d 105 return false;
lawliet 0:8ccbd963e74d 106 }
lawliet 0:8ccbd963e74d 107 return true;
lawliet 0:8ccbd963e74d 108 }
lawliet 0:8ccbd963e74d 109
lawliet 0:8ccbd963e74d 110 bool GPRS::join()
lawliet 0:8ccbd963e74d 111 {
lawliet 0:8ccbd963e74d 112 char cmd[64];
lawliet 0:8ccbd963e74d 113 char ipAddr[32];
lawliet 0:8ccbd963e74d 114 //Select multiple connection
lawliet 0:8ccbd963e74d 115 sendCmdAndWaitForResp("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
lawliet 0:8ccbd963e74d 116
lawliet 0:8ccbd963e74d 117 //set APN
lawliet 0:8ccbd963e74d 118 snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
lawliet 0:8ccbd963e74d 119 sendCmdAndWaitForResp(cmd, "OK", DEFAULT_TIMEOUT,CMD);
lawliet 0:8ccbd963e74d 120
lawliet 0:8ccbd963e74d 121 //Brings up wireless connection
lawliet 0:8ccbd963e74d 122 sendCmdAndWaitForResp("AT+CIICR\r\n","OK",DEFAULT_TIMEOUT,CMD);
lawliet 0:8ccbd963e74d 123
lawliet 0:8ccbd963e74d 124 //Get local IP address
lawliet 0:8ccbd963e74d 125 sendCmd("AT+CIFSR\r\n");
lawliet 0:8ccbd963e74d 126 readBuffer(ipAddr,32,2);
lawliet 0:8ccbd963e74d 127
lawliet 0:8ccbd963e74d 128 if(NULL != strstr(ipAddr,"AT+CIFSR")) {
lawliet 0:8ccbd963e74d 129 _ip = str_to_ip(ipAddr+12);
lawliet 0:8ccbd963e74d 130 if(_ip != 0) {
lawliet 0:8ccbd963e74d 131 return true;
lawliet 0:8ccbd963e74d 132 }
lawliet 0:8ccbd963e74d 133 }
lawliet 0:8ccbd963e74d 134 return false;
lawliet 0:8ccbd963e74d 135 }
lawliet 0:8ccbd963e74d 136
lawliet 0:8ccbd963e74d 137 bool GPRS::setProtocol(int socket, Protocol p)
lawliet 0:8ccbd963e74d 138 {
lawliet 0:8ccbd963e74d 139 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 140 return false;
lawliet 0:8ccbd963e74d 141 }
lawliet 0:8ccbd963e74d 142 //ToDo: setProtocol
lawliet 0:8ccbd963e74d 143 return true;
lawliet 0:8ccbd963e74d 144 }
lawliet 0:8ccbd963e74d 145
lawliet 0:8ccbd963e74d 146 bool GPRS::connect(int socket, Protocol ptl,const char * host, int port, int timeout)
lawliet 0:8ccbd963e74d 147 {
lawliet 0:8ccbd963e74d 148 char cmd[64];
lawliet 0:8ccbd963e74d 149 char resp[96];
lawliet 0:8ccbd963e74d 150 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 151 return false;
lawliet 0:8ccbd963e74d 152 }
lawliet 0:8ccbd963e74d 153 if(ptl == TCP) {
lawliet 0:8ccbd963e74d 154 sprintf(cmd, "AT+CIPSTART=%d,\"TCP\",\"%s\",%d\r\n",socket, host, port);
lawliet 0:8ccbd963e74d 155 } else if(ptl == UDP) {
lawliet 0:8ccbd963e74d 156 sprintf(cmd, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d\r\n",socket, host, port);
lawliet 0:8ccbd963e74d 157 } else {
lawliet 0:8ccbd963e74d 158 return false;
lawliet 0:8ccbd963e74d 159 }
lawliet 0:8ccbd963e74d 160 sendCmd(cmd);
lawliet 0:8ccbd963e74d 161 readBuffer(resp,96,2*DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 162 if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
lawliet 0:8ccbd963e74d 163 return true;
lawliet 0:8ccbd963e74d 164 }
lawliet 0:8ccbd963e74d 165 return false;//ERROR
lawliet 0:8ccbd963e74d 166 }
lawliet 0:8ccbd963e74d 167
lawliet 0:8ccbd963e74d 168 bool GPRS::gethostbyname(const char* host, uint32_t* ip)
lawliet 0:8ccbd963e74d 169 {
lawliet 0:8ccbd963e74d 170 uint32_t addr = str_to_ip(host);
lawliet 0:8ccbd963e74d 171 char buf[17];
lawliet 0:8ccbd963e74d 172 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
lawliet 0:8ccbd963e74d 173 if (strcmp(buf, host) == 0) {
lawliet 0:8ccbd963e74d 174 *ip = addr;
lawliet 0:8ccbd963e74d 175 return true;
lawliet 0:8ccbd963e74d 176 }
lawliet 0:8ccbd963e74d 177 return false;
lawliet 0:8ccbd963e74d 178 }
lawliet 0:8ccbd963e74d 179
lawliet 0:8ccbd963e74d 180 bool GPRS::disconnect()
lawliet 0:8ccbd963e74d 181 {
lawliet 0:8ccbd963e74d 182 sendCmd("AT+CIPSHUT\r\n");
lawliet 0:8ccbd963e74d 183 return true;
lawliet 0:8ccbd963e74d 184 }
lawliet 0:8ccbd963e74d 185
lawliet 0:8ccbd963e74d 186 bool GPRS::is_connected(int socket)
lawliet 0:8ccbd963e74d 187 {
lawliet 0:8ccbd963e74d 188 char cmd[16];
lawliet 3:acf2ea413e72 189 char resp[96];
lawliet 3:acf2ea413e72 190 snprintf(cmd,16,"AT+CIPSTATUS=%d\r\n",socket);
lawliet 0:8ccbd963e74d 191 sendCmd(cmd);
lawliet 0:8ccbd963e74d 192 readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 193 if(NULL != strstr(resp,"CONNECTED")) {
lawliet 0:8ccbd963e74d 194 //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CONNECTED"
lawliet 0:8ccbd963e74d 195 return true;
lawliet 0:8ccbd963e74d 196 } else {
lawliet 0:8ccbd963e74d 197 //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CLOSED"
lawliet 0:8ccbd963e74d 198 //+CIPSTATUS: 0,,"","","","INITIAL"
lawliet 0:8ccbd963e74d 199 return false;
lawliet 0:8ccbd963e74d 200 }
lawliet 0:8ccbd963e74d 201 }
lawliet 0:8ccbd963e74d 202
lawliet 0:8ccbd963e74d 203 void GPRS::reset()
lawliet 0:8ccbd963e74d 204 {
lawliet 0:8ccbd963e74d 205
lawliet 0:8ccbd963e74d 206 }
lawliet 0:8ccbd963e74d 207
lawliet 0:8ccbd963e74d 208 bool GPRS::close(int socket)
lawliet 0:8ccbd963e74d 209 {
lawliet 0:8ccbd963e74d 210 char cmd[16];
lawliet 0:8ccbd963e74d 211 char resp[16];
lawliet 0:8ccbd963e74d 212
lawliet 0:8ccbd963e74d 213 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 214 return false;
lawliet 0:8ccbd963e74d 215 }
lawliet 0:8ccbd963e74d 216 // if not connected, return
lawliet 0:8ccbd963e74d 217 if (is_connected(socket) == false) {
lawliet 0:8ccbd963e74d 218 return true;
lawliet 0:8ccbd963e74d 219 }
lawliet 4:1142bdd07989 220 snprintf(cmd, sizeof(cmd),"AT+CIPCLOSE=%d\r\n",socket);
lawliet 0:8ccbd963e74d 221 snprintf(resp,sizeof(resp),"%d, CLOSE OK",socket);
lawliet 0:8ccbd963e74d 222 if(0 != sendCmdAndWaitForResp(cmd, resp, DEFAULT_TIMEOUT,CMD)) {
lawliet 0:8ccbd963e74d 223 return false;
lawliet 0:8ccbd963e74d 224 }
lawliet 0:8ccbd963e74d 225 return true;
lawliet 0:8ccbd963e74d 226 }
lawliet 0:8ccbd963e74d 227
lawliet 0:8ccbd963e74d 228 bool GPRS::readable(void)
lawliet 0:8ccbd963e74d 229 {
lawliet 0:8ccbd963e74d 230 return readable();
lawliet 0:8ccbd963e74d 231 }
lawliet 0:8ccbd963e74d 232
lawliet 0:8ccbd963e74d 233 int GPRS::wait_readable(int socket, int wait_time)
lawliet 0:8ccbd963e74d 234 {
lawliet 0:8ccbd963e74d 235 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 236 return -1;
lawliet 0:8ccbd963e74d 237 }
lawliet 0:8ccbd963e74d 238 char resp[16];
lawliet 0:8ccbd963e74d 239 snprintf(resp,sizeof(resp),"\r\n\r\n+RECEIVE,%d",socket);//"+RECEIVE:<socketID>,<length>"
lawliet 0:8ccbd963e74d 240 int len = strlen(resp);
lawliet 0:8ccbd963e74d 241
lawliet 0:8ccbd963e74d 242 if(false == respCmp(resp,len,wait_time)) {
lawliet 0:8ccbd963e74d 243 return -1;
lawliet 0:8ccbd963e74d 244 }
lawliet 0:8ccbd963e74d 245 char c = readByte();//','
lawliet 0:8ccbd963e74d 246 char dataLen[4];
lawliet 0:8ccbd963e74d 247 int i = 0;
lawliet 0:8ccbd963e74d 248 c = readByte();
lawliet 0:8ccbd963e74d 249 while((c >= '0') && (c <= '9')) {
lawliet 0:8ccbd963e74d 250 dataLen[i++] = c;
lawliet 0:8ccbd963e74d 251 c = readByte();
lawliet 0:8ccbd963e74d 252 }
lawliet 0:8ccbd963e74d 253 c = readByte();//'\n'
lawliet 0:8ccbd963e74d 254 len = atoi(dataLen);
lawliet 0:8ccbd963e74d 255 return len;
lawliet 0:8ccbd963e74d 256 }
lawliet 0:8ccbd963e74d 257
lawliet 0:8ccbd963e74d 258 int GPRS::wait_writeable(int socket, int req_size)
lawliet 0:8ccbd963e74d 259 {
lawliet 0:8ccbd963e74d 260 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 261 return -1;
lawliet 0:8ccbd963e74d 262 }
lawliet 0:8ccbd963e74d 263 return req_size>256?256:req_size+1;
lawliet 0:8ccbd963e74d 264 }
lawliet 0:8ccbd963e74d 265
lawliet 0:8ccbd963e74d 266 int GPRS::send(int socket, const char * str, int len)
lawliet 0:8ccbd963e74d 267 {
lawliet 0:8ccbd963e74d 268 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 269 return -1;
lawliet 0:8ccbd963e74d 270 }
lawliet 0:8ccbd963e74d 271 char cmd[32];
lawliet 0:8ccbd963e74d 272 wait(1);
lawliet 3:acf2ea413e72 273 if(len > 0){
lawliet 8:180feb3ebe62 274 snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",socket);
lawliet 8:180feb3ebe62 275 if(0 != sendCmdAndWaitForResp(cmd,">",2,CMD)) {
lawliet 3:acf2ea413e72 276 return false;
lawliet 3:acf2ea413e72 277 }
lawliet 8:180feb3ebe62 278 sendCmd(str);
lawliet 8:180feb3ebe62 279 serialModem.putc((char)0x1a);
lawliet 0:8ccbd963e74d 280 }
lawliet 0:8ccbd963e74d 281 return len;
lawliet 0:8ccbd963e74d 282 }
lawliet 0:8ccbd963e74d 283
lawliet 0:8ccbd963e74d 284 int GPRS::recv(int socket, char* buf, int len)
lawliet 0:8ccbd963e74d 285 {
lawliet 0:8ccbd963e74d 286 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 287 return -1;
lawliet 0:8ccbd963e74d 288 }
lawliet 0:8ccbd963e74d 289 cleanBuffer(buf,len);
lawliet 0:8ccbd963e74d 290 readBuffer(buf,len,DEFAULT_TIMEOUT/2);
lawliet 0:8ccbd963e74d 291 return len;
lawliet 0:8ccbd963e74d 292 //return strlen(buf);
lawliet 0:8ccbd963e74d 293 }
lawliet 0:8ccbd963e74d 294
lawliet 0:8ccbd963e74d 295 int GPRS::new_socket()
lawliet 0:8ccbd963e74d 296 {
lawliet 7:1bdcfd6da2d0 297 socketID = 0; //we only support one socket.
lawliet 7:1bdcfd6da2d0 298 return socketID;
lawliet 0:8ccbd963e74d 299 }
lawliet 0:8ccbd963e74d 300
lawliet 0:8ccbd963e74d 301 uint16_t GPRS::new_port()
lawliet 0:8ccbd963e74d 302 {
lawliet 0:8ccbd963e74d 303 uint16_t port = rand();
lawliet 0:8ccbd963e74d 304 port |= 49152;
lawliet 0:8ccbd963e74d 305 return port;
lawliet 0:8ccbd963e74d 306 }
lawliet 0:8ccbd963e74d 307
lawliet 0:8ccbd963e74d 308 uint32_t GPRS::str_to_ip(const char* str)
lawliet 0:8ccbd963e74d 309 {
lawliet 0:8ccbd963e74d 310 uint32_t ip = 0;
lawliet 0:8ccbd963e74d 311 char* p = (char*)str;
lawliet 0:8ccbd963e74d 312 for(int i = 0; i < 4; i++) {
lawliet 0:8ccbd963e74d 313 ip |= atoi(p);
lawliet 0:8ccbd963e74d 314 p = strchr(p, '.');
lawliet 0:8ccbd963e74d 315 if (p == NULL) {
lawliet 0:8ccbd963e74d 316 break;
lawliet 0:8ccbd963e74d 317 }
lawliet 0:8ccbd963e74d 318 ip <<= 8;
lawliet 0:8ccbd963e74d 319 p++;
lawliet 0:8ccbd963e74d 320 }
lawliet 0:8ccbd963e74d 321 return ip;
lawliet 0:8ccbd963e74d 322 }