Ejemplo n.º 1
0
/**********************************************************
Method initializes GPRS

apn:      APN string
login:    user id string
password: password string

return: 
        ERROR ret. val:
        ---------------
        -1 - comm. line is not free


        OK ret val:
        -----------
        0 - GPRS was not initialized
        1 - GPRS was initialized


an example of usage:
        APN si called internet
        user id and password are not used

        GSM gsm;
        gsm.InitGPRS("internet", "", ""); 
**********************************************************/
char GSM::InitGPRS(char* apn, char* login, char* password)
{
  char ret_val = -1;
  char cmd[100];

  if (CLS_FREE != GetCommLineStatus()) return (ret_val);
  SetCommLineStatus(CLS_ATCMD);
  // prepare command:  AT+CGDCONT=1,"IP","apn"
  strcpy(cmd, "AT+CGDCONT=1,\"IP\",\"");
  strcat(cmd, apn);
  strcat(cmd, "\""); // add character "
  ret_val = SendATCmdWaitResp(cmd, 1000, 100, "OK", 2);
  if (ret_val == AT_RESP_OK) {
    // prepare command:  AT#USERID="login"
    strcpy(cmd, "AT#USERID=\"");
    strcat(cmd, login);
    strcat(cmd, "\""); // add character "
    ret_val = SendATCmdWaitResp(cmd, 1000, 100, "OK", 2);
    if (ret_val == AT_RESP_OK) {
      // prepare command:  AT#PASSW="password"
      strcpy(cmd, "AT#PASSW=\"");
      strcat(cmd, password);
      strcat(cmd, "\""); // add character "
      ret_val = SendATCmdWaitResp(cmd, 1000, 100, "OK", 2);
      if (ret_val == AT_RESP_OK) ret_val = 1;
      else ret_val = 0;
    }
    else ret_val = 0;
  }
  else ret_val = 0;

  SetCommLineStatus(CLS_FREE);
  return (ret_val);
}
Ejemplo n.º 2
0
/**********************************************************
  Sends parameters for initialization of GSM module

  group:  0 - parameters of group 0 - not necessary to be registered in the GSM
          1 - parameters of group 1 - it is necessary to be registered
**********************************************************/
void GSM::InitParam(byte group)
{

  switch (group) {
    case PARAM_SET_0:
      // check comm line
      if (CLS_FREE != GetCommLineStatus()) return;
      SetCommLineStatus(CLS_ATCMD);

      // Reset to the factory settings
      SendATCmdWaitResp("AT&F0", 1000, 20, "OK", 5);      
      // switch off echo
      SendATCmdWaitResp("ATE0", 500, 20, "OK", 5);
      // setup auto baud rate
      SendATCmdWaitResp("AT+IPR=0", 500, 20, "OK", 5);
      SetCommLineStatus(CLS_FREE);
      break;

    case PARAM_SET_1:
      // check comm line
      if (CLS_FREE != GetCommLineStatus()) return;
      SetCommLineStatus(CLS_ATCMD);

      // set the SMS mode to text 
      SendATCmdWaitResp("AT+CMGF=1", 500, 20, "OK", 5);
      // init SMS storage
      InitSMSMemory();
      // select phonebook memory storage
      SendATCmdWaitResp("AT+CPBS=\"SM\"", 1000, 20, "OK", 5);
      break;
  }
  
}
Ejemplo n.º 3
0
/**********************************************************
Turns on/off the speaker

off_on: 0 - off
        1 - on
**********************************************************/
void GSM::SetSpeaker(byte off_on)
{
  if (CLS_FREE != GetCommLineStatus()) return;
  SetCommLineStatus(CLS_ATCMD);
  if (off_on) {
    SendATCmdWaitResp("AT#GPIO=5,1,2", 500, 20, "#GPIO:", 1);
  }
  else {
    SendATCmdWaitResp("AT#GPIO=5,0,2", 500, 20, "#GPIO:", 1);
  }
  SetCommLineStatus(CLS_FREE);
}
Ejemplo n.º 4
0
/**********************************************************
  Checks if the GSM module is responding 
  to the AT command
  - if YES  nothing is made 
  - if NO   switch on sequence is repeated until there is a response
				from GSM module
**********************************************************/
void GSM::TurnOn(void)
{
  SetCommLineStatus(CLS_ATCMD);

  while (AT_RESP_ERR_NO_RESP == SendATCmdWaitResp("AT", 500, 20, "OK", 5)) {
    // there is no response => turn on the module
  
#ifdef DEBUG_PRINT
    // parameter 0 - because module is off so it is not necessary 
    // to send finish AT<CR> here
    DebugPrint("DEBUG: GSM module is off\r\n", 0);
#endif
    
    // generate switch on pulse
    digitalWrite(GSM_ON, HIGH);
    delay(1200);
    digitalWrite(GSM_ON, LOW);
    delay(1200);

    delay(1500); // wait before next try
  }
  SetCommLineStatus(CLS_FREE);

  // send collection of first initialization parameters for the GSM module    
  InitParam(PARAM_SET_0);
}
Ejemplo n.º 5
0
/**********************************************************
Turns off the user LED
**********************************************************/
void GSM::TurnOffLED(void)
{
  if (CLS_FREE != GetCommLineStatus()) return;
  SetCommLineStatus(CLS_ATCMD);
  // response here is not important
  SendATCmdWaitResp("AT#GPIO=8,0,1", 500, 20, "#GPIO:", 1);
  SetCommLineStatus(CLS_FREE);
}
Ejemplo n.º 6
0
/**********************************************************
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);
}
Ejemplo n.º 7
0
/**********************************************************
Method enables GPRS context

open_mode:
        0 (= CHECK_AND_OPEN) - checks the current state of context
                               and in case context has been already activated
                               nothing else in made

        1 (= CLOSE_AND_REOPEN) - context is deactivated anyway and then activated again
                               it was found during testing, that you may need to reset the module etc.,
                               and in these cases, you may not be able to activate the GPRS context
                               unless you deactivate it first

return:
        ERROR ret. val:
        ---------------
        -1 - comm. line is not free

        OK ret val:
        -----------
        0 - GPRS context was disabled
        1 - GPRS context was enabled


an example of usage:

        GSM gsm;
        if (gsm.EnableGPRS(CHECK_AND_OPEN) == 1) {
          // GPRS context was enabled, so we have IP address
          // and we can communicate if necessary
        }
**********************************************************/
char GSM::EnableGPRS(byte open_mode)
{
    char ret_val = -1;

    if (CLS_FREE != GetCommLineStatus()) return (ret_val);
    SetCommLineStatus(CLS_ATCMD);

    if (open_mode == CHECK_AND_OPEN) {
        // first try if the GPRS context has not been already initialized
        ret_val = SendATCmdWaitResp("AT+CIPSTATUS", 1000, 1000, "STATE: IP GPRSACT", 2);
        if (ret_val != AT_RESP_OK) {
            // context is not initialized => init the context
            //Enable GPRS
            ret_val = SendATCmdWaitResp("AT+CSTT", 1000, 1000, "OK", 1);
            if (ret_val == AT_RESP_OK) {
                // cstt OK
                ret_val = SendATCmdWaitResp("AT+CIICR", 60000, 1000, "OK", 1);
                if (ret_val == AT_RESP_OK) {
                    // context was activated
                    SendATCmdWaitResp("AT+CIFSR", 2000, 1000, "", 1);//get ip
                    ret_val = 1;
                }
                else ret_val = 0; // not activated
            }
            else ret_val = 0; // not activated
        }
        else ret_val = 1; // context has been already activated
    }
    else {
        // CLOSE_AND_REOPEN mode
        //disable GPRS context
        ret_val = SendATCmdWaitResp("AT+CIPSHUT", 2000, 1000, "SHUT OK", 3);
        if (ret_val == AT_RESP_OK) {
            // context is dactivated
            // => activate GPRS context again
            ret_val = SendATCmdWaitResp("AT+CSTT", 1000, 1000, "OK", 1);
            if (ret_val == AT_RESP_OK) {
                // cstt OK
                ret_val = SendATCmdWaitResp("AT+CIICR", 10000, 1000, "OK", 1);
                if (ret_val == AT_RESP_OK) {
                    // context was activated
                    SendATCmdWaitResp("AT+CIFSR", 2000, 1000, "", 1);//get ip
                    ret_val = 1;
                }
                else ret_val = 0; // not activated
            }
            else ret_val = 0; // not activated
        }
        else ret_val = 0; // not activated
    }

    SetCommLineStatus(CLS_FREE);
    return (ret_val);
}
Ejemplo n.º 8
0
bool GSM::isResponse()
{
	if (AT_RESP_OK == SendATCmdWaitResp("AT", 500, 100, "OK", 3))
		return true;
	else
	{
		DEBUG(F("Modem not answered OK"));
		return false;
	}
}
Ejemplo n.º 9
0
/**********************************************************
Method enables GPRS context

open_mode: 
        0 (= CHECK_AND_OPEN) - checks the current state of context
                               and in case context has been already activated
                               nothing else in made 

        1 (= CLOSE_AND_REOPEN) - context is deactivated anyway and then activated again
                               it was found during testing, that you may need to reset the module etc., 
                               and in these cases, you may not be able to activate the GPRS context 
                               unless you deactivate it first

return: 
        ERROR ret. val:
        ---------------
        -1 - comm. line is not free

        OK ret val:
        -----------
        0 - GPRS context was disabled
        1 - GPRS context was enabled


an example of usage:

        GSM gsm;
        if (gsm.EnableGPRS(CHECK_AND_OPEN) == 1) {
          // GPRS context was enabled, so we have IP address
          // and we can communicate if necessary
        }
**********************************************************/
char GSM::EnableGPRS(byte open_mode)
{
  char ret_val = -1;

  if (CLS_FREE != GetCommLineStatus()) return (ret_val);
  SetCommLineStatus(CLS_ATCMD);

  if (open_mode == CHECK_AND_OPEN) {
    // first try if the GPRS context has not been already initialized
    ret_val = SendATCmdWaitResp("AT#GPRS?", 1000, 100, "#GPRS: 0", 2);
    if (ret_val == AT_RESP_OK) {
      // context is not initialized => init the context
      //Enable GPRS
      ret_val = SendATCmdWaitResp("AT#GPRS=1", 10000, 1000, "OK", 1);
      if (ret_val == AT_RESP_OK) {
        // context was activated
        ret_val = 1;
      }
      else ret_val = 0; // not activated
    }
    else ret_val = 1; // context has been already activated
  }
  else {
    // CLOSE_AND_REOPEN mode
    //disable GPRS context
    ret_val = SendATCmdWaitResp("AT#GPRS=0", 10000, 1000, "OK", 3);
    if (ret_val == AT_RESP_OK) {
      // context is dactivated
      // => activate GPRS context again
      ret_val = SendATCmdWaitResp("AT#GPRS=1", 10000, 1000, "OK", 1);
      if (ret_val == AT_RESP_OK) {
        // context was activated
        ret_val = 1;
      }
      else ret_val = 0; // not activated
    }
    else ret_val = 0; // not activated
  }

  SetCommLineStatus(CLS_FREE);
  return (ret_val);
}
Ejemplo n.º 10
0
char GSM::InitSMSMemory(void)
{
     char ret_val = -1;

     if (CLS_FREE != GetCommLineStatus()) return (ret_val);
     SetCommLineStatus(CLS_ATCMD);
     ret_val = 0; // not initialized yet

     // Disable messages about new SMS from the GSM module
     SendATCmdWaitResp(F("AT+CNMI=2,0"), 1000, 50, str_ok, 2);

     // send AT command to init memory for SMS in the SIM card
     // response:
     // +CPMS: <usedr>,<totalr>,<usedw>,<totalw>,<useds>,<totals>
     if (AT_RESP_OK == SendATCmdWaitResp(F("AT+CPMS=\"SM\",\"SM\",\"SM\""), 1000, 1000, "+CPMS:", 10)) {
          ret_val = 1;
     } else ret_val = 0;

     SetCommLineStatus(CLS_FREE);
     return (ret_val);
}
Ejemplo n.º 11
0
/**********************************************************
Method returns state of user button


return: 0 - not pushed = released
        1 - pushed
**********************************************************/
byte GSM::IsUserButtonPushed(void)
{
  byte ret_val = 0;
  if (CLS_FREE != GetCommLineStatus()) return(0);
  SetCommLineStatus(CLS_ATCMD);
  if (AT_RESP_OK == SendATCmdWaitResp("AT#GPIO=9,2", 500, 20, "#GPIO: 0,0", 1)) {
    // user button is pushed
    ret_val = 1;
  }
  else ret_val = 0;
  SetCommLineStatus(CLS_FREE);
  return (ret_val);
}
Ejemplo n.º 12
0
/**********************************************************
Method initializes GPRS

apn:      APN string
login:    user id string
password: password string

return:
        ERROR ret. val:
        ---------------
        -1 - comm. line is not free


        OK ret val:
        -----------
        0 - GPRS was not initialized
        1 - GPRS was initialized


an example of usage:
        APN is called internet
        user id and password are not used

        GSM gsm;
        gsm.InitGPRS("internet", "", "");
**********************************************************/
char GSM::InitGPRS(char* apn, char* login, char* password)
{
    char ret_val = -1;
    char cmd[150];

    if (CLS_FREE != GetCommLineStatus()) return (ret_val);
    SetCommLineStatus(CLS_ATCMD);
    ret_val = SendATCmdWaitResp("AT+CIPSHUT", 2000, 1000, "SHUT OK", 3);
    if (ret_val == AT_RESP_OK) {
        //Set Single IP Connection
        ret_val = SendATCmdWaitResp("AT+CIPMUX=0", 1000, 1000, "OK", 3);
        if (ret_val == AT_RESP_OK) {
            // Set transparent mode
            ret_val = SendATCmdWaitResp("AT+CIPMODE=1", 1000, 2000, "OK", 3);
            if (ret_val == AT_RESP_OK) {
                //prepare AT+CSTT command: AT+CSTT="apn","user","pass"
                strcpy(cmd, "AT+CSTT=\"");
                strcat(cmd, apn);
                strcat(cmd, "\",\""); // add character " and , and "
                strcat(cmd, login);
                strcat(cmd, "\",\""); // add character " and , and "
                strcat(cmd, password);
                strcat(cmd, "\""); // add character "
                ret_val = SendATCmdWaitResp(cmd, 1000, 2000, "OK", 5);
                if (ret_val == AT_RESP_OK) ret_val = 1;
                else ret_val = 0;
            }
            else ret_val = 0;
        }
        else ret_val = 0;
    }
    else ret_val = 0;

    SetCommLineStatus(CLS_FREE);
    return (ret_val);
}
Ejemplo n.º 13
0
/**********************************************************
Method disables GPRS context

return:
        ERROR ret. val:
        ---------------
        -1 - comm. line is not free

        OK ret val:
        -----------
        0 - GPRS context was not disabled
        1 - GPRS context was disabled


an example of usage:

        GSM gsm;
        gsm.DisableGPRS();
**********************************************************/
char GSM::DisableGPRS(void)
{
    char ret_val = -1;

    if (CLS_FREE != GetCommLineStatus()) return (ret_val);
    SetCommLineStatus(CLS_ATCMD);
    ret_val = SendATCmdWaitResp("AT+CIPSHUT", 2000, 1000, "SHUT OK", 2);
    if (ret_val == AT_RESP_OK) {
        // context was disabled
        ret_val = 1;
    }
    else ret_val = 0; // context was not disabled

    SetCommLineStatus(CLS_FREE);
    return (ret_val);
}
Ejemplo n.º 14
0
/**********************************************************
Method opens the socket

<socket type> - socket protocol type
                0 - TCP
                1 - UDP
<remote port> - remote host port to be opened
                0..65535 - port number
<remote addr> - address of the remote host, string type.
              This parameter can be either:
              - any valid IP address in the format: xxx.xxx.xxx.xxx
              - any host name to be solved with a DNS query in the format: <host
              name>

return:
        ERROR ret. val:
        ---------------
        -1 - comm. line is not free

        OK ret val:
        -----------
        0 - socket was not opened
        1 - socket was successfully opened


an example of usage:

        GSM gsm;
        gsm.OpenSocket(TCP, 80, "www.google.com");
**********************************************************/
char GSM::OpenSocket(byte socket_type, uint16_t remote_port, char* remote_addr)
{
    char ret_val = -1;
    char cmd[200];
    char tmp_str[10];

    if (CLS_FREE != GetCommLineStatus()) return (ret_val);
    SetCommLineStatus(CLS_ATCMD);
    // prepare command:  AT+CIPSTART="TCP","www.google.com","port"
    strcpy(cmd, "AT+CIPSTART=\"");
    // add socket type
    if (socket_type == UDP_SOCKET)
    {
        strcat(cmd, "UDP\"");
    }
    else
    {
        strcat(cmd, "TCP\",\"");
    }
    // add remote addr
    strcat(cmd, remote_addr);
    strcat(cmd, "\",\""); // add characters ,"
    // add remote_port
    strcat(cmd, itoa(remote_port, tmp_str, 10));
    strcat(cmd, "\""); // add characters "

    // send AT command and waits for the response "CONNECT\r\n" - max. 3 times
    ret_val = SendATCmdWaitResp(cmd, 20000, 3000, "CONNECT\r\n", 3);
    if (ret_val == AT_RESP_OK) {
        ret_val = 1;
        SetCommLineStatus(CLS_DATA);
    }
    else {
        ret_val = 0;
        SetCommLineStatus(CLS_FREE);
    }

    return (ret_val);
}
Ejemplo n.º 15
0
/**********************************************************
Method opens the socket

<socket type> - socket protocol type
                0 - TCP
                1 - UDP
<remote port> - remote host port to be opened
                0..65535 - port number
<remote addr> - address of the remote host, string type. 
              This parameter can be either:
              - any valid IP address in the format: xxx.xxx.xxx.xxx
              - any host name to be solved with a DNS query in the format: <host
              name>
<closure type> - socket closure behaviour for TCP
              0 - local host closes immediately when remote host has closed (default)
              255 - local host closes after an escape sequence (+++) or after an abortive
                    disconnect from remote.
<local port> - local host port to be used on UDP socket

return: 
        ERROR ret. val:
        ---------------
        -1 - comm. line is not free

        OK ret val:
        -----------
        0 - socket was not opened
        1 - socket was successfully opened


an example of usage:

        GSM gsm;
        gsm.OpenSocket(TCP, 80, "www.google.com", 0, 0); 
**********************************************************/
char GSM::OpenSocket(byte socket_type, uint16_t remote_port, char* remote_addr,
                     byte closure_type, uint16_t local_port)
{
  char ret_val = -1;
  char cmd[100];
  char tmp_str[10];

  if (CLS_FREE != GetCommLineStatus()) return (ret_val);
  SetCommLineStatus(CLS_ATCMD);
  // prepare command:  AT+CGDCONT=1,"IP","apn"
  strcpy(cmd, "AT#SKTD=");
  // add socket type
  strcat(cmd, itoa(socket_type, tmp_str, 10));
  strcat(cmd, ","); // add character ,
  // add remote_port
  strcat(cmd, itoa(remote_port, tmp_str, 10));
  strcat(cmd, ",\""); // add characters ,"
  // add remote addr
  strcat(cmd, remote_addr);
  strcat(cmd, "\","); // add characters ",
  // add closure type
  strcat(cmd, itoa(closure_type, tmp_str, 10));
  strcat(cmd, ","); // add character ,
  // add local port
  strcat(cmd, itoa(local_port, tmp_str, 10));

  // send AT command and waits for the response "CONNECT" - max. 3 times
  ret_val = SendATCmdWaitResp(cmd, 20000, 100, "CONNECT", 3);
  if (ret_val == AT_RESP_OK) {
    ret_val = 1;
    SetCommLineStatus(CLS_DATA);
  }
  else {
    ret_val = 0;
    SetCommLineStatus(CLS_FREE);
  }
  
  return (ret_val);
}
Ejemplo n.º 16
0
/**********************************************************
Method gets temperature measured by the Telit GSM module

the temperature is calculated by the formula:
T[C] = (Vin-600)/10
where Vin is the millivolts value

return: 
        ERROR ret. val:
        ---------------
        -1000 - comm. line to the GSM module is not free
        -2000 - GSM temperature answer is not valid

        OK ret val:
        -----------
        temperature in the C in the format XXX ~ XX,XC
        so e.g. 235 means   23.5 C
                -100 means  -10.0 C
**********************************************************/
int GSM::GetTemp(void)
{
  int ret_val = -1000;
  char *pos;

  if (CLS_FREE != GetCommLineStatus()) return(ret_val);
  SetCommLineStatus(CLS_ATCMD);
  ret_val = -2000; // we do not have right value yet

  // response is in the format: #ADC: 885
  if (AT_RESP_OK == SendATCmdWaitResp("AT#ADC=2,2,0", 500, 20, "#ADC", 1)) {
    // parse the received string
    pos = strchr((char *)comm_buf, ' ');
    if (pos != NULL) {
      // we are in front of the number
      ret_val = atoi(pos);
      ret_val = ret_val - 600;
    }
  }
 
  SetCommLineStatus(CLS_FREE);
  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);
	}
}
void GSM::InitParam(byte group){
	switch (group) {
	case PARAM_SET_0:
		// check comm line
		//if (CLS_FREE != GetCommLineStatus()) return;

		SetCommLineStatus(CLS_ATCMD);
		// Reset to the factory settings
		SendATCmdWaitResp("AT&F", 1000, 50, "OK", 5);      
		// switch off echo
		SendATCmdWaitResp("ATE0", 500, 50, "OK", 5);
		// setup fixed baud rate
		//SendATCmdWaitResp("AT+IPR=9600", 500, 50, "OK", 5);
		// setup mode
		//SendATCmdWaitResp("AT#SELINT=1", 500, 50, "OK", 5);
		// Switch ON User LED - just as signalization we are here
		//SendATCmdWaitResp("AT#GPIO=8,1,1", 500, 50, "OK", 5);
		// Sets GPIO9 as an input = user button
		//SendATCmdWaitResp("AT#GPIO=9,0,0", 500, 50, "OK", 5);
		// allow audio amplifier control
		//SendATCmdWaitResp("AT#GPIO=5,0,2", 500, 50, "OK", 5);
		// Switch OFF User LED- just as signalization we are finished
		//SendATCmdWaitResp("AT#GPIO=8,0,1", 500, 50, "OK", 5);
		SetCommLineStatus(CLS_FREE);
		break;

	case PARAM_SET_1:
		// check comm line
		//if (CLS_FREE != GetCommLineStatus()) return;
		SetCommLineStatus(CLS_ATCMD);
		// Request calling line identification
		SendATCmdWaitResp(F("AT+CLIP=1"), 500, 50, "OK", 5);
		// Mobile Equipment Error Code
		SendATCmdWaitResp(F("AT+CMEE=0"), 500, 50, "OK", 5);
		// Echo canceller enabled 
		//SendATCmdWaitResp("AT#SHFEC=1", 500, 50, "OK", 5);
		// Ringer tone select (0 to 32)
		//SendATCmdWaitResp("AT#SRS=26,0", 500, 50, "OK", 5);
		// Microphone gain (0 to 7) - response here sometimes takes 
		// more than 500msec. so 1000msec. is more safety
		//SendATCmdWaitResp("AT#HFMICG=7", 1000, 50, "OK", 5);
		// set the SMS mode to text 
		SendATCmdWaitResp(F("AT+CMGF=1"), 500, 50, "OK", 5);
		// Auto answer after first ring enabled
		// auto answer is not used
		//SendATCmdWaitResp("ATS0=1", 500, 50, "OK", 5);
		// select ringer path to handsfree
		//SendATCmdWaitResp("AT#SRP=1", 500, 50, "OK", 5);
		// select ringer sound level
		//SendATCmdWaitResp("AT+CRSL=2", 500, 50, "OK", 5);
		// we must release comm line because SetSpeakerVolume()
		// checks comm line if it is free
		SetCommLineStatus(CLS_FREE);
		// select speaker volume (0 to 14)
		//SetSpeakerVolume(9);
		// init SMS storage
		InitSMSMemory();
		// select phonebook memory storage
		SendATCmdWaitResp(F("AT+CPBS=\"SM\""), 1000, 50, "OK", 5);
		SendATCmdWaitResp(F("AT+CIPSHUT"), 500, 50, "SHUT OK", 5);
		break;
	}
}