Sending and Receiving an SMS text message using the SIM7600X 4G Hat

Hi,

For my project, I am using an Arduino UNO connected to a 4G modem and GPS module (SIM7600A). My goal is to use this modem in order to send and receive SMS messages between a user’s phone and the unit.

I found an example code online that I have used to verify that it works and begin to understand how the modem works with the Arduino, I just had a few questions. Here is the code I am running:

#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"


#define LED13   13


#define GSM_RX  4
#define GSM_TX  3
#define GSM_PWR 5
#define GSM_RST 20 // Dummy
#define GSM_BAUD  9600


char replybuffer[255];


SoftwareSerial SIM7600SS = SoftwareSerial(GSM_TX, GSM_RX);
SoftwareSerial *SIM7600Serial = &SIM7600SS;


Adafruit_FONA SIM7600 = Adafruit_FONA(GSM_RST);


uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
char SIM7600InBuffer[64]; // For notifications from the FONA
char callerIDbuffer[32]; // We'll store the SMS sender number in here
char SMSbuffer[32]; // We'll store the SMS content in here
uint16_t SMSLength;
String SMSString = "";
boolean buttonEnable = false;


void setup()
{


  pinMode(LED13, OUTPUT);
  
  pinMode(GSM_PWR, OUTPUT);
  pinMode(GSM_PWR, INPUT_PULLUP);


  Serial.begin(115200);
  Serial.println("Interfacing SIM7600 GSM Module with Arduino UNO");
  Serial.println("Initializing... (May take a minute)");




  // Make it slow so its easy to read!
  SIM7600Serial->begin(GSM_BAUD);            // 9600 Baud
  if (!SIM7600.begin(*SIM7600Serial)) {
    Serial.println("Couldn't find SIM7600");
    while (1);                                          
  }
  Serial.println(F("SIM7600 is OK"));


  // Print SIM card IMEI number.
  char imei[16] = {0}; // MUST use a 16 character buffer for IMEI!
  uint8_t imeiLen = SIM7600.getIMEI(imei);
  if (imeiLen > 0) {
    Serial.print("SIM card IMEI: "); Serial.println(imei);
  }


  SIM7600Serial->print("AT+CNMI=2,1\r\n");  


  Serial.println("GSM is ready!");


}


void loop()
{
  char* bufPtr = SIM7600InBuffer; 


  if (SIM7600.available()) { // Any data available from the SIM800L
    int slot = 0; // This will be the slot number of the SMS
    int charCount = 0;


    do {
      *bufPtr = SIM7600.read();
      Serial.write(*bufPtr);
      delay(1);
    } while ((*bufPtr++ != '\n') && (SIM7600.available()) && (++charCount < (sizeof(SIM7600InBuffer) - 1)));


    // Add a terminal NULL to the notification string
    *bufPtr = 0;


    // Scan the notification string for an SMS received notification.
    // If it's an SMS message, we'll get the slot number in 'slot'
    if (1 == sscanf(SIM7600InBuffer, "+CMTI: \"SM\",%d", &slot)) {
      Serial.print("slot: "); Serial.println(slot);


      // Retrieve SMS sender address/phone number.
      if (!SIM7600.getSMSSender(slot, callerIDbuffer, 31)) {
        Serial.println("Didn't find SMS message in slot!");
      }
      Serial.print("FROM: "); 
      Serial.println(callerIDbuffer);




//// QUESTION 1 BELOW*


      if (!SIM7600.readSMS(slot, SMSbuffer, 250, &SMSLength)) { // pass in buffer and max len!
        Serial.println("Failed!");
      }
      else {
        SMSString = String(SMSbuffer);
        Serial.print("SMS: "); Serial.println(SMSString);
      }






      // Compare SMS string
     
      if (SMSString == "LED13 ON") {
        Serial.print("Setting LED 13 to on.");
        digitalWrite(LED13, HIGH);


        // Send SMS for status
        if (!SIM7600.sendSMS(callerIDbuffer, "Setting LED to on.")) {
          Serial.println("Failed");
        }
        else {
          Serial.println(F("Sent!"));
        }
      }
      
      else if (SMSString == "LED13 OFF") {
        Serial.print("Setting LED 13 to off.");
        digitalWrite(LED13, LOW);
 
        // Send SMS for status
        if (!SIM7600.sendSMS(callerIDbuffer, "Setting LED to off.")) {
          Serial.println("Failed");
        }
        else {
          Serial.println(F("Sent!"));
        }
      }
      else {
        Serial.print("Invalid command.");
      }


 ///// QUESTION 2 BELOW*     

      while (1) {
        boolean deleteSMSDone = SIM7600.deleteSMS(slot);
        if (deleteSMSDone == true) {
          Serial.println("OK!");
          break;
        }
        else {
          Serial.println("Couldn't delete, try again.");
        }
      }


    }
  }


}

It is pretty basic, just using an incoming text to turn ON/OFF LED 13 on the Uno. My questions are:

  1. By the tag “Question 1 Below*” in the code, that if-else loop seems to run incorrectly quite often, and I don’t understand it well enough to figure out why. When a text is received, my serial monitor will print “Failed!” many times over, even for a successful text!

  2. Every time the unit receives a text, it has trouble in the last while loop deleting the previous message. The code will be hung up for far too long printing “Couldn’t delete, try again” over and over. This prevents a new message from being received for a few minutes, and sometimes I even have to re-upload to send a new message.

I was hoping anyone with some further guidance/advice could help to understand a little better what is going on here. Tthanks!