GSM 900 Sending SMS AT different temperature level using Arduinomega2560

This is my small project .Arduino will send one alert SMS through GSM Module at different temperature level.

component details:-

1] Arduino Mega 2560

2] Temperature sensor.

3] GSM 900A Module.

Here is my code:-

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial GPRS(10, 11);
#define ONE_WIRE_BUS A0//sensor connected to A0 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int TempState = 0;
int TempState1 = 0;
int TempState2 = 0;
float temp;
void setup() {
  Serial.begin(9600);
  GPRS.begin(9600);
  sensors.begin();
  GPRS.println("AT+CMGF=1");
  delay(500);
  GPRS.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
}
void loop() {
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  Serial.print("Temperature is");
  Serial.print(temp);// temp in celcius
  if ( temp >= 31 ) {

    while (TempState < 1) {
      Temp_sms1();
    }
  }
  if ( temp >= 32 ) {

    while (TempState1 < 1) {
      Temp_sms2();
    }
  }

  if ( temp >= 33 ) {

    while (TempState2 < 1) {
      Temp_sms3();
    }
  }
}
void Temp_sms1() {
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  char Temp1[8];
  dtostrf(temp, 7, 2, Temp1);// converted in to sting
  String message = String (" temp High : ") + Temp1;
  Serial.println(message);
  GPRS.println("AT+CMGF=1");
  delay(1000);
  GPRS.println("AT+CMGS=\"+919575610284\"");
  delay(1000);
  GPRS.println(message);
  delay(1000);
  GPRS.println((char)26);
  delay(800);
  Serial.println("SMS SEND");
  TempState++;
}
void Temp_sms2() {
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  char Temp1[8];
  dtostrf(temp, 7, 2, Temp1);
  String message = String (" temp High : ") + Temp1;
  Serial.println(message);
  GPRS.println("AT+CMGF=1");
  delay(1000);
  GPRS.println("AT+CMGS=\"+919575610284\"");
  delay(1000);
  GPRS.println(message);
  delay(1000);
  GPRS.println((char)26);
  delay(800);
  Serial.println("SMS SEND");
  TempState1++;
}
void Temp_sms3() {
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  char Temp1[8];
  dtostrf(temp, 7, 2, Temp1);
  String message = String (" temp High : ") + Temp1;
  Serial.println(message);
  GPRS.println("AT+CMGF=1");
  delay(1000);
  GPRS.println("AT+CMGS=\"+919575610284\"");
  delay(1000);
  GPRS.println(message);
  delay(1000);
  GPRS.println((char)26);
  delay(800);
  Serial.println("SMS SEND");
  TempState2++;
}

!!! This working fine !!!

Can anybody help me to reduce the size of this program while maintaining all the conditions

One thing you could do is to note that your Temp_smsN functions are very similar. The only difference appears to be which TempState variable they increment. Make the function take a reference to an int as a parameter, pass whichever TempState variable is appropriate and you will only need one function instead of the three.

Why do you need to make it smaller?

wildbill:
One thing you could do is to note that your Temp_smsN functions are very similar. The only difference appears to be which TempState variable they increment. Make the function take a reference to an int as a parameter, pass whichever TempState variable is appropriate and you will only need one function instead of the three.

Why do you need to make it smaller?

I am not well expert in programing,Tempstate i have used in this program for execute the function only one time..
Absence of Tempstate GSM will send SMS continuously.
Humbly request you to post the major changes i have to make in the program

Your use of the String type is completely unnecessary. This code:

sreekanthmp:

  String message = String (" temp High : ") + Temp1;

Serial.println(message);

Should be written like this:

Serial.print(" temp High : ");
Serial.println(Temp1);

If you want to reduce the amount of program memory, use the F() macro on your string literals. For example:

GPRS.println(F("AT+CMGF=1"));
Serial.print(F(" temp High : "));