#include<SoftwareSerial.h>
#include "DHT.h"
SoftwareSerial Serial1(2,3); // Software serial library to make pin 2 and 3 as Rx & Tx for GSM module
#define Pump_Realy 6
#define DHT_Sensor 7
#define Moisture_Sensor A0
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHT_Sensor, DHTTYPE);
void setup()
{
pinMode(Pump_Realy,OUTPUT); //Set pin 6 as OUTPUT pin
pinMode(Moisture_Sensor, INPUT);
pinMode(Moisture_Sensor, INPUT_PULLUP);
pinMode(DHT_Sensor, INPUT);
gsmInit(); // gsmInit function is called for initialize the GSM module
Serial1.begin(9600); // Initialize serial and wait for port to open - opens serial port, sets data rate to 9600 bits per second:
Serial.begin(9600); // Initialize serial and wait for port to open - opens serial port, sets data rate to 9600 bits per second:
while (! Serial); // wait for serial port to connect. Needed for native USB
Serial.println("Speed 0 to 255");
Serial.println(F("DHT11 test!"));
dht.begin();
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000); //Wait before accessing Sensor
}
void loop() // the loop routine runs over and over again forever:
{
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity(); // Read temperature as Celsius (the default)
float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hif = dht.computeHeatIndex(f, h); // Compute heat index in Fahrenheit (the default)
float hic = dht.computeHeatIndex(t, h, false); // Compute heat index in Celsius (isFahreheit = false)
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
int sensorValue = analogRead(Moisture_Sensor); //Read data from soil moisture sensor
Serial.print(" Soil humidity = ");
Serial.println(sensorValue); // print out the value you read:
Serial.print("% ");
delay(10); // delay in between reads for stability
int flag=0;
if(sensorValue ==1 && flag==0)
{
delay(1000);
if(sensorValue ==1)
{
sendSMS("Low Soil Moisture detected. Pump turned ON");
digitalWrite(Pump_Realy, HIGH); //if soil moisture sensor provides HIGH value send HIGH value to motor pump and motor pump get ON
delay(1000); //Wait for few second and then continue the loop.
flag=1;
}
}
else if(sensorValue==0 && flag==1)
{
delay(1000);
if(sensorValue ==0)
{
sendSMS("Soil Moisture is Normal. Pump turned OFF");
digitalWrite(Pump_Realy, LOW); //if soil moisture sensor provides LOW value send LOW value to motor pump and motor pump goes OFF
delay(1000); //Wait for few second and then continue the loop.
flag=0;
}
}
}
void sendSMS(String msg) // A SMS is being sent to the user
{
Serial1.println("AT+CMGF=1");
delay(500);
Serial1.print("AT+CMGS=");
Serial1.print('"');
Serial1.print("+61452410776"); //User mobile number
Serial1.print('"');
Serial1.println();
delay(500);
Serial1.println(msg);
delay(500);
Serial1.write(26);
delay(1000);
}
void gsmInit() // GSM Communication to user
{
boolean at_flag=1;
while(at_flag)
{
Serial1.println("AT");
while(Serial1.available()>0)
{
if(Serial1.find("OK"))
at_flag=0;
}
delay(1000);
}
Serial1.println("ATE0");
boolean net_flag=1;
while(net_flag)
{
Serial1.println("AT+CPIN?");
while(Serial1.available()>0)
{
if(Serial1.find("READY"))
net_flag=0;
break;
}
delay(1000);
}
Serial1.println("AT+CNMI=2,2,0,0,0");
delay(1000);
Serial1.println("AT+CMGF=1");
delay(1000);
Serial1.println("AT+CSMP=17,167,0,0");
Serial1.flush();
}