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:
lawliet
Date:
Tue May 06 03:56:03 2014 +0000
Revision:
8:180feb3ebe62
Parent:
7:1bdcfd6da2d0
Child:
10:8c55dfcc9a7c
update gprs send function

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 }
lawliet 0:8ccbd963e74d 45
lawliet 0:8ccbd963e74d 46 bool GPRS::checkSIMStatus(void)
lawliet 0:8ccbd963e74d 47 {
lawliet 0:8ccbd963e74d 48 char gprsBuffer[32];
lawliet 0:8ccbd963e74d 49 int count = 0;
lawliet 0:8ccbd963e74d 50 cleanBuffer(gprsBuffer,32);
lawliet 0:8ccbd963e74d 51 while(count < 3) {
lawliet 0:8ccbd963e74d 52 sendCmd("AT+CPIN?\r\n");
lawliet 0:8ccbd963e74d 53 readBuffer(gprsBuffer,32,DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 54 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
lawliet 0:8ccbd963e74d 55 break;
lawliet 0:8ccbd963e74d 56 }
lawliet 0:8ccbd963e74d 57 count++;
lawliet 0:8ccbd963e74d 58 wait(1);
lawliet 0:8ccbd963e74d 59 }
lawliet 0:8ccbd963e74d 60 if(count == 3) {
lawliet 0:8ccbd963e74d 61 return false;
lawliet 0:8ccbd963e74d 62 }
lawliet 0:8ccbd963e74d 63 return true;
lawliet 0:8ccbd963e74d 64 }
lawliet 0:8ccbd963e74d 65
lawliet 0:8ccbd963e74d 66 bool GPRS::join()
lawliet 0:8ccbd963e74d 67 {
lawliet 0:8ccbd963e74d 68 char cmd[64];
lawliet 0:8ccbd963e74d 69 char ipAddr[32];
lawliet 0:8ccbd963e74d 70 //Select multiple connection
lawliet 0:8ccbd963e74d 71 sendCmdAndWaitForResp("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
lawliet 0:8ccbd963e74d 72
lawliet 0:8ccbd963e74d 73 //set APN
lawliet 0:8ccbd963e74d 74 snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
lawliet 0:8ccbd963e74d 75 sendCmdAndWaitForResp(cmd, "OK", DEFAULT_TIMEOUT,CMD);
lawliet 0:8ccbd963e74d 76
lawliet 0:8ccbd963e74d 77 //Brings up wireless connection
lawliet 0:8ccbd963e74d 78 sendCmdAndWaitForResp("AT+CIICR\r\n","OK",DEFAULT_TIMEOUT,CMD);
lawliet 0:8ccbd963e74d 79
lawliet 0:8ccbd963e74d 80 //Get local IP address
lawliet 0:8ccbd963e74d 81 sendCmd("AT+CIFSR\r\n");
lawliet 0:8ccbd963e74d 82 readBuffer(ipAddr,32,2);
lawliet 0:8ccbd963e74d 83
lawliet 0:8ccbd963e74d 84 if(NULL != strstr(ipAddr,"AT+CIFSR")) {
lawliet 0:8ccbd963e74d 85 _ip = str_to_ip(ipAddr+12);
lawliet 0:8ccbd963e74d 86 if(_ip != 0) {
lawliet 0:8ccbd963e74d 87 return true;
lawliet 0:8ccbd963e74d 88 }
lawliet 0:8ccbd963e74d 89 }
lawliet 0:8ccbd963e74d 90 return false;
lawliet 0:8ccbd963e74d 91 }
lawliet 0:8ccbd963e74d 92
lawliet 0:8ccbd963e74d 93 bool GPRS::setProtocol(int socket, Protocol p)
lawliet 0:8ccbd963e74d 94 {
lawliet 0:8ccbd963e74d 95 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 96 return false;
lawliet 0:8ccbd963e74d 97 }
lawliet 0:8ccbd963e74d 98 //ToDo: setProtocol
lawliet 0:8ccbd963e74d 99 return true;
lawliet 0:8ccbd963e74d 100 }
lawliet 0:8ccbd963e74d 101
lawliet 0:8ccbd963e74d 102 bool GPRS::connect(int socket, Protocol ptl,const char * host, int port, int timeout)
lawliet 0:8ccbd963e74d 103 {
lawliet 0:8ccbd963e74d 104 char cmd[64];
lawliet 0:8ccbd963e74d 105 char resp[96];
lawliet 0:8ccbd963e74d 106 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 107 return false;
lawliet 0:8ccbd963e74d 108 }
lawliet 0:8ccbd963e74d 109 if(ptl == TCP) {
lawliet 0:8ccbd963e74d 110 sprintf(cmd, "AT+CIPSTART=%d,\"TCP\",\"%s\",%d\r\n",socket, host, port);
lawliet 0:8ccbd963e74d 111 } else if(ptl == UDP) {
lawliet 0:8ccbd963e74d 112 sprintf(cmd, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d\r\n",socket, host, port);
lawliet 0:8ccbd963e74d 113 } else {
lawliet 0:8ccbd963e74d 114 return false;
lawliet 0:8ccbd963e74d 115 }
lawliet 0:8ccbd963e74d 116 sendCmd(cmd);
lawliet 0:8ccbd963e74d 117 readBuffer(resp,96,2*DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 118 if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
lawliet 0:8ccbd963e74d 119 return true;
lawliet 0:8ccbd963e74d 120 }
lawliet 0:8ccbd963e74d 121 return false;//ERROR
lawliet 0:8ccbd963e74d 122 }
lawliet 0:8ccbd963e74d 123
lawliet 0:8ccbd963e74d 124 bool GPRS::gethostbyname(const char* host, uint32_t* ip)
lawliet 0:8ccbd963e74d 125 {
lawliet 0:8ccbd963e74d 126 uint32_t addr = str_to_ip(host);
lawliet 0:8ccbd963e74d 127 char buf[17];
lawliet 0:8ccbd963e74d 128 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
lawliet 0:8ccbd963e74d 129 if (strcmp(buf, host) == 0) {
lawliet 0:8ccbd963e74d 130 *ip = addr;
lawliet 0:8ccbd963e74d 131 return true;
lawliet 0:8ccbd963e74d 132 }
lawliet 0:8ccbd963e74d 133 return false;
lawliet 0:8ccbd963e74d 134 }
lawliet 0:8ccbd963e74d 135
lawliet 0:8ccbd963e74d 136 bool GPRS::disconnect()
lawliet 0:8ccbd963e74d 137 {
lawliet 0:8ccbd963e74d 138 sendCmd("AT+CIPSHUT\r\n");
lawliet 0:8ccbd963e74d 139 return true;
lawliet 0:8ccbd963e74d 140 }
lawliet 0:8ccbd963e74d 141
lawliet 0:8ccbd963e74d 142 bool GPRS::is_connected(int socket)
lawliet 0:8ccbd963e74d 143 {
lawliet 0:8ccbd963e74d 144 char cmd[16];
lawliet 3:acf2ea413e72 145 char resp[96];
lawliet 3:acf2ea413e72 146 snprintf(cmd,16,"AT+CIPSTATUS=%d\r\n",socket);
lawliet 0:8ccbd963e74d 147 sendCmd(cmd);
lawliet 0:8ccbd963e74d 148 readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
lawliet 0:8ccbd963e74d 149 if(NULL != strstr(resp,"CONNECTED")) {
lawliet 0:8ccbd963e74d 150 //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CONNECTED"
lawliet 0:8ccbd963e74d 151 return true;
lawliet 0:8ccbd963e74d 152 } else {
lawliet 0:8ccbd963e74d 153 //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CLOSED"
lawliet 0:8ccbd963e74d 154 //+CIPSTATUS: 0,,"","","","INITIAL"
lawliet 0:8ccbd963e74d 155 return false;
lawliet 0:8ccbd963e74d 156 }
lawliet 0:8ccbd963e74d 157 }
lawliet 0:8ccbd963e74d 158
lawliet 0:8ccbd963e74d 159 void GPRS::reset()
lawliet 0:8ccbd963e74d 160 {
lawliet 0:8ccbd963e74d 161
lawliet 0:8ccbd963e74d 162 }
lawliet 0:8ccbd963e74d 163
lawliet 0:8ccbd963e74d 164 bool GPRS::close(int socket)
lawliet 0:8ccbd963e74d 165 {
lawliet 0:8ccbd963e74d 166 char cmd[16];
lawliet 0:8ccbd963e74d 167 char resp[16];
lawliet 0:8ccbd963e74d 168
lawliet 0:8ccbd963e74d 169 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 170 return false;
lawliet 0:8ccbd963e74d 171 }
lawliet 0:8ccbd963e74d 172 // if not connected, return
lawliet 0:8ccbd963e74d 173 if (is_connected(socket) == false) {
lawliet 0:8ccbd963e74d 174 return true;
lawliet 0:8ccbd963e74d 175 }
lawliet 4:1142bdd07989 176 snprintf(cmd, sizeof(cmd),"AT+CIPCLOSE=%d\r\n",socket);
lawliet 0:8ccbd963e74d 177 snprintf(resp,sizeof(resp),"%d, CLOSE OK",socket);
lawliet 0:8ccbd963e74d 178 if(0 != sendCmdAndWaitForResp(cmd, resp, DEFAULT_TIMEOUT,CMD)) {
lawliet 0:8ccbd963e74d 179 return false;
lawliet 0:8ccbd963e74d 180 }
lawliet 0:8ccbd963e74d 181 return true;
lawliet 0:8ccbd963e74d 182 }
lawliet 0:8ccbd963e74d 183
lawliet 0:8ccbd963e74d 184 bool GPRS::readable(void)
lawliet 0:8ccbd963e74d 185 {
lawliet 0:8ccbd963e74d 186 return readable();
lawliet 0:8ccbd963e74d 187 }
lawliet 0:8ccbd963e74d 188
lawliet 0:8ccbd963e74d 189 int GPRS::wait_readable(int socket, int wait_time)
lawliet 0:8ccbd963e74d 190 {
lawliet 0:8ccbd963e74d 191 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 192 return -1;
lawliet 0:8ccbd963e74d 193 }
lawliet 0:8ccbd963e74d 194 char resp[16];
lawliet 0:8ccbd963e74d 195 snprintf(resp,sizeof(resp),"\r\n\r\n+RECEIVE,%d",socket);//"+RECEIVE:<socketID>,<length>"
lawliet 0:8ccbd963e74d 196 int len = strlen(resp);
lawliet 0:8ccbd963e74d 197
lawliet 0:8ccbd963e74d 198 if(false == respCmp(resp,len,wait_time)) {
lawliet 0:8ccbd963e74d 199 return -1;
lawliet 0:8ccbd963e74d 200 }
lawliet 0:8ccbd963e74d 201 char c = readByte();//','
lawliet 0:8ccbd963e74d 202 char dataLen[4];
lawliet 0:8ccbd963e74d 203 int i = 0;
lawliet 0:8ccbd963e74d 204 c = readByte();
lawliet 0:8ccbd963e74d 205 while((c >= '0') && (c <= '9')) {
lawliet 0:8ccbd963e74d 206 dataLen[i++] = c;
lawliet 0:8ccbd963e74d 207 c = readByte();
lawliet 0:8ccbd963e74d 208 }
lawliet 0:8ccbd963e74d 209 c = readByte();//'\n'
lawliet 0:8ccbd963e74d 210 len = atoi(dataLen);
lawliet 0:8ccbd963e74d 211 return len;
lawliet 0:8ccbd963e74d 212 }
lawliet 0:8ccbd963e74d 213
lawliet 0:8ccbd963e74d 214 int GPRS::wait_writeable(int socket, int req_size)
lawliet 0:8ccbd963e74d 215 {
lawliet 0:8ccbd963e74d 216 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 217 return -1;
lawliet 0:8ccbd963e74d 218 }
lawliet 0:8ccbd963e74d 219 return req_size>256?256:req_size+1;
lawliet 0:8ccbd963e74d 220 }
lawliet 0:8ccbd963e74d 221
lawliet 0:8ccbd963e74d 222 int GPRS::send(int socket, const char * str, int len)
lawliet 0:8ccbd963e74d 223 {
lawliet 0:8ccbd963e74d 224 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 225 return -1;
lawliet 0:8ccbd963e74d 226 }
lawliet 0:8ccbd963e74d 227 char cmd[32];
lawliet 0:8ccbd963e74d 228 wait(1);
lawliet 3:acf2ea413e72 229 if(len > 0){
lawliet 8:180feb3ebe62 230 snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",socket);
lawliet 8:180feb3ebe62 231 if(0 != sendCmdAndWaitForResp(cmd,">",2,CMD)) {
lawliet 3:acf2ea413e72 232 return false;
lawliet 3:acf2ea413e72 233 }
lawliet 8:180feb3ebe62 234 sendCmd(str);
lawliet 8:180feb3ebe62 235 serialModem.putc((char)0x1a);
lawliet 0:8ccbd963e74d 236 }
lawliet 0:8ccbd963e74d 237 return len;
lawliet 0:8ccbd963e74d 238 }
lawliet 0:8ccbd963e74d 239
lawliet 0:8ccbd963e74d 240 int GPRS::recv(int socket, char* buf, int len)
lawliet 0:8ccbd963e74d 241 {
lawliet 0:8ccbd963e74d 242 if (socket < 0 || socket > MAX_SOCK_NUM-1) {
lawliet 0:8ccbd963e74d 243 return -1;
lawliet 0:8ccbd963e74d 244 }
lawliet 0:8ccbd963e74d 245 cleanBuffer(buf,len);
lawliet 0:8ccbd963e74d 246 readBuffer(buf,len,DEFAULT_TIMEOUT/2);
lawliet 0:8ccbd963e74d 247 return len;
lawliet 0:8ccbd963e74d 248 //return strlen(buf);
lawliet 0:8ccbd963e74d 249 }
lawliet 0:8ccbd963e74d 250
lawliet 0:8ccbd963e74d 251 int GPRS::new_socket()
lawliet 0:8ccbd963e74d 252 {
lawliet 7:1bdcfd6da2d0 253 socketID = 0; //we only support one socket.
lawliet 7:1bdcfd6da2d0 254 return socketID;
lawliet 0:8ccbd963e74d 255 }
lawliet 0:8ccbd963e74d 256
lawliet 0:8ccbd963e74d 257 uint16_t GPRS::new_port()
lawliet 0:8ccbd963e74d 258 {
lawliet 0:8ccbd963e74d 259 uint16_t port = rand();
lawliet 0:8ccbd963e74d 260 port |= 49152;
lawliet 0:8ccbd963e74d 261 return port;
lawliet 0:8ccbd963e74d 262 }
lawliet 0:8ccbd963e74d 263
lawliet 0:8ccbd963e74d 264 uint32_t GPRS::str_to_ip(const char* str)
lawliet 0:8ccbd963e74d 265 {
lawliet 0:8ccbd963e74d 266 uint32_t ip = 0;
lawliet 0:8ccbd963e74d 267 char* p = (char*)str;
lawliet 0:8ccbd963e74d 268 for(int i = 0; i < 4; i++) {
lawliet 0:8ccbd963e74d 269 ip |= atoi(p);
lawliet 0:8ccbd963e74d 270 p = strchr(p, '.');
lawliet 0:8ccbd963e74d 271 if (p == NULL) {
lawliet 0:8ccbd963e74d 272 break;
lawliet 0:8ccbd963e74d 273 }
lawliet 0:8ccbd963e74d 274 ip <<= 8;
lawliet 0:8ccbd963e74d 275 p++;
lawliet 0:8ccbd963e74d 276 }
lawliet 0:8ccbd963e74d 277 return ip;
lawliet 0:8ccbd963e74d 278 }