I have a circuit with various sensors (ultrasonic, temperature, voltage and current) and have connected this to an lcd screen and simm800l. when the sensor measurements exceed certain parameters then the sim800l (d5, 6) sends a text to the specified number (removed). This all works correctly. also connected to the circuit is a lamp controlled by a relay switch (d13). I am trying to be able to open/ close the relay by text message but something in the code (presumably the parse or extract SMS functions) is not properly extracting the information within the texts I send. In the serial monitor when i send a text i will either get something like AT+CMGR=1: ''REC UNREAD'', "+44XXXXXXXXXX", "",14/05/24,1......or....+CMTI: SM,1.....
so i am receiving the message but not properly extracting its contents. I have attempted using AT+CMGR in the serial monitor but get the same response. I am attempting to message 'status' to get a message back with sensor values... or 'transformer on' 'transformer off' to open close the relay. If anyone is able to look at the code and help in any way it would be greatly appreciated.

#include <LiquidCrystal.h>          // this header file er  is used to drive the 16x2 LCD
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);   // this function is used to assign the Pin of LCD



#define RefreshTime 1




// Part Of GSM //
#include <SoftwareSerial.h>
SoftwareSerial SIM800L(5, 6);
String PHONE_NUM = "+44XXXXXXXXXX"; // 


bool OLF_Flag = 1;
bool OTF_Flag = 1;
bool OVF_Flag = 1;

String smsStatus, senderNumber, receivedDate, msg;
boolean isReply = false;




// Temperature Sensor

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

float t = 0;
#define ONE_WIRE_BUS 4
//#define ONE_WIRE_BUS1 D6


float Celsius = 0;
float Fahrenheit = 0;



OneWire oneWire(ONE_WIRE_BUS);       // Setup a oneWire instance to (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.





//part of ultrasonic sensor//

const int trigPin = 3;     // Variable to inrtface level sensor pin
const int echoPin = 2;// Variable to inrtface level sensor pin
long duration;             // conversion varible
double level, distanceCm; // conversion varible
float TankHeight = 20;





//part of current sensor//

const int currentsensor0 = A0;  // Variable to inrtface cur rent sensor pin
int mVperAmp = 185;             //  used to adjust sensitivity according to range
double Voltage0 = 0;            // conversion varible
double VRMS0 = 0;               // conversion varible
double Current = 0;            // conversion varible
float cutOffLimit = 0.0;





//part of voltage sensor//

float voltagevalue = A1;        // Variable to inrtface voltage sensor pin
float sensorvalue;              // conversion varible
float voltage;                  // conversion varible
float r1 = 47000.0;             // variable to select volatage devider circuit
float r2 = 33000.0;             // variable to select volatage devider circuit





int   state;
String Motor = "Motor off";
String space = " ";















// void setup is used to run anything at once during starting
// and Pin direction defination

void setup()

{


  sensors.begin();
  lcd.begin(16, 2);

  SIM800L.begin(9600);

  Serial.begin(9600);

  pinMode(13, OUTPUT);       // to define as output


  // To Write name of LCD//

  lcd.setCursor(0, 0);            // Used to select the cursor position
  lcd.print(" GSM Transformer");   // to Print the data as it is

  lcd.setCursor(0, 1);            // Used to select the cursor position
  lcd.print(" Fault Detection");       // to Print the data as it is

  delay(3000);                    // delay is used to wait in microsecond
  lcd.clear();                    // To Clear LCD





  // Part Of Level Sensor

  pinMode(trigPin, OUTPUT);       // to define as output
  pinMode(echoPin, INPUT);        // to define as input


  SIM800L.print("AT+CMGF=1\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=1\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=2\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=3\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=4\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=5\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=6\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=7\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=8\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=9\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=10\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=11\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=12\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=13\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=14\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=15\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=16\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=17\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=18\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=19\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=20\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=21\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=22\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=23\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=24\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=25\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=26\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=27\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=28\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=29\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=30\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=31\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=32\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=33\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=34\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=35\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=36\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=37\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=38\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=39\r"); //SMS text mode
  delay(100);
  SIM800L.print("AT+CMGD=40\r"); //SMS text mode
  delay(100);
}








// void Loop is used to run the code continuosly

void loop()

{

  while (SIM800L.available())  {
    parseData(SIM800L.readString());
  }

  while (Serial.available()) {
    SIM800L.println(Serial.readString());
  }




  //part of temperature measurement

  sensors.requestTemperatures();
  Celsius = sensors.getTempCByIndex(0);
  Fahrenheit = sensors.toFahrenheit(Celsius);
  //t = Fahrenheit;




  //part of ultra sonic sensor


  digitalWrite(trigPin, LOW);                 // to send the ultrasonic Wave
  delayMicroseconds(2);                       // delay is used to wait in microsecond

  digitalWrite(trigPin, HIGH);                // to recieve the ultrasonic Wave
  delayMicroseconds(10);                      // delay is used to wait in microsecond
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);          // measure the time of wave duration

  distanceCm = (duration * 0.034 / 2) - 3;        // to convert the time into distance


  if (distanceCm < 0)

  {
    distanceCm = 0;

  }

  if (distanceCm > TankHeight)

  {
    distanceCm = TankHeight;
  }



  level = abs((((distanceCm / TankHeight) * 100) - 100));
  Serial.println(level);









  //part of voltage measurement//

  sensorvalue = analogRead(voltagevalue);                                   // reading the volatge pin as analoge
  voltage =  (sensorvalue * (5.0 / 1024) * ((r1 + r2) / r2)); // used to convert the data into voltage







  //part of current measurment//


  Voltage0 = getVPP();                    // assign the current measung function
  VRMS0 = (Voltage0 / 2.0) * 0.707;          //root 2 is 0.707
  Current = ((VRMS0) / mVperAmp) * 1000 ; // convesion function




  // Part of GSM

  if (SIM800L.available() > 0)
    Serial.write(SIM800L.read());





  if (Current > 550 && OLF_Flag == 1)
  {
    String SMS = "Over Load Fault!";
    SendMessage(SMS);
    OLF_Flag = 0;
  }

  if (Current < 500 &&  OLF_Flag == 0)
  {
    String SMS = "Over Load Fault Recoverd!";
    SendMessage(SMS);
    OLF_Flag = 1;
  }



  if (Celsius > 60 && OTF_Flag == 1)
  {
    String SMS = "High Temprature Fault!";
    SendMessage(SMS);
    OTF_Flag = 0;
  }

  if (Celsius < 55 && OTF_Flag == 0)
  {
    String SMS = "High Temprature Fault Recoverd!";
    SendMessage(SMS);
    OTF_Flag = 1;
  }



  if (level < 40 && OVF_Flag == 1)
  {
    String SMS = "Low Oil Fault!";
    SendMessage(SMS);
    OVF_Flag = 0;
  }

  if (level > 70 && OVF_Flag == 0)
  {
    String SMS = "Low Oil Fault Recoverd!";
    SendMessage(SMS);
    OVF_Flag = 1;
  }







  // data printing on lcd//

  lcd.clear();

  lcd.setCursor(0, 0);             // to set cursor position  (row, column)
  lcd.print("I=");                 // to print the data as it is
  lcd.setCursor(5, 0);             // to set cursor position  (row, column)
  lcd.print("mA");                  // to print the data as it is
  lcd.setCursor(2, 0);             // to set cursor position  (row, column)
  lcd.print(Current, 0);          // to print the value of sensor on screen


  lcd.setCursor(0 , 1);           // to set cursor position  (row, column)
  lcd.print("V=");                // to print the data as it is
  lcd.setCursor(5 , 1);           // to set cursor position  (row, column)
  lcd.print("V");                 // to print the data as it is
  lcd.setCursor(2, 1);            // to set cursor position  (row, column)
  lcd.print(voltage, 2);          // to print the value of sensor on screen


  lcd.setCursor(10, 0);          // to set cursor position  (row, column)
  lcd.print("T=");               // to print the data as it is
  lcd.setCursor(15, 0);          // to set cursor position  (row, column)
  lcd.print("C");                // to print the data as it is
  lcd.setCursor(12, 0);          // to set cursor position  (row, column)
  lcd.print(Celsius);           // to print the value of sensor on screen


  lcd.setCursor(10, 1);         // to set cursor position  (row, column)
  lcd.print("L=");              // to set cursor position  (row, column)
  lcd.setCursor(14, 1);         // to set cursor position  (row, column)
  lcd.print("%");               // to set cursor position  (row, column)
  lcd.setCursor(12, 1);         // to set cursor position  (row, column)
  lcd.print(level, 0); // to print the value of sensor on screen



  delay(RefreshTime * 1000);                // LCD Update Time //

}








// Function to Measure AC Current//


float getVPP()
{
  float result0;
  int readValue0;           //value read from the sensor
  int maxValue0 = 0;        // store max value here
  int minValue0 = 1024;     // store min value here

  uint32_t start_time0 = millis();
  while ((millis() - start_time0) < 3000) //sample for 3 Sec
  {
    readValue0 = analogRead(currentsensor0);
    // see if you have a new maxValue
    if (readValue0 > maxValue0)
    {
      //record the maximum sensor value/
      maxValue0 = readValue0;
    }
    if (readValue0 < minValue0)
    {
      //record the minimum sensor value/
      minValue0 = readValue0;
    }
  }


  result0 = (((maxValue0 - minValue0) * 5.0) / 1024.0) * 1000;
  return result0;             //to store the result
}






void SendMessage(String SMS)
{
  SIM800L.println("AT+CMGF=1");
  delay(300);

  SIM800L.println("AT+CMGS=\"" + PHONE_NUM + "\"\r");
  delay(300);

  SIM800L.println(SMS);
  delay(100);

  SIM800L.println((char)26);
  delay(300);
}



void callNumber()
{
  SIM800L.print (F("ATD"));
  SIM800L.print (PHONE_NUM);
  SIM800L.print (F(";\r\n"));
}





void parseData(String Buffer)
{
  Serial.println(Buffer);
  unsigned int len, index;

  //Remove sent "AT Command" from the response string.
  index = Buffer.indexOf("\r");
  Buffer.remove(0, index + 2);
  Buffer.trim();

  if (Buffer != "OK") {
    index = Buffer.indexOf(":");
    String cmd = Buffer.substring(0, index);
    cmd.trim();

    Buffer.remove(0, index + 2);

    if (cmd == "+CMTI")
    {
      //get newly arrived memory location and store it in temp
      index = Buffer.indexOf(",");
      String temp = Buffer.substring(index + 1, Buffer.length());
      temp = "AT+CMGR=" + temp + "\r";
      //get the message stored at memory location "temp"
      SIM800L.println(temp);
    }


    else if (cmd == "+CMGR")
    {
      extractSms(Buffer);

      //if (senderNumber == PHONE_NUM)
      {
        doAction();
      }
    }
  }


  else
  {
    //The result of AT Command is "OK"
  }
}





void extractSms(String Buffer)

{
  unsigned int index;

  index = Buffer.indexOf(","); //Locates a character or String within another String. By default, searches from the beginning of the String,
  //but can also start from a given index, allowing for the locating of all instances of the character or String.

  smsStatus = Buffer.substring(1, index - 1);
  Buffer.remove(0, index + 2);

  senderNumber = Buffer.substring(0, 13);
  Buffer.remove(0, 19);

  receivedDate = Buffer.substring(0, 20);
  Buffer.remove(0, Buffer.indexOf("\r"));
  Buffer.trim();

  index = Buffer.indexOf("\n\r");
  Buffer = Buffer.substring(0, index);
  Buffer.trim();
  msg = Buffer;
  Buffer = "";
  msg.toLowerCase();
  Serial.println(msg);
}



void Reply(String text)
{
  SIM800L.print("AT+CMGF=1\r");
  delay(1000);
  SIM800L.print("AT+CMGS=\"" + PHONE_NUM + "\"\r");
  delay(1000);
  SIM800L.print(text);
  delay(1000);
  SIM800L.write(0x1A); //ascii code for ctrl-26      // SIM800L.println((char)26);   //ascii code for ctrl-26
  delay(1000);
  Serial.println("SMS Sent Successfully.");
}








void doAction()
{

  if (msg == "Transformer on")
  {
    digitalWrite(13, HIGH);
    Motor = "Transformer On";
    //  Reply("Relay 2 has been On");
    Reply("TRANSFORMer TURNED ON" + space + '\n' + '\n' + "TANK LEVEL = "  +  String(level) + '\n' + "TEMPERATURE = " + String(Celsius)  );
  }



  else if (msg == "Transformer off")
  {
    digitalWrite(13, LOW);
    Motor = "Transformer Off";
    //  Reply("Relay 2 has been On");
    Reply("TRANSFORMer TURNED OFF" + space + '\n' + '\n' + "TANK LEVEL = "  +  String(level) + '\n' + "TEMPERATURE = " + String(Celsius)  );
  }


  else  if (msg == "status")
  {
    Reply("TANK LEVEL = " +  String(level) + '\n' + "TEMPERATURE = " + String(Celsius) + '\n' + Motor);
  }



  smsStatus = "";
  senderNumber = "";
  receivedDate = "";
  msg = "";
}