AltSoftSerial with GSM library. Arduino keeps on resetting!

Good day!

I know there's been a lot of post about this but everything I've read has no concrete solution.

I'm using arduino (antenova) gsm shield with an arduino uno. I want to add an nrf module to control the house via sms.

I can send and receive data on sms and send data to nrf using tx rx but I'm having problem getting data from my nrf module since I the serial data I get from rt/tx are gibberish and I cant use software serial when using the gsm library.

So.. I downloaded AltSoftSerial library. I removed SoftwareSerial from my arduino library and replace it with AltSoftSerial. Now my arduino keeps on resetting.

If you want useful advice you have to post your code.

As you have made over 200 posts, I suspect you already know that.

…R

Sorry. I removed the long parts

#include <GSM.h>
#include <AltSoftSerial.h>

AltSoftSerial altSerial;

GSM gsmAccess;
GSM_SMS sms;

//VARIABLES


void setup() { 
  // initialize serial communications and wait for port to open:
  Serial.begin(115200);
  altSerial.begin(115200);

  Serial.println("Sensor Alarm Message Sender");

  InitGSM("");
  readSIM();
}

void loop() {
  // If there are any SMS available    
  checkSms();  

  checkNrf();
}

// SMS
void checkSms() {
  if (sms.available()) {
    char rx;
    char commMsg[160];
    char senderNumber[15];
    int index = 0;
    boolean authorized; 
    String txtMsg = "";

    Serial.print("\nMESSAGE RECEIVED FROM: ");   
    // Get remote number
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);

    char* incomingNum = strtok(senderNumber, "+");
    //Serial.println(incomingNum);
    
    // Read message bytes and print them
    commMsg[index] = '\0';
    while (rx = sms.read()) {
      //Serial.println(rx);
      if (rx != ' ') {
        commMsg[index] = rx;
        index++;
        commMsg[index] = '\0'; 
      }
    }
    //Serial.println(commMsg);
    
    // Delete message from modem memory
    sms.flush(); 
      

    if (strcmp(incomingNum, adminOneNum) == 0) {
      authorized = true;
    } else if (strcmp(incomingNum, adminTwoNum) == 0) {
      authorized = true;
    } else {
      authorized = false;
    }      
    Serial.println(authorized);
      
    char* header = strtok(commMsg, ",");
    char* dataMsg = strtok(NULL, ",");

    //Serial.println(header);
    //Serial.println(dataMsg);           

    if (authorized) {
      // APPLY CHANGES
    } else {
      // REGISTER NUMBER
    } 

    Serial.println("END OF MESSAGE\n");       
    index = 0;
    commMsg[index] = '\0';     

    if (resetSim == true) {
      delay(3000);
      softwareReset();
    }
  } 
}

void readSIM() {
  Serial.println("Reading SIM...");

  theGSM3ShieldV1ModemCore.println("AT+CPBS=\"SM\"");  
  start1 = 0;
  start1 = millis();   
  while ((millis() - start1) < timeout) {
    theGSM3ShieldV1ModemCore.theBuffer().read();
  }   

  // search for contact name "User1"
  theGSM3ShieldV1ModemCore.println("AT+CPBF=\"User1\"");  
  start1 = 0;
  start1 = millis();   
  while ((millis() - start1) < timeout && !theGSM3ShieldV1ModemCore.theBuffer().extractSubstring(",\"", "\",", adminOneNum, sizer)) {
    theGSM3ShieldV1ModemCore.theBuffer().read();
  }
  if (strcmp(adminOneNum, "") == 0) {
    adminOne = false;
  } else {
    adminOne = true;
    Serial.print("User1 exist: ");
    Serial.println(adminOneNum);    
    digitalWrite(user1Led, LOW);
    delay(100);
  }

  theGSM3ShieldV1ModemCore.println("AT+CPBS=\"SM\"");  
  start1 = 0;
  start1 = millis();   
  while((millis() - start1) < timeout){
    theGSM3ShieldV1ModemCore.theBuffer().read();
  } 

  // search for contact name "User2"
  theGSM3ShieldV1ModemCore.println("AT+CPBF=\"User2\"");  
  start1 = 0;
  start1 = millis();   
  while ((millis() - start1) < timeout && !theGSM3ShieldV1ModemCore.theBuffer().extractSubstring(",\"", "\",", adminTwoNum, sizer)) {
    theGSM3ShieldV1ModemCore.theBuffer().read();
  }
  if (strcmp(adminTwoNum, "") == 0) {
    adminTwo = false;
  } else {
    adminTwo = true;
    Serial.print("User2 exist: ");
    Serial.println(adminTwoNum);    
    digitalWrite(user2Led, LOW);
    delay(100);
  }

  Serial.println("Done");
}

void writeToSIM(char* index, char* number, String text) {  
  theGSM3ShieldV1ModemCore.println("AT+CPBS=\"SM\"");  
  start1 = 0;
  start1 = millis();   
  while ((millis() - start1) < timeout) {
    theGSM3ShieldV1ModemCore.theBuffer().read();
  }
  delay(1000);
  
  // add contact
  theGSM3ShieldV1ModemCore.print("AT+CPBW=");
  theGSM3ShieldV1ModemCore.print(index);
  theGSM3ShieldV1ModemCore.print(",\"");
  theGSM3ShieldV1ModemCore.print(number);
  theGSM3ShieldV1ModemCore.print("\",,\"");
  theGSM3ShieldV1ModemCore.print(text);
  theGSM3ShieldV1ModemCore.println("\"");
  start1 = 0;
  start1 = millis();   
  while ((millis() - start1) < timeout) {
    theGSM3ShieldV1ModemCore.theBuffer().read();
  }
  delay(1000);  
}

// NRF
void checkNrf() { //serialEvent
  if (altSerial.available()) {
    char inChar = altSerial.read();
    altSerial.print(inChar);
  }
}

void InitGSM(char* pin) {
   unsigned long myTimeout = 50000; // YOUR LIMIT IN MILLISECONDS 
   unsigned long timeConnect = millis();
  
   Serial.println("Trying to connect..");
  
   // Use async mode and requires that GSM debug mode has been set on GSM object creation
   gsmAccess.begin(pin, true, false); 
   
   while (millis()-timeConnect < myTimeout) {
      // Call this if debugging is on. Otherwise we will never reach GSM_READY...?!?
      gsmAccess.ready(); 
      
      // might not call ready too often.??? See GSM3ShieldV1AccessProvider.cpp, GSM3ShieldV1AccessProvider::begin
      delay(1000); 
      
      // Status check
      if (gsmAccess.getStatus() == GSM_READY) {
         Serial.println("Connected!\n");
         return;  
      }
   } 
   Serial.println("Timeout!\n");
   delay(500); // Let the message printout before reset is performed!
   
   SoftReset();
}

The output:

Sensor Alarm Message Sender
Trying to connect..
Connected!

Reading SIM...
User1 exist: xxxxx5217568
User2 exist: xxxxx9556785
Do"5
,Sensor Alarm Message Sender
Trying to connect..
Connected!

Reading SIM...
User1 exist: xxxxx5217568
User2 exist: xxxxx9556785
Do"5
"Sensor Alarm Message Sender
Trying to connect..
Connected!

Reading SIM...
User1 exist: xxxxx5217568
User2 exist: xxxxx9556785
Do"5
PSensor Alarm Message Sender
Trying to connect..
Connected!

Have you checked available free RAM while running your sketch?

It looks as if you are wasting tons of RAM by printing literal strings from RAM:

Serial.println("Sensor Alarm Message Sender");

instead of saving RAM and printing literal strings from flash memory using the F-macro:

Serial.println(F("Sensor Alarm Message Sender"));

Sketches that try to use more RAM than the controller has built in tend to show automatic resets due to buffer overflows.

I even tried SoftwareSerial Issues here: Learning Purposeful Science: Arduino GSM Shield tips & tricks

but I cant send message to the gsm anymore.

jurs:
Have you checked available free RAM while running your sketch?

how can I check?

arduinoTime:
how can I check?

Include this function in your sketch:

int freeRam() {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

And then perhaps in your loop and/or in your deepest nested function include some serial debug output like:

  Serial.print(F("Free RAM: "));Serial.println(freeRam());

Result with your original code?
Result after using the F-macro with print/println functions for printing literal strings?

arduinoTime:
Sorry. I removed the long parts

Please post a complete program.

...R