#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
#include <DHT.h>
#define DHTPIN 2
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
float h, t;
#define soilmoisture A0
#define pir A1
int relay = 3;
int Animal_flag = 0;
int on_flag = 0;
int off_flag = 0;
int SoilValue,sensorValue;
#include <SoftwareSerial.h>
//GSM Serial
SoftwareSerial GSerial(A4, A5); // RX, TX // GSM
char aux_string[25];
char phone_number[20] = "xxxxxxxxxx"; // Change your no. here
char SMS[400];
unsigned long previousMillis = 5000, previousMillis1;
//*****************
int8_t sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, unsigned int timeout) //This function sends command to the GSM Modem and wait for reply according to arguments
{
uint8_t x = 0, answer = 0;
char response[300];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while ( GSerial.available() > 0) GSerial.read(); // Clean the input buffer
GSerial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do {
// if there are data in the UART input buffer, reads it and checks for the asnwer
if (GSerial.available() != 0) {
response[x] = GSerial.read();
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
else if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}
// Waits for the asnwer with time out
while ((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
//*****************
void send_message(char *message)
{
char aux_string[25];
uint8_t answer = 0;
Serial.print(F("GSM : Sending SMS ..."));
memset(aux_string, '\0', 24); // Initialize the string
sprintf(aux_string, "AT+CMGS=\"%s\"", phone_number);
answer = sendATcommand2(aux_string, ">", ">", 3000); // send the SMS number
if (answer == 1)
{
GSerial.println(message);
GSerial.write(0x1A);
answer = sendATcommand2("", "OK", "OK", 20000);
if (answer == 1)
{
Serial.println(F("Sent"));
lcd.clear();
lcd.print("Sent SMS OK");
delay(500);
}
else
{
Serial.println(F("X....Error"));
}
}
else
{
Serial.print(F("X...error "));
Serial.println(answer, DEC);
}
}
//*****************
void send_message_Load(char *message)
{
char aux_string[25];
uint8_t answer = 0;
Serial.print(F("GSM : Sending SMS ..."));
memset(aux_string, '\0', 24); // Initialize the string
sprintf(aux_string, "AT+CMGS=\"%s\"", phone_number);
answer = sendATcommand2(aux_string, ">", ">", 3000); // send the SMS number
if (answer == 1)
{
GSerial.println(message);
GSerial.print(F("Temperature : "));GSerial.print(t);
GSerial.print(F(", Humidity : "));GSerial.print(h);
GSerial.print(F(", Soil moisture : "));GSerial.print(SoilValue);
GSerial.write(0x1A);
answer = sendATcommand2("", "OK", "OK", 20000);
if (answer == 1)
{
Serial.println(F("Sent"));
lcd.clear();
lcd.print("Sent SMS OK");
delay(500);
}
else
{
Serial.println(F("X....Error"));
}
}
else
{
Serial.print(F("X...error "));
Serial.println(answer, DEC);
}
}
//*****************
void init_gsm()
{
uint8_t answer = 0;
uint8_t gsm_detect_count = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GSM init...");
delay(2000);
GSerial.begin(9600); //UART for GSM Modem
while ((sendATcommand2("AT", "OK", "OK", 2000) != 1) && (gsm_detect_count < 3)) //If 3 times gsm is not connected then it is not initialized
{
gsm_detect_count++;
Serial.println(F("GSM : Not Detected"));
delay(5000);
}
if (gsm_detect_count >= 3)
{
Serial.println(F("GSM : Finally Not Detected"));
Serial.println(F("GSM : System Hang"));
lcd.print("X");
lcd.setCursor(0, 1);
lcd.print("System Hang");
while (1)
{
}
}
else
{
lcd.print("OK");
Serial.println(F("GSM : Detected, Initializing"));
//****
Serial.print(F("GSM : Setting SMS mode..."));
answer = sendATcommand2("AT+CMGF=1", "OK", "OK", 1000); // sets the SMS mode to text
if (answer == 1)
{
Serial.println("OK");
}
else
{
Serial.println("X");
}
//****
Serial.print(F("GSM : Setting Baud Rate..."));
answer = sendATcommand2("AT+IPR=9600", "OK", "OK", 1000); // sets the SMS mode to text
if (answer == 1)
{
Serial.println(F("OK"));
}
else
{
Serial.println(F("X"));
}
//****
Serial.print(F("GSM : Setting Extra Mode..."));
answer = sendATcommand2("AT+CSMP=17,167,0,0", "OK", "OK", 1000); // sets the SMS mode to text
if (answer == 1)
{
Serial.println(F("OK"));
}
else
{
Serial.println(F("X"));
}
}
delay(2000);
lcd.clear();
}
void receive_gsm_message()
{
char *n1;
int8_t answer;
int x;
unsigned long previous;
unsigned int timeout = 5000;
// answer = sendATcommand("AT+CMGR=1", "+CMGR:", 5000); // reads the first SMS
answer = sendATcommand2("AT+CMGR=1", "+CMGR:", "OK", 250); // reads the first SMS
if (answer == 1)
{
Serial.println(F("\nGSM : Some Message Received"));
answer = 0;
x = 0;
previous = millis();
do {
// if there are data in the UART input buffer, reads it and checks for the asnwer
if (GSerial.available() > 0) {
SMS[x] = GSerial.read();
x++;
// check if the desired answer (OK) is in the response of the module
/*
if (strstr(SMS, "OK") != NULL)
{
answer = 1;
}*/
}
} while (answer == 0 && ((millis() - previous) < timeout)); // Waits for the asnwer with time out
SMS[x] = '\0';
answer = sendATcommand2("AT+CMGD=1", "OK", "OK", 500); // sets the SMS mode to text
/*Serial.print("Answer2 = ");
Serial.println(answer,DEC);
*/
Serial.print(F("GSM : Message Rec-")); Serial.println(SMS);
//decode_mobile_no(SMS);
//decode_string(SMS);
//decode_array3(SMS);
if (strstr(SMS, "DATA"))
{
Serial.println(F("Yes, DATA String Present"));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Received Message");
lcd.setCursor(0, 1);
lcd.print("Read Info ");
GSerial.listen();
send_message_Load("Info : ");
delay(1000);
}
else
{
Serial.println(F("No, String Not Present"));
}
}
else
{
//Serial.println(F("GSM : Error, No Message"));
}
}
void setup()
{
Serial.begin(9600);
dht.begin();
lcd.begin(16, 2);
pinMode(relay , OUTPUT);
pinMode(pir , INPUT);
digitalWrite(relay, LOW);
lcd.setCursor(0, 0);
lcd.print("XXXXXXXXXXXXXXXX");
lcd.setCursor(0, 1);
lcd.print("XXXXXXXXXXXXXXXX");
delay(2000);
lcd.clear();
//init_gsm();
delay(1000);
GSerial.listen();
send_message("System Started");
}
void dht11()
{
h = dht.readHumidity();
// Read temperature as Celsius (the default)
t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
lcd.setCursor(0, 0); lcd.print("T:"); lcd.setCursor(2, 0); lcd.print(t, 0); lcd.print("*C ");
lcd.setCursor(10, 0); lcd.print("H:"); lcd.setCursor(12, 0); lcd.print(h, 0); lcd.print(" % ");
}
void soil_Read()
{
int i;
long avg_val = 0;
for (i = 0; i < 1000; i++)
{
avg_val += analogRead(soilmoisture);
}
avg_val /= 1000;
sensorValue = avg_val;
SoilValue = map(sensorValue, 0, 1023, 100, 0);
lcd.setCursor(0, 1); lcd.print("SM: "); lcd.setCursor(4, 1); lcd.print(SoilValue); lcd.print(" % ");
if (SoilValue < 80)
{
digitalWrite(relay, HIGH);
if(on_flag == 0)
{
// SEND SMS
send_message("Water Pump ON");
on_flag = 1;
off_flag = 0;
}
}
else if (SoilValue > 80 )
{
digitalWrite(relay, LOW);
if(off_flag == 0)
{
// SEND SMS
send_message("Water Pump OFF");
on_flag = 0;
off_flag = 1;
}
}
return sensorValue;
}
void Animel_check()
{
if (digitalRead(pir) == HIGH)
{
delay(500);
if (digitalRead(pir) == HIGH)
{
if (Animal_flag == 0)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Animal Detect");
lcd.setCursor(0, 1);
lcd.print("Send SMS. ");
// SEND SMS
send_message("Animal Detect. Alart");
Animal_flag = 1;
delay(1000);
lcd.clear();
}
}
}
else
{
Animal_flag = 0;
}
}
void loop() {
dht11();
soil_Read();
Animel_check();
delay(200);
}