Keeps sending SMS Dont come out of loop

#include <SoftwareSerial.h>
#include <DHT.h>

#define DHTPIN 6 //DHT OneWire pin and its type
#define DHTTYPE DHT11

// Configure software serial port
SoftwareSerial sim800(3, 2);

// Variable to store text message
String textMessage;

// Create a variable to store Lamp state
String lampState = “off”;
String lampState1 = “off”;

// Relay connected to pin 4
const int relay = 4;
const int relay1 = 5;
int SMSSent=0

DHT dht(DHTPIN, DHTTYPE);

void setup() {

// Set relay as OUTPUT
pinMode(relay, OUTPUT);
pinMode (relay1, OUTPUT);

// By default the relay is off
digitalWrite(relay, HIGH);
digitalWrite(relay1, HIGH);

// Initializing serial commmunication
Serial.begin(19200);
sim800.begin(19200);

// Give time to your GSM shield log on to network
delay(3000);

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

Serial.println(“sim800 is ready to send receive sms”);

}

void loop() {
delay (2000);
{
while(sms_count<2)
}
dht.begin();

float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();

Serial.println(“DHT11 Temperature = “);
Serial.print(t);
Serial.println(” *C\t” ); //DHT11 Humidity
Serial.print(h);
Serial.println(" %\t"); //Show it on the serial monitor also optional
delay (5000);

if (sim800.available() > 0) {
textMessage = sim800.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(“On1”) >= 0) {
// Turn on relay and save current state
digitalWrite(relay1, LOW);
lampState1 = “on”;
Serial.println(“Relay set to ON 1”);
textMessage = “”;

}
if (textMessage.indexOf(“Off1”) >= 0) {
// Turn off relay and save current state
digitalWrite(relay1, HIGH);
lampState1 = “off”;
Serial.println(“Relay set to OFF 1”);
textMessage = “”;
}

if (textMessage.indexOf(“State”) >= 0) {
String message = "Lamp is " + lampState + ", Lamp 1 is " + lampState1;
sendSMS(message);
Serial.write(“Lamp state resquest”);
textMessage = “”;
}

if (textMessage.indexOf(“Temp”) >= 0) {
String message = “DHT11\nTemp = " + String(t, 1) + " C” + " \nHumidity = " + String(h, 1) + " %";
sendSMS(message);
Serial.write(“Temp request”);
textMessage = “”;
}

}
void sendSMS(String message) {

// AT command to set sim800 to SMS mode
sim800.write(“AT+CMGF=1\r\n”);
delay(100);

// change to your sim800’s your phone number
sim800.write(“AT+CMGS=”+5732889"\r\n");
delay(100);
// Send the SMS
sim800.println(message);
delay(100);
sim800.println((char)26);
delay(100);
sim800.println(“AT+CMGD=1,4”);

}

TempSim-DONE-Temp-progress.ino (3.31 KB)

Hello
Welcome to the Arduino fora.
Before you do anything else please take a moment to read General guidance and
How to use this forum
Especially item #7 on posting code.

People are more likely to help you if they don't have to download your code.

Thank you.

I found your sketch interesting as I am trying to do a similar project (to have a machine respond to requests for data, and send that data via a text message).

I believe, from my standpoint of being a learner too, that you must FLUSH the sms data once it has been used, or you will run the risk that on every loop the condition

if (textMessage.indexOf("whatever sms you sent") >= 0) {

will always be true, and so in your case it'll send the same reply about every 8 seconds (once per loop iteration).

I'm sure more learned programmers and experts will be along shortly, in the meantime look up FLUSH SMS on the Arduino resources page.

GM

Also note that a text message of "On1" will match the conditions for both the first relay (controlled via "On"/"Off") and second relay (controlled via "On1"/"Off1"). Similarly for "Off1".
That may be what you intended, but is seems unlikely.