bool GPRS::getSignalStrength(int *buffer) { //AT+CSQ --> 6 + CR = 10 //+CSQ: <rssi>,<ber> --> CRLF + 5 + CRLF = 9 //OK --> CRLF + 2 + CRLF = 6 byte i = 0; char gprsBuffer[26]; char *p, *s; char buffers[4]; sim900_flush_serial(); sim900_send_cmd("AT+CSQ\r"); sim900_clean_buffer(gprsBuffer, 26); sim900_read_buffer(gprsBuffer, 26, DEFAULT_TIMEOUT); if (NULL != (s = strstr(gprsBuffer, "+CSQ:"))) { s = strstr((char *)(s), " "); s = s + 1; //We are in the first phone number character p = strstr((char *)(s), ","); //p is last character """ if (NULL != s) { i = 0; while (s < p) { buffers[i++] = *(s++); } buffers[i] = '\0'; } *buffer = atoi(buffers); return true; } return false; }
bool GPRS::getSubscriberNumber(char *number) { //AT+CNUM --> 7 + CR = 8 //+CNUM: "","+628157933874",145,7,4 --> CRLF + 45 + CRLF = 49 // --> //OK --> CRLF + 2 + CRLF = 6 byte i = 0; char gprsBuffer[65]; char *p,*s; sim900_flush_serial(); sim900_send_cmd("AT+CNUM\r\n"); sim900_clean_buffer(gprsBuffer,65); sim900_read_buffer(gprsBuffer,65,DEFAULT_TIMEOUT); //Serial.print(gprsBuffer); if(NULL != ( s = strstr(gprsBuffer,"+CNUM:"))) { s = strstr((char *)(s),","); s = s + 2; //We are in the first phone number character p = strstr((char *)(s),"\""); //p is last character """ if (NULL != s) { i = 0; while (s < p) { number[i++] = *(s++); } number[i] = '\0'; } return true; } return false; }
bool GPRS::getDateTime(char *buffer) { //AT+CCLK? --> 8 + CR = 9 //+CCLK: "14/11/13,21:14:41+04" --> CRLF + 29+ CRLF = 33 // //OK --> CRLF + 2 + CRLF = 6 byte i = 0; char gprsBuffer[50]; char *p,*s; sim900_flush_serial(); sim900_send_cmd("AT+CCLK?\r"); sim900_clean_buffer(gprsBuffer,50); sim900_read_buffer(gprsBuffer,50,DEFAULT_TIMEOUT); if(NULL != ( s = strstr(gprsBuffer,"+CCLK:"))) { s = strstr((char *)(s),"\""); s = s + 1; //We are in the first phone number character p = strstr((char *)(s),"\""); //p is last character """ if (NULL != s) { i = 0; while (s < p) { buffer[i++] = *(s++); } buffer[i] = '\0'; } return true; } return false; }
bool GPRS::getVcc(char *buffer) { //AT+CBC --> 6 + CR //+CBC: 0,100,4241 --> CRLF + 16 + CRLF // //OK --> CRLF + 2 + CRLF byte i = 0; char gprsBuffer[50]; char *p,*s; sim900_flush_serial(); sim900_send_cmd(F("AT+CBC\r")); sim900_clean_buffer(gprsBuffer,50); sim900_read_buffer(gprsBuffer,50,DEFAULT_TIMEOUT); if(NULL != ( s = strstr(gprsBuffer,"+CBC:"))) { s = strstr((char *)(s),","); s = s + 1; s = strstr((char *)(s), ","); s = s + 1; //We are in the first Vcc character p = s + 4; //p is last character if (NULL != s) { i = 0; while (s < p) { buffer[i++] = *(s++); } buffer[i] = '\0'; } return true; } return false; }
boolean sim900_wait_for_resp(const char* resp, DataType type, unsigned int timeout, unsigned int chartimeout) { int len = strlen(resp); int sum = 0; unsigned long timerStart, prevChar; //prevChar is the time when the previous Char has been read. timerStart = millis(); prevChar = 0; while(1) { if(sim900_check_readable()) { char c = serialSIM900->read(); DEBUG("-"); DEBUG(c); prevChar = millis(); sum = (c==resp[sum]) ? sum+1 : 0; if(sum == len)break; } if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) { return false; } //If interchar Timeout => return FALSE. So we can return sooner from this function. if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) { return false; } } //If is a CMD, we will finish to read buffer. if(type == CMD) sim900_flush_serial(); return true; }
bool GPRS::delBookEntry(int index) { char indexStr[4]; itoa(index, indexStr, 10); sim900_flush_serial(); sim900_send_cmd(F("AT+CPBW=")); sim900_send_cmd(indexStr); return sim900_check_with_cmd(F("\r"),"OK\r\n",CMD); }
bool GPRS::queryBearer(uint8_t * bearerStatus) { char receiveBuffer[32]; char * commaPtr; // send AT+SAPBR=2,1 and read "+SAPBR" if (sim900_check_with_cmd(F("AT+SAPBR=2,1\r\n"),"+SAPBR:", DATA) == false) return false; sim900_clean_buffer(receiveBuffer, sizeof(receiveBuffer)); // check response which looks like: // +SAPBR: <cid>,<Status>,<IP_Addr>\r\nOK // read cid (always 1) if (sim900_read_string_until(receiveBuffer, sizeof(receiveBuffer), "1,") == NULL) return false; // read until next comma -> status commaPtr = sim900_read_string_until(receiveBuffer, sizeof(receiveBuffer), ","); if (commaPtr == NULL) return false; // replace comma with string termination *commaPtr = '\0'; // now extract status *bearerStatus = (uint8_t)atol(receiveBuffer); // only check for ip address if we are connected (=1) if (*bearerStatus == 1) { char * endOfIpAddress = NULL; // read ip address, which is enclosed in '"', so read first '"' if (sim900_read_string_until(receiveBuffer, sizeof(receiveBuffer), "\"") == NULL) return false; // read second '"' endOfIpAddress = sim900_read_string_until(receiveBuffer, sizeof(receiveBuffer), "\""); if (endOfIpAddress == NULL) return false; *endOfIpAddress = '\0'; strncpy(ip_string, receiveBuffer, sizeof(ip_string)); _ip = str_to_ip(ip_string); } // flush rest of data which should be "\r\nOK" sim900_flush_serial(); return true;; }
bool GPRS::getLocation(const __FlashStringHelper *apn, float *longitude, float *latitude) { int i = 0; char gprsBuffer[80]; char buffer[20]; char *s; //send AT+SAPBR=3,1,"Contype","GPRS" sim900_check_with_cmd("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r","OK\r\n",CMD); //sen AT+SAPBR=3,1,"APN","GPRS_APN" sim900_send_cmd("AT+SAPBR=3,1,\"APN\",\""); if (apn) { sim900_send_cmd(apn); } sim900_check_with_cmd("\"\r","OK\r\n",CMD); //send AT+SAPBR =1,1 sim900_check_with_cmd("AT+SAPBR=1,1\r","OK\r\n",CMD); //AT+CIPGSMLOC=1,1 sim900_flush_serial(); sim900_send_cmd("AT+CIPGSMLOC=1,1\r"); sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer)); sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),2*DEFAULT_TIMEOUT,6*DEFAULT_INTERCHAR_TIMEOUT); //Serial.println(gprsBuffer); if(NULL != ( s = strstr(gprsBuffer,"+CIPGSMLOC:"))) { s = strstr((char *)s, ","); s = s+1; //Serial.println(*s); i=0; while(*(++s) != ',') buffer[i++]=*s; buffer[i] = 0; *longitude = atof(buffer); i=0; while(*(++s) != ',') buffer[i++]=*s; buffer[i] = 0; *latitude = atof(buffer); return true; } return false; }
bool GPRS::getDateTime(char *buffer) { //If it doesn't work may be for two reasons: // 1. Your carrier doesn't give that information // 2. You have to configurate the SIM900 IC. // - First with SIM900_Serial_Debug example try this AT command: AT+CLTS? // - If response is 0, then it is disabled. // - Enable it by: AT+CLTS=1 // - Now you have to save this config to EEPROM memory of SIM900 IC by: AT&W // - Now, you have to power down and power up again the SIM900 // - Try now again: AT+CCLK? // - It should work //AT+CCLK? --> 8 + CR = 9 //+CCLK: "14/11/13,21:14:41+04" --> CRLF + 29+ CRLF = 33 // //OK --> CRLF + 2 + CRLF = 6 byte i = 0; char gprsBuffer[50]; char *p,*s; sim900_flush_serial(); sim900_send_cmd(F("AT+CCLK?\r")); sim900_clean_buffer(gprsBuffer,50); sim900_read_string_until(gprsBuffer, sizeof(gprsBuffer), "OK\r\n"); if(NULL != ( s = strstr(gprsBuffer,"+CCLK:"))) { s = strstr((char *)(s),"\""); s = s + 1; //We are in the first phone number character p = strstr((char *)(s),"\""); //p is last character """ if (NULL != s) { i = 0; while (s < p) { buffer[i++] = *(s++); } buffer[i] = '\0'; } return true; } return false; }
bool GPRS::sendSMS(char *number, char *data) { //char cmd[32]; if(!sim900_check_with_cmd("AT+CMGF=1\r\n", "OK\r\n", CMD)) { // Set message mode to ASCII return false; } delay(500); sim900_flush_serial(); sim900_send_cmd("AT+CMGS=\""); sim900_send_cmd(number); // sprintf(cmd,"AT+CMGS=\"%s\"\r\n", number); //snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number); // if(!sim900_check_with_cmd(cmd,">",CMD)) { if(!sim900_check_with_cmd("\"\r\n",">",CMD)) { return false; } delay(1000); sim900_send_cmd(data); delay(500); sim900_send_End_Mark(); return sim900_wait_for_resp("OK\r\n", CMD); }
bool GPRS::sendUSSDSynchronous(char *ussdCommand, char *resultcode, char *response) { //AT+CUSD=1,"{command}" //OK // //+CUSD:1,"{response}",{int} byte i = 0; char gprsBuffer[200]; char *p,*s; sim900_clean_buffer(response, sizeof(response)); sim900_flush_serial(); sim900_send_cmd("AT+CUSD=1,\""); sim900_send_cmd(ussdCommand); sim900_send_cmd("\"\r"); if(!sim900_wait_for_resp("OK\r\n", CMD)) return false; sim900_clean_buffer(gprsBuffer,200); sim900_read_buffer(gprsBuffer,200,DEFAULT_TIMEOUT); if(NULL != ( s = strstr(gprsBuffer,"+CUSD: "))) { *resultcode = *(s+7); resultcode[1] = '\0'; if(!('0' <= *resultcode && *resultcode <= '2')) return false; s = strstr(s,"\""); s = s + 1; //We are in the first phone number character p = strstr(s,"\""); //p is last character """ if (NULL != s) { i = 0; while (s < p) { response[i++] = *(s++); } response[i] = '\0'; } return true; } return false; }
bool GPRS::wake(void) { unsigned long timerStart; bool ok = false; //First, send AT dummy command to wake up sim900_send_cmd(F("AT\r\n")); //Second delay almost 100 ms delay(200); sim900_flush_serial(); //Third, send the second AT command to check that it is alive timerStart = millis(); while(!ok && ((unsigned long) (millis() - timerStart) < 3000UL) ) { //Until 3 seconds maximum ok = sim900_check_with_cmd(F("AT\r\n"),"OK",CMD); delay(300); } if (ok) { //Four, exit sleep mode return sim900_check_with_cmd(F("AT+CSCLK=0\r\n"),"OK",CMD); } else { return false; } }
bool GPRS::httpReadResponseData(char* buffer, uint16_t bufferSize) { char receiveBuffer[32]; char * charFoundPtr = NULL; // issue command if (sim900_check_with_cmd(F("AT+HTTPREAD\r\n"), "+HTTPREAD:", DATA) == false) return false; sim900_clean_buffer(receiveBuffer, sizeof(receiveBuffer)); // analyzing response which looks like: //+HTTPREAD:<data_len> //<data> //OK // read first line of response, we ignore the data length as httpSendGetRequest already // returns this information if (sim900_read_string_until(receiveBuffer, sizeof(receiveBuffer), "\r\n") == false) return false; sim900_clean_buffer(buffer, bufferSize); // read HTTP response data and the following "OK" // Note: at this point the response was already received (via GPRS), this is just // reading the data from the module. This should be done in 500ms charFoundPtr = sim900_read_string_until(buffer, bufferSize, "\r\nOK", 1, 500); if (charFoundPtr == NULL) return false; // write \0 to index where the pattern was found to terminate the received HTTP response data *charFoundPtr = '\0'; sim900_flush_serial(); return true; }
bool GPRS::getLocation(const __FlashStringHelper *apn, float *longitude, float *latitude) { int i = 0; char gprsBuffer[80]; char buffer[20]; char *s; if (openBearer(apn) == false) return false; //AT+CIPGSMLOC=1,1 sim900_flush_serial(); sim900_send_cmd(F("AT+CIPGSMLOC=1,1\r")); sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer)); sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),2*DEFAULT_TIMEOUT,6*DEFAULT_INTERCHAR_TIMEOUT); //Serial.println(gprsBuffer); if(NULL != ( s = strstr(gprsBuffer,"+CIPGSMLOC:"))) { s = strstr((char *)s, ","); s = s+1; //Serial.println(*s); i=0; while(*(++s) != ',') buffer[i++]=*s; buffer[i] = 0; *longitude = atof(buffer); i=0; while(*(++s) != ',') buffer[i++]=*s; buffer[i] = 0; *latitude = atof(buffer); return true; } return false; }
bool GPRS::sendSMS(const char *number, const char *data) { //180822 In the init function //if(!sim900_check_with_cmd(F("AT+CMGF=1\r\n"), "OK\r\n", CMD)) { // Set message mode to ASCII // return false; //} //delay(500); sim900_flush_serial(); sim900_send_cmd(F("AT+CMGS=\"")); sim900_send_cmd(number); //sprintf(cmd,"AT+CMGS=\"%s\"\r\n", number); //snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number); // if(!sim900_check_with_cmd(cmd,">",CMD)) { if(!sim900_check_with_cmd(F("\"\r\n"),">",CMD)) { return false; } delay(1000); sim900_send_cmd(data); delay(500); sim900_send_End_Mark(); return sim900_wait_for_resp("OK\r\n", CMD, 20); }