ostaquet / Arduino-SIM800L-driver

Arduino driver for GSM/GPRS module SIMCom SIM800L to make HTTP/S connections with GET and POST methods
MIT License
133 stars 61 forks source link

Problem with reading SMS #42

Closed DavidRochaHenrique closed 3 years ago

DavidRochaHenrique commented 3 years ago

I am using the example "HTTPS_GET_SoftSerial" to read data from a server and it works fine. But when I try to read the SMS message, sending the following configuration beforehand, the SMS bytes come in with problems.

void configGSM() {
   Sim800l.print("AT+CMGF=1\n;AT+CNMI=2,2,0,0,0\n;ATX4\n;AT+COLP=1\n"); 
}

If I use only the SMS reading code, it will also work well.

The curious thing is that the HTTPS routine does not run in my code. I put the following line:

if (false) {uint16_t rc = sim800l-> doGet (URL, 10000); }

That is, the interference between functions occurs only because the code is compiled.

void ReadGSM()
{
  static String textoRec = "";
  static unsigned long delay1 = 0;
  static int count=0;  
  static unsigned char buffer[200];

delay(10);
  if (Sim800l.available()) {            

     while(Sim800l.available()) {         

        buffer[count++] = Sim800l.read();     
        if(count == 200)break;
     }

     textoRec += (char*)buffer;
     delay1   = millis();

     for (int i=0; i<count; i++) {
         buffer[i]=NULL;
     } 
     count = 0;                       
  }

  if ( ((millis() - delay1) > 100) && textoRec != "" ) {
    Serial.print("GSMRead = ");Serial.println(textoRec);

     if ( textoRec.substring(2,7) == "+CMT:" ) {SMS_Received = true; }

     if (SMS_Received) 
     {

        telefoneSMS = "";
        dataHoraSMS = "";
        mensagemSMS = "";

        byte linha = 0;  
        byte aspas = 0;
        for (int nL=1; nL < textoRec.length(); nL++) 
        {

            if (textoRec.charAt(nL) == '"') {
               aspas++;
               continue;
            }                        

            if ( (linha == 1) && (aspas == 1) ) {
               telefoneSMS += textoRec.charAt(nL);
            }

            if ( (linha == 1) && (aspas == 5) ) {
               dataHoraSMS += textoRec.charAt(nL);
            }

            if ( linha == 2 ) {
               mensagemSMS += textoRec.charAt(nL);
            }

            if (textoRec.substring(nL - 1, nL + 1) == "\r\n") {
               linha++;
            }
        }
     } 

     else 
     {
       comandoGSM = textoRec;
       comandoGSM.trim();
       Serial.print("comandoGSM = ");Serial.println(comandoGSM);
       if ( comandoGSM == "RING"  )       {Call_Received = true; Call_Finished = false; }
       if ( comandoGSM == "NO CARRIER"  ) {Call_Finished = true; }

     }

     textoRec = "";  
  }     
}
ostaquet commented 3 years ago

Hi,

I didn't read all the code in details but I have 3 comments.

First, the library is not designed to send and receive SMS :-). This is why the library name is SIM800L HTTP connector (I admit that the repository name is confusing ;-).

Second, you mentioned the HTTPS routine doesn't work with the line:

if (false) {uint16_t rc = sim800l-> doGet (URL, 10000); }

It is normal because the if statement is always false. The snippet between {} is indeed never executed.

Third, regarding the few lines you wrote:

void configGSM() {
   Sim800l.print("AT+CMGF=1\n;AT+CNMI=2,2,0,0,0\n;ATX4\n;AT+COLP=1\n"); 
}

I personally avoid to send all the command at once and let the MCU of the module manage the commands one by one. It is safer in embedded systems ;-)

KR, Olivier