Unreliable SMS message reception on Arduino with GSM modem

I am using Arduino UNO along with GSM modem mini GSM800A for receiving SMS messages. Here's the complete code. It works but I feel it's not that reliable. I am planning to modify the printserial() function to avoid the strstr() function. How can I improve the program for more reliable reception of SMS messages?

#include <AltSoftSerial.h> //Buffer size 256

AltSoftSerial altSerial; // pin 8 & 9  

char comm_buf[256];

bool printSerial(char* response= NULL);
void sentNotification(char* msg);
void SyncOrResetGsmAndTx();
void MySetup();

void setup() 
{
}
void loop()
{
  MySetup();
  while(true){
    MyLoop();
  }
}


void SyncOrResetGsmAndTx(){
  
  while(true){  
     bool result = false;  
     unsigned long start_time = millis();
     while(result == false){
        altSerial.println("AT");
        delay(3000);  
        if(printSerial("OK")){
          
           result = true;
           Serial.println("Synchronization with GSM modem sucessful."); 
           altSerial.println("AT+CMGF=1");
           delay(3000);
           
           // Disable messages about new SMS from the GSM module 
           altSerial.println("AT+CNMI=2,0");
           delay(3000);
           printSerial();
 
           altSerial.println("AT+CPMS=\"SM\",\"SM\",\"SM\"");
           delay(3000);
           printSerial();
           
           return;
        } 
        else Serial.println("Failed to synchronize with GSM modem.");
        if( millis() - start_time> 10000UL) {
          Serial.println("GSM init timeout.");
          break;
        }
     }//while result == false   

     if( result == false){ 
       // time to reboot gsm 
       digitalWrite(11,HIGH); 
       delay(300); // Pull down 200ms to LOW
       digitalWrite(11,LOW);
       delay(10000UL); // wait 10 sec for initialization of GSM
       // it will retry
     }  
  } //while(true)  
}

void MySetup() {  
    
  pinMode(11,OUTPUT); // GSM Reset PIN
  digitalWrite(11,LOW);
  pinMode(13,OUTPUT);
  
  bool result;
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Hardware Serial initialized.");   
  
  altSerial.begin(9600);  
  delay(3000);
  Serial.println("Software Serial initialized."); 
  
 // GetStartSignalFromPC();
 // Serial.println("\nSynced with PC.\n");
 
  SyncOrResetGsmAndTx();
  
  Serial.println("\nAggregator is listening for sms...\n"); 
}

void MyLoop(){
  char msg[256];
    char* p_char;
    char* p_char1;
    static unsigned long start_time = millis();
    
    altSerial.println("AT+CMGL=\"ALL\"");  // find unread sms from memory
    delay(5000);
    if( printSerial("+CMGL:")){
       p_char = strchr(comm_buf,':');
       if (p_char != NULL) {
                    
          p_char1 = strchr(p_char+1, ',');
          *p_char1 = 0;
          char index = atoi(p_char+1);
          
          altSerial.print("AT+CMGR="); //read from memory
          altSerial.println((int)index); 
          delay(5000);
          
          if( printSerial("+CMGR")){
              p_char = strstr(p_char+1, "StnID");
              if (p_char != NULL) {                
                p_char1 = strchr((char *)(p_char), 0x0d); // find <CR> as the end of SMS string
                if (p_char1 != NULL) { 
                  //Serial.println("\n<CR> found\n");
                  *p_char1 = 0;
                  strcpy(msg,p_char); 
                  sentNotification(msg);
                }
                else
                {
                  //Serial.println("\n<CR> not found\n");
                }
              }//if (p_char 
            
            altSerial.print("AT+CMGD="); //delete from memory
            altSerial.println((int)index); 
            delay(5000);      
            printSerial();
            start_time = millis();           
           }  // if("+CMGR"      
           
       }//if (p_char 
    } //if("+CMGL" 
    else
    if( millis() - start_time > 300000UL)
    {
      start_time = millis();
      altSerial.println("AT");
      delay(3000);  
      if(!printSerial("OK")) SyncOrResetGsmAndTx();      
    }
    
} // loop

bool printSerial(char* response)
{
  int i = 0;
  while(altSerial.available()>0 && i<254){
    char c = altSerial.read();
    //Serial.print(c);
    comm_buf[i++] = c;
  }
   comm_buf[i++] = '\0';
   
   if(response != NULL){
      char* p = NULL;
      p = strstr(comm_buf, response);        
      if( p!= NULL) return true;
   }
  return false;
}

 void sentNotification(char* msg)
{ 
  int l = strlen(msg);
  for(int i=0;i< l; i++)
  {
    char ch = msg[i];
    Serial.print(ch);
    delay(10);
  }
  delay(4000);  
}

So that we don't have to have a blind foxhunt in your code, can you explain in what way it is not reliable?

Some SMS messages are dropped and not received.

Do you have some other reference code that you can test with, like an SMS example sketch?

I directly send SMS message to this system for testing purpose. I don't have a separate code for testing. SMS example code that comes with Arduino ide is meant for GSM shields and don't work with my modem.

Is the modem in a place where it receives a strong RF signal from the network?

It definitely receives good rf signal and the modem locks pretty well.

Does the time interval between messages seem to make any difference? I ask because there are delays in your code...

I am sending sms messages from sensors on hourly basis to the receiver. The messages are stored in the Sim memory for later retrieval. It works when multiple sms messages are being sent simultaneously. So I don't think delay matters here.

From another Arduino? If so, how are you sure that they are sent correctly (or at all)?

Each sensor sends hourly sms message using an Arduino. I have confirmed that the sending part is ok. It attempts at least 3 times in case of failure to send. Also I checked by sending plain text from mobile phone.

What does this part do?

  else if ( millis() - start_time > 300000UL)
  {
    start_time = millis();
    altSerial.println("AT");
    delay(3000);
    if (!printSerial("OK")) SyncOrResetGsmAndTx();
  }


Since I am using the system 24x7, I use the above routine to check if the modem is responding from time to time. If the modem don't respond, the GSM 800A module is given a software reset.

Just on speculation, because it is so hard to definitely blame on that, have you tried disabling the reset, or making it less frequent and monitoring the results?

I spent some time studying your code and I can't see anything wrong (I don't pretend that I fully understand it, though... it would take more time).

Also run a side campaign to rule out silly stuff, like swapping hardware components?

This is a good suggestion. I will verify . Thanks @aarg for your time.