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.
Dependencies: Chemical_Sensor_DMA GPRS DPG_FINAL MBed_Adafruit-GPS-Library SDFileSystem Socket mbed
Diff: GPRS/GPRS.cpp
- Revision:
- 23:0ce1f69ea710
- Parent:
- 22:f46576c40722
--- a/GPRS/GPRS.cpp Sat Dec 19 21:48:00 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,522 +0,0 @@ -/* - GPRS.cpp - 2014 Copyright (c) Seeed Technology Inc. All right reserved. - - Author:lawliet zou(lawliet.zou@gmail.com) - 2014-2-24 - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include "mbed.h" -#include "GPRS.h" -#include "GSMLibrary.h" - -GPRS* GPRS::inst; -char* numbers[100]; -FILE *fpNum; -int number_index = 0; -char phone_number[12]; - -GPRS::GPRS(PinName tx, PinName rx, int baudRate, const char* apn, const char* userName, const char* passWord) : Modem(tx,rx,baudRate) -{ - inst = this; - _apn = apn; - _userName = userName; - _passWord = passWord; - socketID = -1; -} - -bool GPRS::preInit() -{ - //send AT command a couple of times - for(int i = 0; i < 2; i++) { - sendCmd("AT\r\n"); - //set time to update from tower - //sendCmd("AT+CLTS=1\r\n"); - } - return checkSIMStatus(); -} -// added by Noah Milam -void GPRS::start_server() -{ - sendCmdResp("AT+CGATT?\r\n"); - - sendCmdResp("AT+CIPSERVER=1,1234\r\n"); - listen_server(); -} -void GPRS::listen_server() -{ - gprs_response(); -} - -void GPRS::send_SMS(char* number, char* data) -{ - printf("sending text message to: %s\r\n", number); - sendCmdAndWaitForResp("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD); - wait(2); - //printf("\032\n"); - char cmd[64]; - snprintf(cmd,sizeof(cmd),"AT+CMGS=\"+%s\"\r\n",number); - printf(">>>>%s",cmd); - sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT,CMD); - printf("sent at cmgf\n"); - wait(2); - //printf("032"); - sendCmd(data); // sends the address - sendCmd("\x1a"); // this is like pressing control - z to end the send command - wait(10); // giving the send enough time to do its thing - printf("should have been received"); -} - -char* GPRS::read_SMS() -{ - wait(1); - sendCmd("AT+CMGF=1\r\n"); // sms mode - wait(2); - sendCmd("AT+CMGD=1\r\n"); // delete the frist message so incoming message is spot 1 - wait(1); - wait_for_sms(); - wait(10); - sendCmd("AT+CMGR=1\r\n"); - storeResp(); - return get_server_IP(); -} - -// end of what Noah Milam added -bool GPRS::checkSIMStatus(void) -{ - printf("Checking SIM Status...\r\n"); - char gprsBuffer[32]; - int count = 0; - - while(count < 3) { - cleanBuffer(gprsBuffer,32); - sendCmd("AT+CPIN?\r\n"); - readBuffer(gprsBuffer,32,DEFAULT_TIMEOUT); - if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { - break; - } - printf("SIM Not Ready..Try Again--%s--\r\n", gprsBuffer); - count++; - wait(1); - } - if(count == 3) { - return false; - } - printf("SIM Status GOOD!\r\n"); - - //get the phone number - char resp[96]; - printf(">>>>AT+CNUM\r\n"); - sendCmd("AT+CNUM\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"+CNUM:")) { - printf("Response: -%s-", resp); - const char *p1 = strstr(resp, ",\"")+2; - const char *p2 = strstr(p1, "\""); - size_t len = p2-p1; - phoneNumber = (char*)malloc(sizeof(char)*(len+1)); - strncpy(phoneNumber, p1, len); - phoneNumber[len] = '\0'; - } - else{ - printf("Response: -%s-", resp); - return false; - } - return true; - -} -char* GPRS::getCellTime(){ - return cellTime; -} -void GPRS::setCellTime(){ - //what time is it - char resp[96]; - printf(">>>AT+CCLK?\r\n"); - sendCmd("AT+CCLK?\r\n"); - readBuffer(resp,96,(DEFAULT_TIMEOUT/2)); - if(NULL != strstr(resp,"+CCLK")) { //ALREADY CONNECT or CONNECT OK - const char *p1 = strstr(resp, ",")+1; - const char *p2 = strstr(p1, "-"); - size_t len = p2-p1; - cellTime = (char*)malloc(sizeof(char)*(len+1)); - strncpy(cellTime, p1, len); - cellTime[len] = '\0'; - } - else{ - printf("Time is (error): %s\r\n", resp); - } -} -bool GPRS::join() -{ - char cmd[64]; - char ipAddr[32]; - char resp[96]; - - printf(">>>>AT+CREG?\r\n"); - sendCmd("AT+CREG?\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"+CREG: 0,1")) { - printf("Response: -%s-", resp); - } - else{ - printf("Response: -%s-", resp); - return false; - } - - //check if need to attach the device - printf(">>>>AT+CGATT?\r\n"); - sendCmd("AT+CGATT?\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"+CGATT: 1")) { - printf("Response: -%s-", resp); - } - else{ - printf("Response: -%s-", resp); - return false; - } - //check the signal quality - printf(">>>>AT+CSQ?\r\n"); - sendCmd("AT+CSQ?\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - printf("Response: -%s-", resp); - - //close any existing connections - printf(">>>>AT+CIPSHUT\r\n"); - sendCmd("AT+CIPSHUT\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"OK")) { - } - else{ - return false; - } - //check the ip Status - printf(">>>>AT+CIPSTATUS\r\n"); - sendCmd("AT+CIPSTATUS\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"GPRSACT")) { - //Get local IP address - printf(">>>>AT+CIFSR\r\n"); - sendCmd("AT+CIFSR\r\n"); - readBuffer(ipAddr,32,2); - printf(">>>>AT+CIFSR returns: %s\r\n", ipAddr); - if(NULL != strstr(ipAddr,".")) { - _ip = str_to_ip(ipAddr+12); - if(_ip != 0) { - return true; - } - } - } - else if(NULL != strstr(resp,"INITIAL")){ - printf("Response: -%s-", resp); - - } - else if(NULL != strstr(resp,"CONNECT OK")){ - printf("Response: -%s-", resp); - - } - else if(NULL != strstr(resp,"START")){ - - //Get local IP address - printf(">>>>AT+CIFSR\r\n"); - sendCmd("AT+CIFSR\r\n"); - readBuffer(ipAddr,32,2); - printf(">>>>AT+CIFSR returns: %s\r\n", ipAddr); - if(NULL != strstr(ipAddr,".")) { - _ip = str_to_ip(ipAddr+12); - if(_ip != 0) { - return true; - } - } - } - else{ - printf("Response: -%s-", resp); - return false; - } - - //single connection - printf(">>>>AT+CIPMUX=0\r\n"); - sendCmd("AT+CIPMUX=0\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"OK")) { - printf("Response: -%s-", resp); - } - else{ - printf("Response: -%s-", resp); - return false; - } - - //set APN - snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord); - printf(">>>>%s",cmd); - //sendCmdAndWaitForResp(cmd, "OK", DEFAULT_TIMEOUT,CMD); - sendCmd(cmd); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"OK")) { - printf("Response: -%s-", resp); - } - else{ - printf("Response: -%s-", resp); - return false; - } - //Brings up wireless connection - printf(">>>>AT+CIICR\r\n"); - sendCmd("AT+CIICR\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT*3); - if(NULL != strstr(resp,"OK")) { - printf("Response: -%s-", resp); - } - else{ - printf("Response: -%s-", resp); - return false; - } - - //Get local IP address - printf(">>>>AT+CIFSR\r\n"); - sendCmd("AT+CIFSR\r\n"); - readBuffer(ipAddr,32,2); - printf(">>>>AT+CIFSR returns: %s\r\n", ipAddr); - if(NULL != strstr(ipAddr,".")) { - _ip = str_to_ip(ipAddr+12); - if(_ip != 0) { - return true; - } - } - printf(">>>>AT+CIPQSEND=1\r\n"); - sendCmd("AT+CIPQSEND=1\r\n"); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"OK")) { - printf("Response: -%s-", resp); - } - else{ - printf("Response: -%s-", resp); - return false; - } - return false; -} - -bool GPRS::setProtocol(int socket, Protocol p) -{ - if (socket < 0 || socket > MAX_SOCK_NUM-1) { - return false; - } - //ToDo: setProtocol - return true; -} - -bool GPRS::connect(int socket, Protocol ptl,const char * host, int port, int timeout) -{ - char cmd[64]; - char resp[96]; - if (socket < 0 || socket > MAX_SOCK_NUM-1) { - return false; - } - if(ptl == TCP) { - sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n", host, port); - } else if(ptl == UDP) { - sprintf(cmd, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d\r\n",socket, host, port); - } else { - return false; - } - - printf("CONNECT: %s", cmd); - sendCmd(cmd); - cleanBuffer(resp,96); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK - printf("SUCCESS: %s\r\n", resp); - - return true; - } - printf("FAILURE: %s\r\n", resp); - return false;//ERROR -} - -bool GPRS::gethostbyname(const char* host, uint32_t* ip) -{ - uint32_t addr = str_to_ip(host); - char buf[17]; - snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); - if (strcmp(buf, host) == 0) { - *ip = addr; - return true; - } - return false; -} - -bool GPRS::disconnect() -{ - char resp[96]; - printf(">>>AT+CIPSHUT\r\n"); - sendCmd("AT+CIPSHUT\r\n"); - readBuffer(resp,96,DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"SHUT OK")) { //ALREADY CONNECT or CONNECT OK - printf("Response: %s\r\n", resp); - } - else{ - printf("Response: %s\r\n", resp); - return false; - } - return true; -} - -bool GPRS::is_connected(int socket) -{ - char cmd[16]; - char resp[96]; - snprintf(cmd,16,"AT+CIPSTATUS\r\n"); - sendCmd(cmd); - readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT); - if(NULL != strstr(resp,"CONNECT OK")) { - printf("Response -%s-\r\n", resp); - return true; - } else { - printf("Fail Response -%s-\r\n", resp); - return false; - } -} - -void GPRS::reset() -{ -} - -bool GPRS::close(int socket) -{ - char cmd[16]; - char resp[16]; - - if (socket < 0 || socket > MAX_SOCK_NUM-1) { - return false; - } - // if not connected, return - if (is_connected(socket) == false) { - return true; - } - snprintf(cmd, sizeof(cmd),"AT+CIPCLOSE=%d\r\n",socket); - snprintf(resp,sizeof(resp),"%d, CLOSE OK",socket); - if(0 != sendCmdAndWaitForResp(cmd, resp, DEFAULT_TIMEOUT,CMD)) { - return false; - } - return true; -} - -bool GPRS::readable(void) -{ - return readable(); -} - -int GPRS::wait_readable(int socket, int wait_time) -{ - if (socket < 0 || socket > MAX_SOCK_NUM-1) { - return -1; - } - char resp[16]; - snprintf(resp,sizeof(resp),"\r\n\r\n+RECEIVE,%d",socket);//"+RECEIVE:<socketID>,<length>" - int len = strlen(resp); - - if(false == respCmp(resp,len,wait_time)) { - return -1; - } - char c = readByte();//',' - char dataLen[4]; - int i = 0; - c = readByte(); - while((c >= '0') && (c <= '9')) { - dataLen[i++] = c; - c = readByte(); - } - c = readByte();//'\n' - len = atoi(dataLen); - return len; -} - -int GPRS::wait_writeable(int socket, int req_size) -{ - if (socket < 0 || socket > MAX_SOCK_NUM-1) { - return -1; - } - return req_size>256?256:req_size+1; -} - -int GPRS::send(int socket, const char * str, int len) -{ - if (socket < 0 || socket > MAX_SOCK_NUM-1) { - return -1; - } - char cmd[32]; - if(len > 0){ - //printf("Sending data: %s\r\n", str); - snprintf(cmd,sizeof(cmd),"AT+CIPSEND\r\n"); - if(0 != sendCmdAndWaitForResp(cmd,">",1,CMD)) { - printf("Couldn't send data!!"); - return false; - } - sendCmd(str); - serialModem.putc((char)0x1a); - - } - return len; -} - -int GPRS::recv(int socket, char* buf, int len) -{ - if (socket < 0 || socket > MAX_SOCK_NUM-1) { - return -1; - } - cleanBuffer(buf,len); - readBuffer(buf,len,DEFAULT_TIMEOUT/2); - return len; - //return strlen(buf); -} - -int GPRS::new_socket() -{ - socketID = 0; //we only support one socket. - return socketID; -} - -uint16_t GPRS::new_port() -{ - uint16_t port = rand(); - port |= 49152; - return port; -} - -uint32_t GPRS::str_to_ip(const char* str) -{ - uint32_t ip = 0; - char* p = (char*)str; - for(int i = 0; i < 4; i++) { - ip |= atoi(p); - p = strchr(p, '.'); - if (p == NULL) { - break; - } - ip <<= 8; - p++; - } - return ip; -} \ No newline at end of file