GSM Sim900 and Arduino uno keeps sending SMS over and over.

Hi. I am working on a project where you can send an SMS to the sim900 in a greenhouse to get the temperature from a DS18B20 sensor and control other variables and see other data. I am halfway through writing my code but i keep being put back by an issue i can't seem to resolve myself. When sending an SMS to read the temperature it sends back the temperature exactly how i wanted it to be. However, a few seconds later i am bombarded with these SMS messages from the Sim900. I do not know the reasoning for this because sending other SMS messages work just fine. I will paste all the code but the main functions you want to look at are updateTemp, getTemp, and send_message.

#include <DallasTemperature.h>
#include <OneWire.h>

#include <SoftwareSerial.h> // Library for using serial communication
SoftwareSerial SIM900(7, 8); // Pins 7, 8 are used as used as software serial pins

String incomingData;   // for storing incoming serial data
String message = "";   // A String for storing the message
int relay_pin = 4;    // Initialized a pin for relay module

int temp_sensor = 5;

float temp = 0;
float tempLimit = 25;
bool isHeatOn = true;
bool tempStated = false;

OneWire oneWirePin(temp_sensor);
DallasTemperature sensors(&oneWirePin);

void setup()
{
  Serial.begin(115200); // baudrate for serial monitor
  SIM900.begin(19200); // baudrate for GSM shield

  pinMode(relay_pin, OUTPUT);   // Setting erlay pin as output pin
  digitalWrite(relay_pin, HIGH);  // Making relay pin initailly high

  sensors.begin();

  // set SMS mode to text mode
  SIM900.print("AT+CMGF=1\r");  
  delay(100);
  
  // set gsm module to tp show the output on serial out
  SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
  delay(100);
}

void loop()
{
  //Function for updating the temp
  updateTemp();
  
  //Function for receiving sms
  receive_message();

  //Function to manually turn on or off the heater
  manualSwitch();

  //Function for getting the current temperature
  getTemp();

  //Function for changing the temperature limit
  changeTempLimit();

  //Function for checking heater status
  getHeatStatus();

  //Function for checking overall status
  getAllStatus();
  
  //Reset incoming data to stop texts sending repeatedly
  delay(1000);
  incomingData = "";
}

void updateTemp() {
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  delay(500);
}

void getAllStatus() {
  if (incomingData.indexOf("Status")>=0) {
    message = "Temperature: " + String(temp) + " \n Temperature Limit: " + String(tempLimit) + " \n Heater is on: " + String(isHeatOn);
    send_message(message);
  }
}

void getHeatStatus() {
  if (incomingData.indexOf("Heater status")>=0) {
    if (isHeatOn == true) {
      message = "Heater is on.";
      send_message(message);
      Serial.print(message);
    } else if (isHeatOn == false) {
      message = "Heater is off.";
      send_message(message);
      Serial.print(message);
    }
  }
  tempStated = false;
}

void changeTempLimit() {
  if (incomingData.indexOf("Increase limit")>=0) {
    tempLimit = tempLimit + 1;
    message = "Limit increased, now: " + String(tempLimit);
    send_message(message); 
    Serial.print(message);
  }
  if (incomingData.indexOf("Decrease limit")>=0) {
    tempLimit = tempLimit - 1;
    message = "Limit decreased, now: " + String(tempLimit);
    send_message(message); 
    Serial.print(message);
  }
  tempStated == false;
}

void getTemp() {
  if (incomingData.indexOf("Temp")>=0 && tempStated == false) {
    message = "Celsius Temperature is: " + String(temp);
    send_message(message); 
    
    tempStated == true;
    incomingData = "";
  }
}

void manualSwitch() {
   // if received command is to turn on relay
  if(incomingData.indexOf("Heater on")>=0)
  {
    digitalWrite(relay_pin, HIGH);
    message = "Led is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
    Serial.print("\n led is ON.");
  }
  
  // if received command is to turn off relay
  if(incomingData.indexOf("Heater off")>=0)
  {
    digitalWrite(relay_pin, LOW);
    message = "Led is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
    Serial.print("\n led is OFF.");
  }  
  tempStated = false;  
}

void receive_message()
{
  if (SIM900.available() > 0)
  {
    incomingData = SIM900.readString(); // Get the data from the serial port.
    Serial.print("Data: " + String(incomingData)); 
    delay(10); 
  }
}

void send_message(String message)
{
  SIM900.println("AT+CMGF=1");    //Set the GSM Module in Text Mode
  delay(100);  
  SIM900.println("AT+CMGS=\"+447719291638\""); // Replace it with your mobile number
  delay(100);
  SIM900.println(message);   // The SMS text you want to send
  delay(100);
  SIM900.println((char)26);  // ASCII code of CTRL+Z
  delay(100);
  SIM900.println();
  delay(1000);  
}

The response i get in the Serial Monitor is the string being repeated over and over but the last letters being cut of by the next string. Thanks for the hep in advance

You need to get rid of all your String variables. The String class is most likely fragmenting your memory and causing your program misbehave. Study up on using regular c-strings (arrays of char with a null byte at the end)

Here is one problem:

tempStated == true;

You need to get rid of all your String variables.

Totally with you on that, at some point this going to cause issues, and it is better to change now before the program evolves.

The String class is most likely fragmenting your memory and causing your program misbehave.

Well, not yet though, not after just 1 sms message, and if so the results would probably be different.
I looked the functions you mentioned, but i think there is an issue here.

void receive_message()
{
  if (SIM900.available() > 0)
  {
    incomingData = SIM900.readString(); // Get the data from the serial port.
    Serial.print("Data: " + String(incomingData));
    delay(10);
  }
}

Since most the other functions don't do anything unless there is any incomingData, and the Baud-rate is at 19200, very likely the buffer is empty before the whole msg is received.

Thanks everyone for your help.

tomisturfy:
Thanks everyone for your help.

Is it working now?