GSM not sending sms

relay control through SMS using GSM900A AND Arduino uno

program copied from google.

#include <SoftwareSerial.h>

// Configure software serial port
SoftwareSerial SIM900A(10, 11);

// Variable to store text message
String textMessage;

// Create a variable to store Lamp state
String lampState = "HIGH";

// Relay connected to pin 12
const int relay = 12;

void setup() {
  // Automatically turn on the shield
 /* digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);*/
  
  // Set relay as OUTPUT
  pinMode(relay, OUTPUT);

  // By default the relay is off
  digitalWrite(relay, HIGH);
  
  // Initializing serial commmunication
  Serial.begin(9600); 
  SIM900A.begin(9600);

  // Give time to your GSM shield log on to network
  delay(1000);
  Serial.print("SIM900A ready...");

  // AT command to set SIM900 to SMS mode
  SIM900A.print("AT+CMGF=1\ "); 
  delay(100);
  // Set module to send SMS data to serial out upon receipt 
  SIM900A.print("AT+CNMI=2,2,0,0,0\ ");
  delay(100);
}

void loop(){
  
  if(SIM900A.available()>0){
    textMessage = SIM900A.readString();
    Serial.print(textMessage);    
    delay(10);
  } 
  if(textMessage.indexOf("ON")>=0){ 
    // Turn on relay and save current state
    digitalWrite(relay, LOW);
    lampState = "on";
    Serial.println("Relay set to ON");  
    textMessage = "";   
  }
  if(textMessage.indexOf("OFF")>=0){
    // Turn off relay and save current state
    digitalWrite(relay, HIGH);
    lampState = "off"; 
    Serial.println("Relay set to OFF");
    textMessage = ""; 
  }
  if(textMessage.indexOf("STATE")>=0){
    String message = "Lamp is " + lampState;
    sendSMS(message);
    Serial.println("Lamp state resquest");
    textMessage = "";
  }
}  

// Function that sends SMS
void sendSMS(String  message){
  // AT command to set SIM900 to SMS mode
  SIM900A.print("AT+CMGF=1\ "); 
  delay(100);

  // REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER
  // USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS
  SIM900A.println("AT+CMGS=\"+91798755378\"\ "); 
  delay(1000);
  // Send the SMS
  Serial.println ("Set SMS Content");
  SIM900A.println(message); 
  delay(100);
  Serial.println ("Finish");
  // End AT command with a ^Z, ASCII code 26
  SIM900A.println((char)26); 
  delay(100);
  Serial.println ("Message has been sent ->SMS Selesai dikirim");
  SIM900A.println();
  // Give module time to send SMS
  delay(5000); 
}

when i sending "STATE" sms to GSM for knowing the current status of lamp GSM not sending reply sms on my mobile.Remaining functions are ok.

Serial monitor appear like below.

SIM900A ready...AT+CMGF=1 AT+CNMI=2,2,0,0,0 
+CMT: "+917987553768","","18/12/18,16:19:03+22"
STATE
Set SMS Content
Finish
Message has been sent ->SMS Selesai dikirim
Lamp state resquest
AT+CMGF=1 AT+CMGS="+917987553768" 

ERROR
Lamp is HIGH

Pls advice what are the mistakes on my programme

increase delay for cmgf to 2 secs and cmgs to 4 secs

KASSIMSAMJI:
increase delay for cmgf to 2 secs and cmgs to 4 secs

no improvement after increasing delay time

add enough delay after SIM900A.println(message); , like 2 secs may be

#include <SoftwareSerial.h>

// Configure software serial port
SoftwareSerial SIM900(10, 11);

// Variable to store text message
String textMessage;

// Create a variable to store Lamp state
String lampState = "HIGH";

// Relay connected to pin 12
const int relay = 12;

void setup() {
  // Automatically turn on the shield
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(5000);
  
  // Set relay as OUTPUT
  pinMode(relay, OUTPUT);

  // By default the relay is off
  digitalWrite(relay, HIGH);
  
  // Initializing serial commmunication
  Serial.begin(9600); 
  SIM900.begin(9600);

  // Give time to your GSM shield log on to network
  delay(20000);
  Serial.print("SIM900 ready...");

  // AT command to set SIM900 to SMS mode
  SIM900.print("AT+CMGF=1\r"); 
  delay(100);
  // Set module to send SMS data to serial out upon receipt 
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
}

void loop(){
  if(SIM900.available()>0){
    textMessage = SIM900.readString();
    Serial.print(textMessage);    
    delay(10);
  } 
  if(textMessage.indexOf("ON")>=0){
    // Turn on relay and save current state
    digitalWrite(relay, LOW);
    lampState = "on";
    Serial.println("Relay set to ON");  
    textMessage = "";   
  }
  if(textMessage.indexOf("OFF")>=0){
    // Turn off relay and save current state
    digitalWrite(relay, HIGH);
    lampState = "off"; 
    Serial.println("Relay set to OFF");
    textMessage = ""; 
  }
  if(textMessage.indexOf("STATE")>=0){
    String message = "Lamp is " + lampState;
    sendSMS(message);
    Serial.println("Lamp state resquest");
    textMessage = "";
  }
}  

// Function that sends SMS
void sendSMS(String message){
  // AT command to set SIM900 to SMS mode
  SIM900.print("AT+CMGF=1\r"); 
  delay(100);

  // REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER
  // USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS
  SIM900.println("AT + CMGS = \"+917987553768\""); 
  delay(10000);
  // Send the SMS
  SIM900.println(message); 
  delay(6000);

  // End AT command with a ^Z, ASCII code 26
  SIM900.println((char)26); 
  delay(6000);
  SIM900.println();
  // Give module time to send SMS
  
  delay(5000); 
  SIM900.println((char)26); 
  delay(6000);
}

After changing delay time also i am not getting lamp state sms from GSM.

Please help to me for make it correct

I want to add a part to be able to rest the program just by sending a code.
I tried so many ways but mostly got problem. Any help regarding the issue.

int GSM_SMS::available()
{
if (_incomingBuffer.indexOf("1#")) { //here but does not work
//if (_incomingBuffer.length() != 0) {
Serial.println("reset");
APP_GSM_ShutdownReset();
}
else{
if (_incomingBuffer.length() != 0) {
int nextMessageIndex = _incomingBuffer.indexOf("\r\n+CMGL: ");

if (nextMessageIndex != -1) {
_incomingBuffer.remove(0, nextMessageIndex + 2);
} else {
_incomingBuffer = "";
}
}
}

if (_incomingBuffer.length() == 0) {
int r;

if (_state == SMS_STATE_IDLE) {
_state = SMS_STATE_LIST_MESSAGES;
}

if (_synch) {
while ((r = ready()) == 0) {
delay(100);
}
} else {
r = ready();
}

if (r != 1) {
return 0;
}
}

if (_incomingBuffer.startsWith("+CMGL: ")) {
_smsDataIndex = _incomingBuffer.indexOf('\n') + 1;

_smsDataEndIndex = _incomingBuffer.indexOf("\r\n+CMGL: ");
if (_smsDataEndIndex == -1) {
_smsDataEndIndex = _incomingBuffer.length() - 1;
}

return (_smsDataEndIndex - _smsDataIndex);
} else {
_incomingBuffer = "";
}

return 0;

}

To put your code in a code box, use the </> icon in the far left of the post tool bar and paste your code between the two bracket sets that appear.

To go back and put your code in a code box, in the bottom right of your post, select "more" and click modify. When the modify post opens, high light your code and click the </> in the far left of the post tool bar. This will put you code in code brackets. Then save the changes.