Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of GPRSInterface by
GPRS.cpp
00001 /* 00002 GPRS.cpp 00003 2014 Copyright (c) Seeed Technology Inc. All right reserved. 00004 00005 Author:lawliet zou(lawliet.zou@gmail.com) 00006 2014-2-24 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "mbed.h" 00024 #include "GPRS.h" 00025 00026 GPRS* GPRS::inst; 00027 00028 GPRS::GPRS(PinName tx, PinName rx, int baudRate, const char* apn, const char* userName, const char* passWord) : Modem(tx,rx,baudRate) 00029 { 00030 inst = this; 00031 _apn = apn; 00032 _userName = userName; 00033 _passWord = passWord; 00034 socketID = -1; 00035 } 00036 00037 bool GPRS::preInit() 00038 { 00039 for(int i = 0; i < 2; i++) { 00040 sendCmd("AT\r\n"); 00041 wait(1); 00042 } 00043 return checkSIMStatus(); 00044 } 00045 // added by Noah Milam 00046 void GPRS::start_server() 00047 { 00048 sendCmdResp("AT+CGATT?\r\n"); 00049 00050 sendCmdResp("AT+CIPSERVER=1,8080\r\n"); 00051 listen_server(); 00052 } 00053 void GPRS::listen_server() 00054 { 00055 gprs_response(); 00056 } 00057 00058 void GPRS::send_SMS(char* IPAdress) 00059 { 00060 printf("sending at command\n"); 00061 sendCmdAndWaitForResp("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD); 00062 wait(2); 00063 //printf("\032\n"); 00064 sendCmdAndWaitForResp("AT+CMGS=\"+18183977481\"\r\n",">",DEFAULT_TIMEOUT,CMD); 00065 printf("sent at cmgf\n"); 00066 wait(2); 00067 //printf("032"); 00068 sendCmd(IPAdress); // sends the address 00069 sendCmd("\x1a"); // this is like pressing control - z to end the send command 00070 wait(10); // giving the send enough time to do its thing 00071 printf("should have been received"); 00072 } 00073 00074 char* GPRS::read_SMS() 00075 { 00076 wait(1); 00077 sendCmd("AT+CMGF=1\r\n"); // sms mode 00078 wait(2); 00079 sendCmd("AT+CMGD=1\r\n"); // delete the frist message so incoming message is spot 1 00080 wait(1); 00081 wait_for_sms(); 00082 wait(10); 00083 sendCmd("AT+CMGR=1\r\n"); 00084 storeResp(); 00085 char temp[30]; 00086 return get_server_IP(); 00087 } 00088 00089 // end of what Noah Milam added 00090 bool GPRS::checkSIMStatus(void) 00091 { 00092 char gprsBuffer[32]; 00093 int count = 0; 00094 cleanBuffer(gprsBuffer,32); 00095 while(count < 3) { 00096 sendCmd("AT+CPIN?\r\n"); 00097 readBuffer(gprsBuffer,32,DEFAULT_TIMEOUT); 00098 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { 00099 break; 00100 } 00101 count++; 00102 wait(1); 00103 } 00104 if(count == 3) { 00105 return false; 00106 } 00107 return true; 00108 } 00109 00110 bool GPRS::join() 00111 { 00112 char cmd[64]; 00113 char ipAddr[32]; 00114 //Select multiple connection 00115 sendCmdAndWaitForResp("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD); 00116 00117 //set APN 00118 snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord); 00119 sendCmdAndWaitForResp(cmd, "OK", DEFAULT_TIMEOUT,CMD); 00120 00121 //Brings up wireless connection 00122 sendCmdAndWaitForResp("AT+CIICR\r\n","OK",DEFAULT_TIMEOUT,CMD); 00123 00124 //Get local IP address 00125 sendCmd("AT+CIFSR\r\n"); 00126 readBuffer(ipAddr,32,2); 00127 00128 if(NULL != strstr(ipAddr,"AT+CIFSR")) { 00129 _ip = str_to_ip(ipAddr+12); 00130 if(_ip != 0) { 00131 return true; 00132 } 00133 } 00134 return false; 00135 } 00136 00137 bool GPRS::setProtocol(int socket, Protocol p) 00138 { 00139 if (socket < 0 || socket > MAX_SOCK_NUM-1) { 00140 return false; 00141 } 00142 //ToDo: setProtocol 00143 return true; 00144 } 00145 00146 bool GPRS::connect(int socket, Protocol ptl,const char * host, int port, int timeout) 00147 { 00148 char cmd[64]; 00149 char resp[96]; 00150 if (socket < 0 || socket > MAX_SOCK_NUM-1) { 00151 return false; 00152 } 00153 if(ptl == TCP) { 00154 sprintf(cmd, "AT+CIPSTART=%d,\"TCP\",\"%s\",%d\r\n",socket, host, port); 00155 } else if(ptl == UDP) { 00156 sprintf(cmd, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d\r\n",socket, host, port); 00157 } else { 00158 return false; 00159 } 00160 sendCmd(cmd); 00161 readBuffer(resp,96,2*DEFAULT_TIMEOUT); 00162 if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK 00163 return true; 00164 } 00165 return false;//ERROR 00166 } 00167 00168 bool GPRS::gethostbyname(const char* host, uint32_t* ip) 00169 { 00170 uint32_t addr = str_to_ip(host); 00171 char buf[17]; 00172 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); 00173 if (strcmp(buf, host) == 0) { 00174 *ip = addr; 00175 return true; 00176 } 00177 return false; 00178 } 00179 00180 bool GPRS::disconnect() 00181 { 00182 sendCmd("AT+CIPSHUT\r\n"); 00183 return true; 00184 } 00185 00186 bool GPRS::is_connected(int socket) 00187 { 00188 char cmd[16]; 00189 char resp[96]; 00190 snprintf(cmd,16,"AT+CIPSTATUS=%d\r\n",socket); 00191 sendCmd(cmd); 00192 readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT); 00193 if(NULL != strstr(resp,"CONNECTED")) { 00194 //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CONNECTED" 00195 return true; 00196 } else { 00197 //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CLOSED" 00198 //+CIPSTATUS: 0,,"","","","INITIAL" 00199 return false; 00200 } 00201 } 00202 00203 void GPRS::reset() 00204 { 00205 00206 } 00207 00208 bool GPRS::close(int socket) 00209 { 00210 char cmd[16]; 00211 char resp[16]; 00212 00213 if (socket < 0 || socket > MAX_SOCK_NUM-1) { 00214 return false; 00215 } 00216 // if not connected, return 00217 if (is_connected(socket) == false) { 00218 return true; 00219 } 00220 snprintf(cmd, sizeof(cmd),"AT+CIPCLOSE=%d\r\n",socket); 00221 snprintf(resp,sizeof(resp),"%d, CLOSE OK",socket); 00222 if(0 != sendCmdAndWaitForResp(cmd, resp, DEFAULT_TIMEOUT,CMD)) { 00223 return false; 00224 } 00225 return true; 00226 } 00227 00228 bool GPRS::readable(void) 00229 { 00230 return readable(); 00231 } 00232 00233 int GPRS::wait_readable(int socket, int wait_time) 00234 { 00235 if (socket < 0 || socket > MAX_SOCK_NUM-1) { 00236 return -1; 00237 } 00238 char resp[16]; 00239 snprintf(resp,sizeof(resp),"\r\n\r\n+RECEIVE,%d",socket);//"+RECEIVE:<socketID>,<length>" 00240 int len = strlen(resp); 00241 00242 if(false == respCmp(resp,len,wait_time)) { 00243 return -1; 00244 } 00245 char c = readByte();//',' 00246 char dataLen[4]; 00247 int i = 0; 00248 c = readByte(); 00249 while((c >= '0') && (c <= '9')) { 00250 dataLen[i++] = c; 00251 c = readByte(); 00252 } 00253 c = readByte();//'\n' 00254 len = atoi(dataLen); 00255 return len; 00256 } 00257 00258 int GPRS::wait_writeable(int socket, int req_size) 00259 { 00260 if (socket < 0 || socket > MAX_SOCK_NUM-1) { 00261 return -1; 00262 } 00263 return req_size>256?256:req_size+1; 00264 } 00265 00266 int GPRS::send(int socket, const char * str, int len) 00267 { 00268 if (socket < 0 || socket > MAX_SOCK_NUM-1) { 00269 return -1; 00270 } 00271 char cmd[32]; 00272 wait(1); 00273 if(len > 0){ 00274 snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",socket); 00275 if(0 != sendCmdAndWaitForResp(cmd,">",2,CMD)) { 00276 return false; 00277 } 00278 sendCmd(str); 00279 serialModem.putc((char)0x1a); 00280 } 00281 return len; 00282 } 00283 00284 int GPRS::recv(int socket, char* buf, int len) 00285 { 00286 if (socket < 0 || socket > MAX_SOCK_NUM-1) { 00287 return -1; 00288 } 00289 cleanBuffer(buf,len); 00290 readBuffer(buf,len,DEFAULT_TIMEOUT/2); 00291 return len; 00292 //return strlen(buf); 00293 } 00294 00295 int GPRS::new_socket() 00296 { 00297 socketID = 0; //we only support one socket. 00298 return socketID; 00299 } 00300 00301 uint16_t GPRS::new_port() 00302 { 00303 uint16_t port = rand(); 00304 port |= 49152; 00305 return port; 00306 } 00307 00308 uint32_t GPRS::str_to_ip(const char* str) 00309 { 00310 uint32_t ip = 0; 00311 char* p = (char*)str; 00312 for(int i = 0; i < 4; i++) { 00313 ip |= atoi(p); 00314 p = strchr(p, '.'); 00315 if (p == NULL) { 00316 break; 00317 } 00318 ip <<= 8; 00319 p++; 00320 } 00321 return ip; 00322 }
Generated on Tue Jul 12 2022 18:52:23 by
1.7.2