Controlling Devices using GSM

Hi All,

I have this code below to turn ON/OFF lights and read temperature from a temp sensor using SMS. I have an issue when the code calls this function sendSMSWrongText(); at the end of the code. It keeps looping this function even if the sent code is correct.

Basically what I want it to do is to call this function sendSMSWrongText(); only when the SMS sent is incorrect, in this case if the SMS does not begin with a ? mark. I'm not sure if the else statement is in the correct place.

#include <SoftwareSerial.h>
#include <EEPROM.h>

SoftwareSerial SIM900(2,3); // RX, TX

int pin8 = 8;
int led13 = 13;
char buffer[9]; // eight char plus null terminator
char secret_code[] = "20!4#a1";
char secret_code1[] = "20!4#a0";
char secret_code2[] = "20!4#aS";
char secret_code3[] = "20!4#gt";
char incoming_char=0;
byte buffer_pos=0;
float temperature;
byte threshold;
int addr = 0;
/*-----( Declare Constants )-----*/
#define RELAY_ON 1
#define RELAY_OFF 0

/*-----( Declare variables )-----*/
#define Relay_1  8  // Arduino Digital I/O pin number
#define Relay_2  7
#define Relay_3  6
#define Relay_4  5

void setup()
{               
  pinMode(pin8, OUTPUT);
  pinMode(led13, OUTPUT);
  digitalWrite(led13, LOW);
  digitalWrite(pin8, LOW);  // Set led to LOW

    pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);

  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);

  Serial.begin(19200); // set the baud rate
  SIM900.begin(19200); // for GSM shield
  delay(20000);  // give time to log on to network.
  SIM900.print("ATE0\r");
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.println("AT+CNMI=2,2,0,0,0\r");
  delay(100);
  Serial.println("Finished Setup Section");
}

// SENDING AN SMS WITH THE LIGHT STATUS//

void sendSMS(byte led_status)               
{
  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+27\"");  // recipient's mobile number, in international format
  delay(1000);
  if (led_status==0)
  {
    SIM900.println( " LIGHT STATUS: OFF");
  }
  else
  {
    SIM900.println( " LIGHT STATUS: ON");
  }
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000);
  SIM900.println();
  Serial.println("Message sent");
  delay(5000);     // give module time to send SMS 
}

// SENDING SMS WHEN TEMPERATURE IS ABOVE THE DEFINED THRESHOLD//

void sendSMSTemp()               
{
  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+27\"");  // recipient's mobile number, in international format
  delay(1000);
  SIM900.print( " WARNING TEMPERATURE IS: ");
  SIM900.print(temperature);
  Serial.println(temperature);
  SIM900.println( " degree Celcius");
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000);
  SIM900.println();
  Serial.println("Message sent");
  delay(5000);     // give module time to send SMS 
} 

// SENDING SMS WITH CURRENT TEMPERATURE READINGS//

void sendSMSTempCurrent()               
{
  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+27\"");  // recipient's mobile number, in international format
  delay(1000);
  SIM900.print( " CURRENT TEMPERATURE IS: ");
  SIM900.print(temperature);
  Serial.println(temperature);
  SIM900.println( " degree Celcius");
  delay(1000);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(1000);
  SIM900.println();
  Serial.println("Message sent");
  delay(5000);     // give module time to send SMS 
} 

// SENDING SMS IF WRONG TEXT IS SENT //

void sendSMSWrongText()
{
  SIM900.print("AT+CMGF=1\r");                             // AT command to send SMS message
  delay(1000);
  SIM900.println("AT + CMGS = \"+2772\"");            // recipient's mobile number, in international format
  delay(1000);
  SIM900.println("Sorry! You have sent incorrect SMS code.");        // message to send
  delay(1000);
  SIM900.println((char)26);                  // End AT command with a ^Z, ASCII code 26
  delay(1000); 
  SIM900.println();
  delay(5000);                             // give module time to send SMS                                
}

// READING THE TEMPERATURE //

void readtemp()  
{ 
  int val = analogRead(0);
  float voltage = val * (4.0  /1023);  // compiler can optimize the constant part
  temperature = voltage * 100;  // 0.01V = 1C
  EEPROM.write(addr, 30);    // writing into EEPROM
  threshold = EEPROM.read(addr);  // reading EEPROM value
  Serial.print("Current Temp: ");
  Serial.print(temperature, 2); // Reading the temperature to 2 decimal point
  Serial.println("C");               // ALT-0176 => °
  delay(500);
  if (temperature >= threshold)
  {
    sendSMSTemp();
    do {
    } 
    while (1);
    delay(5000);              // wait for 5 second
  }
  else
  {
    sendSMSTempCurrent();
  }
}

// MAIN LOOP //

void loop()
{
  if (SIM900.available()>0)
  {
    Serial.println("Reading incoming data");
    incoming_char=SIM900.read();
    if (incoming_char=='?')
    {
      Serial.println("Got a ?");
      delay(100);
      buffer_pos=0;
      while (SIM900.available())
      {
        delay(100);
        incoming_char=SIM900.read();
        buffer[buffer_pos]=incoming_char;
        buffer_pos++;

        if (buffer_pos==7) // Already incremented
        {
          //Print it out
          buffer[buffer_pos]='\0';

          Serial.write(buffer);
          Serial.println();
          if (strcmp(buffer,secret_code)==0)
          {
            Serial.println("It's a match!"); 
            digitalWrite(pin8, HIGH);
            int led_status=digitalRead(pin8);
            Serial.print ("LED status = ");
            Serial.println(led_status);  // prints status on serial terminal
            sendSMS(led_status);
            delay(1000);              // wait for a second
          }
          buffer_pos=0;
          if (strcmp(buffer,secret_code1)==0)
          {
            Serial.println("It's a match!"); 
            digitalWrite(pin8, LOW);
            int led_status=digitalRead(pin8);
            Serial.print ("LED status = ");
            Serial.println(led_status);  // prints status on serial terminal
            sendSMS(led_status);
            delay(1000);              // wait for a second
          }
          buffer_pos=0;
          if (strcmp(buffer,secret_code2)==0)
          {
            Serial.println("It's a match!"); 
            int led_status=digitalRead(pin8);
            Serial.print ("LED status = ");
            Serial.println(led_status);  // prints status on serial terminal
            sendSMS(led_status);
            delay(1000);              // wait for a second
          }
          buffer_pos=0;
          if (strcmp(buffer,secret_code3)==0)
          {
            Serial.println("It's a match!"); 
            readtemp();
            delay(1000);              // wait for a second
          }
        }
      }
    }
    else 
  {
    sendSMSWrongText();
    delay(1000);              // wait for a second
  }
  buffer_pos=0;
  }
  
}

Kind Regards,

Snipping a bunch of stuff out of loop() (that belongs in functions), loop() contains:

  if (SIM900.available()>0)
  {
    Serial.println("Reading incoming data");
    incoming_char=SIM900.read();
    if (incoming_char=='?')
    {
      Serial.println("Got a ?");
      delay(100);
      buffer_pos=0;
    }
    else 
    {
      sendSMSWrongText();
      delay(1000);              // wait for a second
    }

I fail to see why you send a wrong text message if you don't get serial data. Clearly that else statement should be paired with some other if statement.