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