Random behavior after 24h GSM Modul

Hey guys,

I have a problem with my program/code, which makes weird things after 24h. The program is a pump control which gets an message and then sets tasks to do.
The Problem is, after about 25-27hours suddenly an if-statement is true and sends out "Okay SniffenOn" and seconds later "Okay SniffenOff". It seems it doesn't delete the string "smsin", but why? Or could it a different problem?

Here the main part:

do
        {
          gprs.sendSMS(Brunnen2Num,"Pumpe1freigabe");
          //Warte 40Sekunden
          delay(40000); 
          //SMS Empfangen
          ReceiveSMS(smsin);
          
            if((strcmp("Kabelein", smsin)) == 0) 
            { 
              if(ing)
                gprs.sendSMS(Testingenieur,"Kabel EIN");
                
              Serial.println("Kabel EIN");
              Kabel = 1;
              deletesms(smsin);
              break;
            }
            //Wenn "SniffenOn" von Testingenieur empfangen wird, wird "ing" auf 1 gesetzt und
            //der gesamte SMS-Verkehr an Testingenieur übertragen, Standardmäßig ist ing = 0.
            if((strcmp("SniffenOn", smsin)) == 0) 
            {
              gprs.sendSMS(Testingenieur,"Okay SniffenOn");
              ing = 1;  
              Serial.println("Sniffen EIN");
              deletesms(smsin);
            }
            if((strcmp("SniffenOff", smsin)) == 0) 
            {
              gprs.sendSMS(Testingenieur,"Okay SniffenOff");
              ing = 0;  
              Serial.println("Sniffen AUS");
              deletesms(smsin);
            }
            if((strcmp("OkayFreigabe", smsin)) == 0)
            {
              Serial.println("OkayFreigabe");
              if(ing)
                gprs.sendSMS(Testingenieur,"OkayFreigabe");

              deletesms(smsin);
              break;
            }
            wiederholen_trockenlaufaus++;
            
            if(wiederholen_trockenlaufaus == 30)
            {
              //Sende SMS an Handy's, x1
              gprs.sendSMS(Benutzer1Num,"Pumpstation Empfangsstoerung");
              gprs.sendSMS(Benutzer2Num,"Pumpstation Empfangsstoerung");
              gprs.sendSMS(Benutzer3Num,"Pumpstation Empfangsstoerung");
              if(ing)
                gprs.sendSMS(Testingenieur,"Pumpstation Empfangsstoerung");
            }
        }while(wiederholen_trockenlaufaus <= 30); //30 Versuche um SMS zu senden und eine Antwort zu erhalten!

With the subprogram deletesms:

void deletesms(char smstodelete[20])
{
  for(int t=0;t<=19;t++)
    {
      //strcpy(sms_buffer[s][t],"0"); //Setze Inhalt von sms_buffer auf 0
      smstodelete[t] = 0;
    }
}

And ReceiveSMS

void ReceiveSMS(char sms_buffer[20])
{
  int messageIndex = 0;
  int message_length = 50; //Gibt an wieviele Zeichen in sms_buffer der Funktion gprs.readSMS gespeichert werden sollen
  char phone[16];
  char datetime[24];
  char orig_sms[20];
  
  messageIndex = gprs.isSMSunread();
  
  if(messageIndex == -1)
  {
    strcpy(sms_buffer, "Error");
    Serial.print("Error in receiving SMS = Code: -1");
  }
  
  if(messageIndex == 0) //keine SMS vorhanden - hier soll nichts passieren
  {
    strcpy(sms_buffer, "Error");
  }
  
  if(messageIndex > 0)
  {
    gprs.readSMS(messageIndex, orig_sms, message_length, phone, datetime);
    gprs.deleteSMS(messageIndex);
    
    //SMS zurückliefern, wenn gültige Nummer
    if( ((strcmp(phone, Brunnen2Num)) == 0) || ((strcmp(phone, Testingenieur)) == 0) )
    {
      strcpy(sms_buffer, orig_sms); 
    }
    else
    {
      strcpy(sms_buffer, "Error");
      Serial.print("Error: Number is not valid"); 
    }
  }
}

Would be happy for every answer! Best regards

Post complete code! In most cases the error is in the code people are hiding from us.

One problem might be here:

  int message_length = 50; //Gibt an wieviele Zeichen in sms_buffer der Funktion gprs.readSMS gespeichert werden sollen
..
  char orig_sms[20];
..
    gprs.readSMS(messageIndex, orig_sms, message_length, phone, datetime);

You're telling readSMS() that it might store up to 50 bytes into orig_sms but that array has a size of 20 bytes. Guess what happens if the read message is bigger than the 20 bytes.

Hey, thank you for your answer!

Oh yeah, that could be an error in future, when i want to receive more than 20bytes. Actually my received messages are less than 20 bytes, but thanks for that hint!

I post the complete code - it's a lot, but maybe the error is there.

I also have another idea where the error could come from: In my sub-program "void deletesms(...)" i get smstodelete[20] and delete the message. In the main program i take again that string, which is called smsin -> delete(smsin) and continue working with smsin. Is it a problem that i have in the subprogram void instead of char?

The full code is attached :slight_smile:

pump_control.ino (26 KB)

Oh yeah, that could be an error in future, when i want to receive more than 20bytes. Actually my received messages are less than 20 bytes, but thanks for that hint!

That's an error in the code. Did you check that you never received more than 20 bytes from the module? There might have been a problem and it sent an error message.

The code has comments that talk about a Mega2560 being used. Why didn't you tell us that? What other relevant information are you hiding from us?

I also have another idea where the error could come from: In my sub-program "void deletesms(...)" i get smstodelete[20] and delete the message. In the main program i take again that string, which is called smsin -> delete(smsin) and continue working with smsin. Is it a problem that i have in the subprogram void instead of char?

You don't delete the message you just set all bytes to 0, so the content of the SMS isn't available anymore byte smsin is still a pointer to a char array.
I don't understand the sentence about void and char. The subroutine expects a char[20] and gets such a variable as the parameter. The only void I can find is the return value. As you don't return anything that's no problem.

Post the serial output you get if such a misbehavior happens.

Hey thank you for your answer!

Big sorry about that, i forgot it totally!
I have a Mega 2560 Board with a GSM Modul Seeedstudio v3 and Arduino Relais Board. The inputs come from a relay with 0-5V input to the mega pin. The output works with the relay shield. My power supply has about 3A and is sufficiently dimensioned.

Could be another possible error the serial.print without anything connected to the arduino. The Mega is installed in a blackbox with no serial communication. It only works with gsm and the input/output pins.
Therefore i have sadly no possibilities to track the serial output.

I have a Mega 2560 Board with a GSM Modul Seeedstudio v3 and Arduino Relais Board.

Where are the links to this hardware?

My power supply has about 3A and is sufficiently dimensioned.

If you connected that to the Mega it's voltage regulator might be the bottleneck.

Could be another possible error the serial.print without anything connected to the arduino.

No. The Arduino don't care if a device is connected to the serial port.

The Mega is installed in a blackbox with no serial communication. It only works with gsm and the input/output pins.
Therefore i have sadly no possibilities to track the serial output.

How about removing it from there during the debugging? Without any hint on what's going on, locating the source of your problem is more than tricky.