Arduino GSM Module | Serial Monitor is looping and displaying excess characters

Hi, my first post on the Arduino forum, I believe this section of the forum is the appropriate place to seek guidance for the problem I am having.

I’m developing a Sim800L EVB GSM module, interfacing through SoftwareSerial.h library, hooked up to a Arduino Uno.

I do not have the same problem as most of the other posts about the GSM modules; my code isn’t not capable of running smoothly, but a lot of the time my code will loop in a strange way in the Serial Monitor.

I will post my full code so you can view the logic:

void setup()
{
  Serial.begin(9600);


  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  GPRS.begin(9600);
  //Serial.println(GPRS.available());
  //Set as appropriate for your case
  pinMode(redPin, OUTPUT);
  pinMode(yelPin, OUTPUT);
  pinMode(potPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  digitalWrite(redPin, HIGH);
  digitalWrite(yelPin, LOW);
  delay(3000);

  prelim();


  //GPRS.println("AT+CNMI=?");
  delay(420);
  delay(1000);




}

void loop()
{
  //Read SIM800 output (if available) and print it in Arduino IDE Serial Monitor
  while (GPRS.available()) {
    parseATText(GPRS.read());
  }
  /* buttonState = digitalRead(buttonPin);
    if (buttonState != lastButtonState) {
     if (buttonPin == HIGH) {
       counter = counter + 1;
       buttonPushCounter = counter;
       Serial.println(buttonPushCounter);
     }
     delay(500);
    }*/

  //POTENTIOMETER
  tempVal = analogRead(potPin);
  value = Potentiometer(value);



} //end of loop function


void resetBuffer() {
  memset(buffer, 0, sizeof(buffer));
  pos = 0;
}

void parseATText(byte b) {

  buffer[pos++] = b;

  if ( pos >= sizeof(buffer) )
    resetBuffer(); // just to be safe

  /*
    // Detailed debugging
    Serial.println();
    Serial.print("state = ");
    Serial.println(state);
    Serial.print("b = ");
    Serial.println(b);
    Serial.print("pos = ");
    Serial.println(pos);
    Serial.print("buffer = ");
    Serial.println(buffer);*/

  switch (state) {
    case PS_DETECT_MSG_TYPE:
      {
        if ( b == '\n' )
          resetBuffer();
        else {
          if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
            state = PS_IGNORING_COMMAND_ECHO;
          }
          else if ( pos == 6 ) {
            //Serial.print("Checking message type: ");
            //Serial.println(buffer);

            if ( strcmp(buffer, "+CMTI:") == 0 ) {
              Serial.println("Received CMTI");
              state = PS_READ_CMTI_STORAGE_TYPE;
            }
            else if ( strcmp(buffer, "+CMGR:") == 0 ) {
              Serial.println("Received CMGR");
              state = PS_READ_CMGR_STATUS;
            }
            resetBuffer();
          }
        }
      }
      break;


    case PS_IGNORING_COMMAND_ECHO:
      {
        if ( b == '\n' ) {
          //Serial.print("Ignoring echo: ");
          //Serial.println(buffer);
          state = PS_DETECT_MSG_TYPE;
          resetBuffer();
        }
      }
      break;

    case PS_READ_CMTI_STORAGE_TYPE:
      {
        if ( b == ',' ) {
          Serial.print("SMS storage is ");
          Serial.println(buffer);
          state = PS_READ_CMTI_ID;
          resetBuffer();
        }
      }
      break;

    case PS_READ_CMTI_ID:
      {
        if ( b == '\n' ) {
          lastReceivedSMSId = atoi(buffer);
          Serial.print("SMS id is ");
          Serial.println(lastReceivedSMSId);

          GPRS.print("AT+CMGR=");
          GPRS.println(lastReceivedSMSId);
          //delay(500); don't do this!

          state = PS_DETECT_MSG_TYPE;
          resetBuffer();
        }
      }
      break;

    case PS_READ_CMGR_STATUS:
      {
        if ( b == ',' ) {
          Serial.print("CMGR status: ");
          Serial.println(buffer);
          state = PS_READ_CMGR_NUMBER;
          resetBuffer();
        }
      }
      break;

    case PS_READ_CMGR_NUMBER:
      {
        if ( b == ',' ) {
          Serial.print("CMGR number: ");
          Serial.println(buffer);

          // Uncomment these two lines to check the sender's cell number
          //validSender = false;
          //if ( strcmp(buffer, "\"+0123456789\",") == 0 )
          validSender = true;

          state = PS_READ_CMGR_SOMETHING;
          resetBuffer();
        }
      }
      break;



    case PS_READ_CMGR_SOMETHING:
      {
        if ( b == ',' ) {
          Serial.print("CMGR something: ");
          Serial.println(buffer);
          state = PS_READ_CMGR_DATE;
          resetBuffer();
        }
      }
      break;

    case PS_READ_CMGR_DATE:
      {
        if ( b == '\n' ) {
          Serial.print("CMGR date: ");
          Serial.println(buffer);
          state = PS_READ_CMGR_CONTENT;
          resetBuffer();
        }
      }
      break;

    case PS_READ_CMGR_CONTENT:
      {
        if ( b == '\n' ) {
          Serial.print("CMGR content: ");
          Serial.print(buffer);

          parseSMSContent();

          GPRS.print("AT+CMGD=");
          GPRS.println(lastReceivedSMSId);

          sendSMS(); // added sendSMS() here from other code.

          state = PS_DETECT_MSG_TYPE;
          resetBuffer();
        }
      }
      break;
  }
}
void prelim() {
  boolean isComplete = false;
  while (isComplete != true) {
    delay(1000);
    GPRS.println("AT+CSCS=\"GSM\"\r");
    delay(125);
    GPRS.println("AT+CMGF=1\r");
    delay(125);
    GPRS.println("AT+CNMI=1,1,0,0,0\n\r");
    delay(125);

    isComplete = true;
  }
  delay(3000);
  if (isComplete == true) {

    for (int i = 0; i <= 10; i++) {
      GPRS.print("AT+CMGD=");
      GPRS.println(i);
      delay(200);


      // Not really necessary but prevents the serial monitor from dropping any input
      while (GPRS.available())
        Serial.write(GPRS.read());
    }

  }
  Serial.println("Preliminary Function has completed\nModule is READY");
}

I run the prelim() function to set the module into GSM mode, and set the CNMI mode to notify me when a SMS is received.

I couldn’t put the entirety of my code because of the 9000 character limit. I have been able to get the program to run without issues a few times, however the Serial Monitor starts looping over and over and doesn’t even complete the commands it’s supposed to execute.

I’ll attach a Serial Monitor output log to explain what I mean. Often I will get CPIN Ready, even though I never set up the program to run AT+CPIN?.

Is there a fault in my logic that is somehow putting the prelim() function into the loop()?

As for the “displaying excess characters” part of the Subject line, my Serial Monitor will often put a “ÿ” at the end of some of my AT commands, and it is making the processor think I didn’t send “AT+CMGF=1”, it thinks I sent “AT+CMGF=1ÿ”, so there is no OK and I don’t believe the setting is changed to 1. It doesn’t do it every time I run the code, and the ÿ is not always in the same place.

Is it possible there is not enough power being supplied to the Arduino Uno?

That is not your full code. Where are variables like redPin, yelPin, potPin, and buttonPin defined?

however the Serial Monitor starts looping over and over and doesn't even complete the commands it's supposed to execute.

That is utter nonsense. The Serial Monitor does NOT execute commands, except Send.