/********************************************************** Method sends AT command and waits for response return: AT_RESP_ERR_NO_RESP = -1, // no response received AT_RESP_ERR_DIF_RESP = 0, // response_string is different from the response AT_RESP_OK = 1, // response_string was included in the response **********************************************************/ char GSM::SendATCmdWaitResp(char const *AT_cmd_string, uint16_t start_comm_tmout, uint16_t max_interchar_tmout, char const *response_string, byte no_of_attempts) { byte status; char ret_val = AT_RESP_ERR_NO_RESP; byte i; for (i = 0; i < no_of_attempts; i++) { // delay 500 msec. before sending next repeated AT command // so if we have no_of_attempts=1 tmout will not occurred if (i > 0) delay(500); _cell.println(AT_cmd_string); status = WaitResp(start_comm_tmout, max_interchar_tmout); if (status == RX_FINISHED) { // something was received but what was received? // --------------------------------------------- if(IsStringReceived(response_string)) { ret_val = AT_RESP_OK; break; // response is OK => finish } else ret_val = AT_RESP_ERR_DIF_RESP; } else { // nothing was received // -------------------- ret_val = AT_RESP_ERR_NO_RESP; } } WaitResp(1000, 5000); return (ret_val); }
//------------------------------------------------------------------------------ void ubxReadShortMessage(void) { unsigned int sms_no = 301; char *ptr, buf[20]; do { if(sms_no > 330) break; sprintf(buf, (const far rom char*)"AT+CMGR=%d\r", sms_no); SerialWriteString(buf); // wait for response if (RX_TMOUT_ERR == WaitResp(500, 5)) return; // check response if(IsStringReceived("+CMGR")) { ptr = strstrrampgm(comm_buf, (const rom far char *)"+CMGR"); ptr = strchr(ptr, '\n') + 1; *(ptr + 6) = 0; (*sms_handler_ptr)(ptr); } sms_no++; Delay10KTCYx(100); }while(ubxIsShortMessageReceived() == UBX_SMS_S_RECEIVED); // delete all read messages SerialWriteRomString("AT+CMGD=1,1\r"); // wait response WaitResp(500, 5); }
/********************************************************** Method sets GPIO pin according required value it doesn't check if the pin was previously set as OUTPUT so INPUT pin is automatically switch as the output pin by using this function gpio_pin: 10..13 value: 0 - "0" as LOW 1 - "1" as HIGH return: ERROR ret. val: --------------- -1 - comm. line to the GSM module is not free -2 - GSM module didn't answer in timeout -3 - GSM module has answered "ERROR" string OK ret val: ----------- 0 - set to the "0" - LOW 1 - set to the "1" - HIGH **********************************************************/ char GSM::SetGPIOVal(byte GPIO_pin, byte value) { char ret_val = -1; if (CLS_FREE != GetCommLineStatus()) return (ret_val); SetCommLineStatus(CLS_ATCMD); // e.g. AT#GPIO=9,0,1 - set to "0" - LOW // or AT#GPIO=9,1,1 - set to "1" - HIGH Serial.print("AT#GPIO="); // pin number Serial.print((int)GPIO_pin); Serial.print(","); if (value > 1) value = 1; Serial.print((int)value); Serial.print(",1\r"); // pin is always set as output // 100 msec. for initial comm tmout // 20 msec. for inter character timeout if (RX_TMOUT_ERR == WaitResp(100, 20)) { ret_val = -2; // ERROR } else { if(IsStringReceived("OK")) { ret_val = value; // OK } else ret_val = -3; // ERROR } SetCommLineStatus(CLS_FREE); return (ret_val); }
/********************************************************** Method sets direction for GPIO pins GPIO10, GPIO11, GPIO12, GPIO13 gpio_pin: 10..13 direction: 0 - input 1 - output return: ERROR ret. val: --------------- -1 - comm. line to the GSM module is not free -2 - GSM module didn't answer in timeout -3 - GSM module has answered "ERROR" string OK ret val: ----------- 0 - set as INPUT 1 - set as OUTPUT **********************************************************/ char GSM::SetGPIODir(byte GPIO_pin, byte direction) { char ret_val = -1; if (CLS_FREE != GetCommLineStatus()) return (ret_val); SetCommLineStatus(CLS_ATCMD); // e.g. AT#GPIO=9,0,0 - sets as INPUT // e.g. AT#GPIO=9,0,1 - sets as OUTPUT and value is "0" - LOW Serial.print("AT#GPIO="); // pin number Serial.print((int)GPIO_pin); Serial.print(",0,"); // if case pin will be OUTPUT - // first value after initialization will be 0 if (direction > 1) direction = 1; Serial.print((int)direction); // 0-INPUT, 1-OUTPUT Serial.print("\r"); // finish command = send<CR> // 100 msec. for initial comm tmout // 20 msec. for inter character timeout if (RX_TMOUT_ERR == WaitResp(100, 20)) { ret_val = -2; // ERROR } else { if(IsStringReceived("OK")) { ret_val = direction; // OK } else ret_val = -3; // ERROR } SetCommLineStatus(CLS_FREE); return (ret_val); }
/********************************************************** Method sets speaker volume speaker_volume: volume in range 0..14 return: ERROR ret. val: --------------- -1 - comm. line to the GSM module is not free -2 - GSM module did not answer in timeout -3 - GSM module has answered "ERROR" string OK ret val: ----------- 0..100 current speaker volume **********************************************************/ char GSM::SetSpeakerVolume(byte speaker_volume) { char ret_val = -1; if (CLS_FREE != GetCommLineStatus()) return (ret_val); SetCommLineStatus(CLS_ATCMD); // remember set value as last value if (speaker_volume > 100) speaker_volume = 100; // select speaker volume (0 to 100) // AT+CLVL=X<CR> X<0..100> Serial.print(F("AT+CLVL=")); Serial.print((int)speaker_volume); Serial.print(F("\r")); // send <CR> // 10 sec. for initial comm tmout // 20 msec. for inter character timeout if (RX_TMOUT_ERR == WaitResp(10000, 20)) { ret_val = -2; // ERROR } else { if(IsStringReceived("OK")) { last_speaker_volume = speaker_volume; ret_val = last_speaker_volume; // OK } else ret_val = -3; // ERROR } SetCommLineStatus(CLS_FREE); return (ret_val); }
/********************************************************** Method del phone number from the specified SIM position position: SMS position <1..20> return: ERROR ret. val: --------------- -1 - comm. line to the GSM module is not free -2 - GSM module didn't answer in timeout -3 - position must be > 0 OK ret val: ----------- 0 - phone number was not deleted 1 - phone number was deleted **********************************************************/ char GSM::DelPhoneNumber(byte position) { char ret_val = -1; if (position == 0) return (-3); if (CLS_FREE != GetCommLineStatus()) return (ret_val); SetCommLineStatus(CLS_ATCMD); ret_val = 0; // phone number was not written yet //send: AT+CPBW=XY // where XY = position _cell.print(F("AT+CPBW=")); _cell.print((int)position); _cell.print(F("\r")); // 5000 msec. for initial comm tmout // 50 msec. for inter character timeout switch (WaitResp(5000, 50, "OK")) { case RX_TMOUT_ERR: // response was not received in specific time break; case RX_FINISHED_STR_RECV: // response is OK = has been written ret_val = 1; break; case RX_FINISHED_STR_NOT_RECV: // other response: e.g. ERROR break; } SetCommLineStatus(CLS_FREE); return (ret_val); }
GSM::RXstateRes GSM::WaitResp(unsigned long int start_comm_tmout, unsigned long int max_interchar_tmout, char const *expected_resp_string) { char status; RXstateRes ret_val; status = WaitResp(start_comm_tmout, max_interchar_tmout); if (status == RX_FINISHED) { // something was received but what was received? // --------------------------------------------- if (IsStringReceived(expected_resp_string)) { // expected string was received // ---------------------------- ret_val = RX_FINISHED_STR_RECV; } else { ret_val = RX_FINISHED_STR_NOT_RECV; } } else { // nothing was received // -------------------- ret_val = RX_TMOUT_ERR; } return (ret_val); }
/********************************************************** Method update signal level return: OK ret val: ----------- 1 - Signal Level updated 0 - Signal level not updated ERROR ret. val: --------------- -1 - comm. line to the GSM module is not free -2 - GSM module didn't answer in timeout **********************************************************/ char GSM::UpdateSignalLevel() { char ret_val = -1; char sig=0; char *p_char; char *p_char1; if (CLS_FREE != GetCommLineStatus()) return (ret_val); SetCommLineStatus(CLS_ATCMD); ret_val = 0; // not found yet Serial.print(F("AT+CSQ\r")); switch (WaitResp(1000, 20, "+CSQ")) { case RX_TMOUT_ERR: // response was not received in specific time ret_val = -2; break; case RX_FINISHED_STR_RECV: p_char = strchr((char *)(comm_buf),':'); if (p_char != NULL) { p_char++; // we are on the first battery level character // find out ',' as finish character of battery level string p_char1 = strchr((char *)(p_char),','); if (p_char1 != NULL) { *p_char1 = 0; // end of string sig= atoi(p_char); //Convert string to integer 0-100% } } if((sig==0) || (sig==99)) signalLevel=0; else if((sig>0) && (sig<=5)) signalLevel=1; else if((sig>5) && (sig<=12)) signalLevel=2; else if((sig>12) && (sig<=17)) signalLevel=3; else if((sig>17) && (sig<=22)) signalLevel=4; else if((sig>22) && (sig<=32)) signalLevel=5; else signalLevel=0; ret_val =1; break; case RX_FINISHED_STR_NOT_RECV: ret_val = 0; break; } SetCommLineStatus(CLS_FREE); return (ret_val); }
char SIMCOM900::forceON(){ char ret_val=0; char *p_char; char *p_char1; SimpleWriteln(F("AT+CREG?")); WaitResp(5000, 100, "OK"); if(IsStringReceived("OK")){ ret_val=1; } //BCL p_char = strchr((char *)(gsm.comm_buf),','); p_char1 = p_char+1; //we are on the first char of BCS *(p_char1+2)=0; p_char = strchr((char *)(p_char1), ','); if (p_char != NULL) { *p_char = 0; } if((*p_char1)=='4'){ digitalWrite(GSM_ON, HIGH); delay(1200); digitalWrite(GSM_ON, LOW); delay(10000); ret_val=2; } return ret_val; }
/********************************************************** Method sends DTMF signal This function only works when call is in progress dtmf_tone: tone to send 0..15 return: ERROR ret. val: --------------- -1 - comm. line to the GSM module is not free -2 - GSM module didn't answer in timeout -3 - GSM module has answered "ERROR" string OK ret val: ----------- 0.. tone **********************************************************/ char GSM::SendDTMFSignal(byte dtmf_tone) { char ret_val = -1; if (CLS_FREE != GetCommLineStatus()) return (ret_val); SetCommLineStatus(CLS_ATCMD); // e.g. AT+VTS=5<CR> Serial.print(F("AT+VTS=")); Serial.print((int)dtmf_tone); Serial.print(F("\r")); // 1 sec. for initial comm tmout // 20 msec. for inter character timeout if (RX_TMOUT_ERR == WaitResp(1000, 20)) { ret_val = -2; // ERROR } else { if(IsStringReceived("OK")) { ret_val = dtmf_tone; // OK } else ret_val = -3; // ERROR } SetCommLineStatus(CLS_FREE); return (ret_val); }
//------------------------------------------------------------------------------ UBX_GPRS_STATUS ubxGetGprsNetworkStatus(void) { UBX_GPRS_STATUS ret_val; // send AT+CGREG? command SerialWriteRomString("AT+CGREG?\r"); // wait response if (RX_TMOUT_ERR == WaitResp(500, 5)) ret_val = UBX_GPRS_S_TIMEOUT; // check response else { if(IsStringReceived("+CGREG: 0,0")) ret_val = UBX_GPRS_S_NOT_SEARCHING; else if(IsStringReceived("+CGREG: 0,1")) ret_val = UBX_GPRS_S_REGISTERED; else if(IsStringReceived("+CGREG: 0,2")) ret_val = UBX_GPRS_S_SEARCHING; else if(IsStringReceived("+CGREG: 0,3") || IsStringReceived("+CGREG: 0,4")) ret_val = UBX_GPRS_S_DENIED_OR_UNKNOWN; else if(IsStringReceived("ERROR")) ret_val = UBX_GPRS_S_ERROR; else ret_val = UBX_GPRS_S_UNKNOWN_RESPONSE; } return ret_val; }
/********************************************************** Method checks if the GSM module is registered in the GSM net - this method communicates directly with the GSM module in contrast to the method IsRegistered() which reads the flag from the module_status (this flag is set inside this method) - must be called regularly - from 1sec. to cca. 10 sec. return values: REG_NOT_REGISTERED - not registered REG_REGISTERED - GSM module is registered REG_NO_RESPONSE - GSM doesn't response REG_COMM_LINE_BUSY - comm line between GSM module and Arduino is not free for communication **********************************************************/ GSM::RegistrationStatus GSM::CheckRegistration(void) { RXstateRes status; RegistrationStatus ret_val = REG_NOT_REGISTERED; if (CLS_FREE != getCOMStatus()) return REG_COMM_LINE_BUSY; setCOMStatus(CLS_ATCMD); print(F("AT+CREG?")); print("\r"); // 5 sec. for initial comm tmout // 50 msec. for inter character timeout status = WaitResp(5000, 50); if (status == RX_FINISHED) { // something was received but what was received? // --------------------------------------------- if (IsStringReceived("+CREG: 0,1") || IsStringReceived("+CREG: 0,5")) { // it means module is registered // ---------------------------- setRegistrationStatus(REG_REGISTERED); // in case GSM module is registered first time after reset // sets flag STATUS_INITIALIZED // it is used for sending some init commands which // must be sent only after registration // -------------------------------------------- if (!isInitialized()) { setGSMStatus(GSM_INITIALIZED); setCOMStatus(CLS_FREE); InitParam(PARAM_SET_1); DEBUG(F("Status: Initialized")); } ret_val = REG_REGISTERED; INFO(F("Status: Registered")); } else { // NOT registered // -------------- setRegistrationStatus(REG_NOT_REGISTERED); ret_val = REG_NOT_REGISTERED; DATA(F("Status: Not registered")); } } else { // nothing was received // -------------------- ret_val = REG_NO_RESPONSE; WARNING(F("Status: Not response")); } setCOMStatus(CLS_FREE); return (ret_val); }
char GSM::GetPhoneNumber(byte position, char *phone_number) { char ret_val = -1; char *p_char; char *p_char1; if (position == 0) return (-3); if (CLS_FREE != GetCommLineStatus()) return (ret_val); SetCommLineStatus(CLS_ATCMD); ret_val = 0; // not found yet phone_number[0] = 0; // phone number not found yet => empty string //send "AT+CPBR=XY" - where XY = position _cell.print(F("AT+CPBR=")); _cell.print((int)position); _cell.print("\r"); // 5000 msec. for initial comm tmout // 50 msec. for inter character timeout switch (WaitResp(5000, 50, "+CPBR")) { case RX_TMOUT_ERR: // response was not received in specific time ret_val = -2; break; case RX_FINISHED_STR_RECV: // response in case valid phone number stored: // <CR><LF>+CPBR: <index>,<number>,<type>,<text><CR><LF> // <CR><LF>OK<CR><LF> // response in case there is not phone number: // <CR><LF>OK<CR><LF> p_char = strstr((char *)(comm_buf),",\""); if (p_char != NULL) { p_char++; p_char++; // we are on the first phone number character // find out '"' as finish character of phone number string p_char1 = strchr((char *)(p_char),'"'); if (p_char1 != NULL) { *p_char1 = 0; // end of string } // extract phone number string strcpy(phone_number, (char *)(p_char)); // output value = we have found out phone number string ret_val = 1; } break; case RX_FINISHED_STR_NOT_RECV: // only OK or ERROR => no phone number ret_val = 0; break; } SetCommLineStatus(CLS_FREE); return (ret_val); }
int GSM::getSignalLevel() { print(F("AT+CSQ\r")); WaitResp(2000, 300); char buf[7]; getString(":", "\r", buf, 6); int signalLevel, RXQUAL; sscanf(buf, "%d,%d", &signalLevel, &RXQUAL); return signalLevel; }
/********************************************************** Method calls the specific number number_string: pointer to the phone number string e.g. gsm.Call("+420123456789"); **********************************************************/ void GSM::Call(char *number_string) { if (CLS_FREE != GetCommLineStatus()) return; SetCommLineStatus(CLS_ATCMD); // ATDxxxxxx;<CR> Serial.print(F("ATD")); Serial.print(number_string); Serial.print(F(";\r")); // 10 sec. for initial comm tmout // 20 msec. for inter character timeout WaitResp(10000, 20); SetCommLineStatus(CLS_FREE); }
/********************************************************** Method checks if the GSM module is registered in the GSM net - this method communicates directly with the GSM module in contrast to the method IsRegistered() which reads the flag from the module_status (this flag is set inside this method) - must be called regularly - from 1sec. to cca. 10 sec. return values: REG_NOT_REGISTERED - not registered REG_REGISTERED - GSM module is registered REG_NO_RESPONSE - GSM doesn't response REG_COMM_LINE_BUSY - comm line between GSM module and Arduino is not free for communication **********************************************************/ byte GSM::CheckRegistration(void) { byte status; byte ret_val = REG_NOT_REGISTERED; if (CLS_FREE != GetCommLineStatus()) return (REG_COMM_LINE_BUSY); SetCommLineStatus(CLS_ATCMD); _cell.println(F("AT+CREG?")); // 5 sec. for initial comm tmout // 50 msec. for inter character timeout status = WaitResp(5000, 50); if (status == RX_FINISHED) { // something was received but what was received? // --------------------------------------------- if(IsStringReceived("+CREG: 0,1") || IsStringReceived("+CREG: 0,5")) { // it means module is registered // ---------------------------- module_status |= STATUS_REGISTERED; // in case GSM module is registered first time after reset // sets flag STATUS_INITIALIZED // it is used for sending some init commands which // must be sent only after registration // -------------------------------------------- if (!IsInitialized()) { module_status |= STATUS_INITIALIZED; SetCommLineStatus(CLS_FREE); InitParam(PARAM_SET_1); } ret_val = REG_REGISTERED; } else { // NOT registered // -------------- module_status &= ~STATUS_REGISTERED; ret_val = REG_NOT_REGISTERED; } } else { // nothing was received // -------------------- ret_val = REG_NO_RESPONSE; } SetCommLineStatus(CLS_FREE); return (ret_val); }
/********************************************************** Method sends AT command and waits for response return: AT_RESP_ERR_NO_RESP = -1, // no response received AT_RESP_ERR_DIF_RESP = 0, // response_string is different from the response AT_RESP_OK = 1, // response_string was included in the response **********************************************************/ GSM::ATresp GSM::SendATCmdWaitResp(char const *AT_cmd_string, unsigned long int start_comm_tmout, unsigned long int max_interchar_tmout, char const *response_string, char no_of_attempts) { DATA(F("SendATCmdWaitResp")); RXstateRes status; ATresp ret_val = AT_RESP_ERR_NO_RESP; char i; for (i = 0; i < no_of_attempts; i++) { // delay 500 msec. before sending next repeated AT command // so if we have no_of_attempts=1 tmout will not occurred if (i > 0) { delay(500); DEBUG(F("SendATCmdWaitResp: next attempts")); } DATA(AT_cmd_string); print(AT_cmd_string); print(F("\r\n")); status = WaitResp(start_comm_tmout, max_interchar_tmout); if (status == RX_FINISHED) { // something was received but what was received? // --------------------------------------------- if (IsStringReceived(response_string)) { ret_val = AT_RESP_OK; DATA(response_string); break; // response is OK => finish } else ret_val = AT_RESP_ERR_DIF_RESP; } else { DEBUG(F("SendATCmdWaitResp: error or nothing received")); // nothing was received // -------------------- ret_val = AT_RESP_ERR_NO_RESP; } } return (ret_val); }
/********************************************************** Method checks status of call return: CALL_NONE - no call activity CALL_INCOM_VOICE - incoming voice CALL_ACTIVE_VOICE - active voice CALL_NO_RESPONSE - no response to the AT command CALL_COMM_LINE_BUSY - comm line is not free **********************************************************/ byte GSM::CallStatus(void) { byte ret_val = CALL_NONE; if (CLS_FREE != GetCommLineStatus()) return (CALL_COMM_LINE_BUSY); SetCommLineStatus(CLS_ATCMD); Serial.println(F("AT+CPAS")); // 5 sec. for initial comm tmout // 20 msec. for inter character timeout if (RX_TMOUT_ERR == WaitResp(5000, 20)) { // nothing was received (RX_TMOUT_ERR) // ----------------------------------- ret_val = CALL_NO_RESPONSE; } else { // something was received but what was received? // --------------------------------------------- // ready (device allows commands from TA/TE) // <CR><LF>+CPAS: 0<CR><LF> <CR><LF>OK<CR><LF> // unavailable (device does not allow commands from TA/TE) // <CR><LF>+CPAS: 1<CR><LF> <CR><LF>OK<CR><LF> // unknown (device is not guaranteed to respond to instructions) // <CR><LF>+CPAS: 2<CR><LF> <CR><LF>OK<CR><LF> - NO CALL // ringing // <CR><LF>+CPAS: 3<CR><LF> <CR><LF>OK<CR><LF> - NO CALL // call in progress // <CR><LF>+CPAS: 4<CR><LF> <CR><LF>OK<CR><LF> - NO CALL if(IsStringReceived("0")) { // ready - there is no call // ------------------------ ret_val = CALL_NONE; } else if(IsStringReceived("3")) { // incoming call // -------------- ret_val = CALL_INCOM_VOICE; } else if(IsStringReceived("4")) { // active call // ----------- ret_val = CALL_ACTIVE_VOICE; } } SetCommLineStatus(CLS_FREE); return (ret_val); }
/********************************************************** Method calls the number stored at the specified SIM position sim_position: position in the SIM <1...> e.g. gsm.Call(1); **********************************************************/ void GSM::Call(int sim_position) { if (CLS_FREE != GetCommLineStatus()) return; SetCommLineStatus(CLS_ATCMD); // ATD>"SM" 1;<CR> Serial.print(F("ATD>\"SM\" ")); Serial.print(sim_position); Serial.print(F(";\r")); // 10 sec. for initial comm tmout // 20 msec. for inter character timeout WaitResp(10000, 20); SetCommLineStatus(CLS_FREE); }
signed char TelitClass::CheckStatus(void) { // Telit.WriteStr_P(PSTR("AT\r\n")); signed char status = -1; _delay_ms(500); for (unsigned char i =0; i<4; i++) { if (SendCmdWaitResp(PSTR("AT+CGATT?"), PSTR("+CGATT: 1"), pause64) > 0 ) { //Пришел ответ "+CGATT: 1", ждем теперь "OK" if (WaitResp(OK, pause64) > 0) {status = 1;break;}; } _delay_ms(1000); } return status; }
/********************************************************** Method closes previously opened socket return: ERROR ret. val: --------------- -1 - comm. line is not in the data(GPRS) state OK ret val: ----------- 0 - socket was not closed 1 - socket was successfully closed an example of usage: GSM gsm; gsm.CloseSocket(); **********************************************************/ char GSM::CloseSocket(void) { char ret_val = -1; byte i; byte* rx_data; if (CLS_FREE == GetCommLineStatus()) { ret_val = 1; // socket was already closed return (ret_val); } // we are in the DATA state so try to close the socket // --------------------------------------------------- for (i = 0; i < 3; i++) { // make dalay 500msec. before escape seq. "+++" RcvData(1500, 100, &rx_data); // trick - function is used for generation a delay // send escape sequence +++ and wait for "NO CARRIER" SendData("+++"); if (RX_FINISHED_STR_RECV == WaitResp(5000, 1000, "OK")) { SetCommLineStatus(CLS_ATCMD); ret_val = SendATCmdWaitResp("AT+CIPCLOSE", 5000, 1000, "CLOSE OK", 2); if (ret_val == AT_RESP_OK) { // socket was successfully closed ret_val = 1; } else ret_val = 0; // socket was not successfully closed SetCommLineStatus(CLS_FREE); break; } else { // try common AT command just to be sure that the socket // has not been already closed ret_val = SendATCmdWaitResp("AT", 1000, 1000, "OK", 2); if (ret_val == AT_RESP_OK) { // socket was successfully closed ret_val = 1; SetCommLineStatus(CLS_FREE); break; } else { ret_val = 0; } } } return (ret_val); }
//------------------------------------------------------------------------------ char SendATCmdWaitResp (const rom char *AT_cmd_string, unsigned int start_comm_tmout, unsigned int max_interchar_tmout, const rom char *response_string, unsigned char no_of_attempts) { unsigned char status; char ret_val = AT_RESP_ERR_NO_RESP; unsigned char i; for (i = 0; i < no_of_attempts; i++) { // delay 500 msec. before sending next repeated AT command // so if we have no_of_attempts=1 tmout will not occurred if (i > 0) Delay10KTCYx(100); SerialWriteRomString(AT_cmd_string); //wait for response status = WaitResp(start_comm_tmout, max_interchar_tmout); if (status == RX_FINISHED) { // something was received but what was received? // --------------------------------------------- if(IsStringReceived(response_string)) { ret_val = AT_RESP_OK; break; // response is OK => finish } else ret_val = AT_RESP_ERR_DIF_RESP; } else { // nothing was received // -------------------- ret_val = AT_RESP_ERR_NO_RESP; } } return (ret_val); }
//------------------------------------------------------------------------------ UBX_SMS_STATUS ubxIsShortMessageStorageFull(void) { UBX_SMS_STATUS ret_val; char *ptr, i, descr = 1; // send AT+CIND? command SerialWriteRomString("AT+CIND?\r"); // wait for response if (RX_TMOUT_ERR == WaitResp(5000, 50)) ret_val = UBX_SMS_S_TIMEOUT; // check response else { if(IsStringReceived("+CIND")) { ptr = strstrrampgm(comm_buf, (const rom far char *)"+CIND"); for(i = 0; i <28; i++) { if(ptr[i] == ',') { descr++; continue; } if(descr == 8) { if(ptr[i] == '1') ret_val = UBX_SMS_S_FULL; else if(ptr[i] == '0') ret_val = UBX_SMS_S_NOT_FULL; break; } } } else if(IsStringReceived("ERROR")) ret_val = UBX_SMS_S_ERROR; else ret_val = UBX_SMS_S_UNKNOWN_RESPONSE; } return ret_val; }
//------------------------------------------------------------------------------ UBX_ERROR ubxGetModemStatus(void) { UBX_ERROR ret_val; // send AT command SerialWriteRomString("AT\r"); // wait response if (RX_TMOUT_ERR == WaitResp(500, 5)) ret_val = UBX_E_TIMEOUT; // check response else { if(IsStringReceived("OK")) ret_val = UBX_E_OK; else if(IsStringReceived("ERROR")) ret_val = UBX_E_ERROR; else ret_val = UBX_E_UNKNOWN_RESPONSE; } return ret_val; }
//------------------------------------------------------------------------------ UBX_ERROR ubxRegisterPdpService(void) { UBX_ERROR ret_val; // send AT+UPSDA=0,3 command SerialWriteRomString("AT+UPSDA=0,3\r"); // up to 1min timeout in case of registration! // it shoud be increased to 3min but this is the max of uint. if (RX_TMOUT_ERR == WaitResp(6000, 5)) ret_val = UBX_E_TIMEOUT; // check response else { if(IsStringReceived("OK")) ret_val = UBX_E_OK; else if(IsStringReceived("ERROR")) ret_val = UBX_E_ERROR; else ret_val = UBX_E_UNKNOWN_RESPONSE; } return ret_val; }
/********************************************************** Method gets GPIO pin last state gpio_pin: 10..13 value: 0 - "0" as LOW 1 - "1" as HIGH return: ERROR ret. val: --------------- -1 - comm. line to the GSM module is not free -2 - GSM module didn't answer in timeout -3 - GSM module has answered "ERROR" string OK ret val: ----------- 0 - pin is in the "0" - LOW state 1 - pin is in the "1" - HIGH state **********************************************************/ char GSM::GetGPIOVal(byte GPIO_pin) { char ret_val = -1; if (CLS_FREE != GetCommLineStatus()) return (ret_val); SetCommLineStatus(CLS_ATCMD); //e.g. AT#GPIO=9,2 Serial.print("AT#GPIO="); // pin number Serial.print((int)GPIO_pin); Serial.print(",2\r"); // 100 msec. for initial comm tmout // 20 msec. for inter character timeout // required answer: #GPIO: 0,1 - input pin set to 1(HIGH) // #GPIO: 0,0 - input pin set to 0(LOW) if (RX_TMOUT_ERR == WaitResp(100, 20)) { ret_val = -2; // ERROR } else { // there is response but which one.. // --------------------------------- if(IsStringReceived("#GPIO: 0,1")) { ret_val = 1; // OK and 1 as HIGH } else if(IsStringReceived("#GPIO: 0,0")) { ret_val = 0; // OK and 0 as LOW } else ret_val = -3; // else response ERROR } SetCommLineStatus(CLS_FREE); return (ret_val); }
//------------------------------------------------------------------------------ UBX_PDP_STATUS ubxGetPdpServiceStatus(void) { UBX_PDP_STATUS ret_val; // send AT+UPSND=0,8 command SerialWriteRomString("AT+UPSND=0,8\r"); // wait response if (RX_TMOUT_ERR == WaitResp(500, 5)) ret_val = UBX_PDP_S_TIMEOUT; // check response else { if(IsStringReceived("+UPSND: 0,8,1")) ret_val = UBX_PDP_S_ACTIVE; else if(IsStringReceived("+UPSND: 0,8,0")) ret_val = UBX_PDP_S_NOT_ACTIVE; else if(IsStringReceived("ERROR")) ret_val = UBX_PDP_S_ERROR; else ret_val = UBX_PDP_S_UNKNOWN_RESPONSE; } return ret_val; }
int GSM::begin(long baud_rate){ #ifdef UNO if (baud_rate==115200){ Serial.println("Don't use baudrate 115200 with Software Serial.\nAutomatically changed at 9600."); baud_rate=9600; } #endif int response=-1; int cont=0; boolean norep=true; boolean turnedON=false; SetCommLineStatus(CLS_ATCMD); _cell.begin(baud_rate); p_comm_buf = &comm_buf[0]; setStatus(IDLE); // if no-reply we turn to turn on the module for (cont=0; cont<3; cont++){ if (AT_RESP_ERR_NO_RESP == SendATCmdWaitResp("AT", 500, 100, "OK", 5)&&!turnedON) { //check power // there is no response => turn on the module #ifdef DEBUG_ON Serial.println("DB:NO RESP"); #endif // generate turn on pulse digitalWrite(GSM_ON, HIGH); delay(1200); digitalWrite(GSM_ON, LOW); delay(10000); WaitResp(1000, 1000); } else{ #ifdef DEBUG_ON Serial.println("DB:ELSE"); #endif WaitResp(1000, 1000); } } if (AT_RESP_OK == SendATCmdWaitResp("AT", 500, 100, "OK", 5)){ #ifdef DEBUG_ON Serial.println(F("DB:CORRECT BR")); #endif turnedON=true; norep=false; } if (AT_RESP_ERR_DIF_RESP == SendATCmdWaitResp("AT", 500, 100, "OK", 5)&&!turnedON){ //check OK #ifdef DEBUG_ON Serial.println(F("DB:AUTO BAUD RATE")); #endif for (int i=0;i<8;i++){ switch (i) { case 0: _cell.begin(1200); break; case 1: _cell.begin(2400); break; case 2: _cell.begin(4800); break; case 3: _cell.begin(9600); break; case 4: _cell.begin(19200); break; case 5: _cell.begin(38400); break; case 6: _cell.begin(57600); break; case 7: _cell.begin(115200); break; // if nothing else matches, do the default // default is optional } delay(100); #ifdef DEBUG_PRINT // parameter 0 - because module is off so it is not necessary // to send finish AT<CR> here DebugPrint("DEBUG: Stringa ", 0); DebugPrint(buff, 0); #endif if (AT_RESP_OK == SendATCmdWaitResp("AT", 500, 100, "OK", 5)){ #ifdef DEBUG_ON Serial.println(F("DB:FOUND PREV BR")); #endif _cell.print(F("AT+IPR=")); _cell.print(baud_rate); _cell.print("\r"); // send <CR> delay(500); _cell.begin(baud_rate); delay(100); if (AT_RESP_OK == SendATCmdWaitResp("AT", 500, 100, "OK", 5)){ #ifdef DEBUG_ON Serial.println("DB:OK BR"); #endif } turnedON=true; break; } #ifdef DEBUG_ON Serial.println("DB:NO BR"); #endif } // communication line is not used yet = free SetCommLineStatus(CLS_FREE); // pointer is initialized to the first item of comm. buffer p_comm_buf = &comm_buf[0]; } if(norep==true&&!turnedON){ Serial.println(F("Trying to force the baud-rate to 9600\n")); for (int i=0;i<8;i++){ switch (i) { case 0: _cell.begin(1200); delay(1000); Serial.println(F("1200")); _cell.print(F("AT+IPR=9600\r")); delay(1000); _cell.begin(9600); delay(1000); SendATCmdWaitResp("AT", 500, 100, "OK", 5); delay(1000); WaitResp(1000,1000); break; case 1: _cell.begin(2400); delay(1000); Serial.println(F("2400")); _cell.print(F("AT+IPR=9600\r")); delay(1000); _cell.begin(9600); delay(1000); SendATCmdWaitResp("AT", 500, 100, "OK", 5); delay(1000); WaitResp(1000,1000); break; case 2: _cell.begin(4800); delay(1000); Serial.println(F("4800")); _cell.print(F("AT+IPR=9600\r")); delay(1000); _cell.begin(9600); delay(1000); SendATCmdWaitResp("AT", 500, 100, "OK", 5); delay(1000); WaitResp(1000,1000); break; case 3: _cell.begin(9600); delay(1000); Serial.println(F("9600")); _cell.print(F("AT+IPR=9600\r")); delay(1000); _cell.begin(9600); delay(1000); SendATCmdWaitResp("AT", 500, 100, "OK", 5); delay(1000); WaitResp(1000,1000); break; case 4: _cell.begin(19200); delay(1000); Serial.println(F("19200")); _cell.print(F("AT+IPR=9600\r")); delay(1000); _cell.begin(9600); delay(1000); SendATCmdWaitResp("AT", 500, 100, "OK", 5); delay(1000); WaitResp(1000,1000); break; case 5: _cell.begin(38400); delay(1000); Serial.println(F("38400")); _cell.print(F("AT+IPR=9600\r")); delay(1000); _cell.begin(9600); delay(1000); SendATCmdWaitResp("AT", 500, 100, "OK", 5); delay(1000); WaitResp(1000,1000); break; case 6: _cell.begin(57600); delay(1000); Serial.println(F("57600")); _cell.print(F("AT+IPR=9600\r")); delay(1000); _cell.begin(9600); delay(1000); SendATCmdWaitResp("AT", 500, 100, "OK", 5); delay(1000); WaitResp(1000,1000); break; case 7: _cell.begin(115200); delay(1000); Serial.println(F("115200")); _cell.print(F("AT+IPR=9600\r")); delay(1000); _cell.begin(9600); delay(1000); SendATCmdWaitResp("AT", 500, 100, "OK", 5); delay(1000); WaitResp(1000,1000); break; } } Serial.println(F("ERROR: SIM900 doesn't answer. Check power and serial pins in GSM.cpp")); digitalWrite(GSM_ON, HIGH); delay(1200); digitalWrite(GSM_ON, LOW); delay(10000); return 0; } SetCommLineStatus(CLS_FREE); if(turnedON){ WaitResp(50, 50); InitParam(PARAM_SET_0); InitParam(PARAM_SET_1);//configure the module Echo(0); //enable AT echo setStatus(READY); return(1); } else{ //just to try to fix some problems with 115200 baudrate _cell.begin(115200); delay(1000); _cell.print(F("AT+IPR=")); _cell.print(baud_rate); _cell.print("\r"); // send <CR> return(0); } }
boolean SIMCOM900::readSMS(char* msg, int msglength, char* number, int nlength) { Serial.println(F("This method is deprecated! Please use GetSMS in the SMS class.")); long index; char *p_char; char *p_char1; /* if (getStatus()==IDLE) return false; */ #ifdef UNO _tf.setTimeout(_GSM_DATA_TOUT_); #endif //_cell.flush(); WaitResp(500, 500); SimpleWriteln(F("AT+CMGL=\"REC UNREAD\",1")); WaitResp(5000, 500); if(gsm.IsStringReceived("+CMGL")) { //index p_char = strchr((char *)(gsm.comm_buf),'+CMGL'); p_char1 = p_char+3; //we are on the first char of string p_char = p_char1+1; *p_char = 0; index=atoi(p_char1); p_char1 = p_char+1; p_char = strstr((char *)(p_char1), "\",\""); p_char1 = p_char+3; p_char = strstr((char *)(p_char1), "\",\""); if (p_char != NULL) { *p_char = 0; } strcpy(number, (char *)(p_char1)); ////// p_char1 = p_char+3; p_char = strstr((char *)(p_char1), "\",\""); p_char1 = p_char+3; p_char = strstr((char *)(p_char1), "\n"); p_char1 = p_char+1; p_char = strstr((char *)(p_char1), "\n"); if (p_char != NULL) { *p_char = 0; } strcpy(msg, (char *)(p_char1)); // #ifdef UNO // index=_tf.getValue(); // #endif // #ifdef MEGA //index=_cell.read(); // #endif // Serial.println("DEBUG"); // #ifdef UNO // _tf.getString("\",\"", "\"", number, nlength); // #endif // Serial.println("PRIMA"); // #ifdef MEGA // _cell.getString("\",\"", "\"", number, nlength); // #endif // Serial.println("DEBUG"); // #ifdef UNO // _tf.getString("\n", "\nOK", msg, msglength); // #endif // #ifdef MEGA // _cell.getString("\n", "\nOK", msg, msglength); // #endif SimpleWrite(F("AT+CMGD=")); SimpleWriteln(index); // Serial.print("VAL= "); // Serial.println(index); gsm.WaitResp(5000, 50, "OK"); return true; }; return false; };
/********************************************************** Method update status of battery charging return: BATT_NOT_CHARGING - no charger connected BATT_CHARGING - battery is charging BATT_FULL - battery is full OK ret val: ----------- 1 - Battery status updated 0 - Battery status not updated ERROR ret. val: --------------- -1 - comm. line to the GSM module is not free -2 - GSM module didn't answer in timeout **********************************************************/ char GSM::CheckBattery() { char ret_val = -1; char *p_char; char *p_char1; char *p_char2; if (CLS_FREE != GetCommLineStatus()) return (ret_val); SetCommLineStatus(CLS_ATCMD); ret_val = 0; // not found yet Serial.print(F("AT+CBC\r")); switch (WaitResp(1000, 20, "+CBC")) { case RX_TMOUT_ERR: // response was not received in specific time ret_val = -2; break; case RX_FINISHED_STR_RECV: if(IsStringReceived("+CBC: 0")){ SetBattChargeStatus(BATT_NOT_CHARGING); } else if(IsStringReceived("+CBC: 1")){ SetBattChargeStatus(BATT_CHARGING); } else if(IsStringReceived("+CBC: 2")){ SetBattChargeStatus(BATT_FULL); } p_char = strchr((char *)(comm_buf),','); if (p_char != NULL) { p_char++; // we are on the first battery level character // find out ',' as finish character of battery level string p_char1 = strchr((char *)(p_char),','); if (p_char1 != NULL) { *p_char1 = 0; // end of string batteryLevel= atoi(p_char); //Convert string to integer 0-100% p_char1++;//Move on to first voltage number p_char2 = strchr((char *)(p_char1),'\r');//find the end of the line if (p_char != NULL) { p_char2 = 0; batteryVoltage= atoi(p_char1);//Convert bat voltage to integer ret_val = 1; } } } break; case RX_FINISHED_STR_NOT_RECV: // only OK or ERROR => no phone number ret_val = 0; break; } SetCommLineStatus(CLS_FREE); return (ret_val); }