#include <DHT.h> //Library for DHT22
#include <LiquidCrystal_I2C.h> //Library for LCD I2C
#include <RTClib.h> //Library for RTC DS1307
LiquidCrystal_I2C lcd(0x27,20,4); //Set the LCD address to 0x27 for a 16 chars and 2 line display
RTC_DS1307 rtc; //Set RTC
#define BUZZER 4
#define SPG 25 //Set Pin
#define DHTPIN 27 //Set Pin
#define SOILPIN 36 //Set Pin
#define DHTTYPE DHT22 //Set Type
DHT dht(DHTPIN, DHTTYPE);
/*Sensor Control*/
float conTemp = 40.00;
float conHumid = 30.00;
int conSoil = 30;
/*Time Control*/
byte conHrs1 = 11;
byte conMins1 = 48;
byte conSec1 = 00;
byte conHrs2 = 11;
byte conMins2 = 49;
byte conSec2 = 00;
/*Cordition Control*/
bool MANUAL = 0;
bool AIRCordition = 0;
bool SOILCordition = 0;
/*Function*/
void AIRCONTROL();
void SOILCONTROL();
void TIMECONTROL();
void setup()
{
Serial.begin(115200);
dht.begin(); //Initialize the DHT
rtc.begin(); //Initialize the RTC
lcd.init(); //Initialize the lcd
lcd.backlight();
lcd.clear();
pinMode(SOILPIN,INPUT);
pinMode(SPG,OUTPUT);
}
void loop(){
if(MANUAL == 0){
if ((AIRCordition == 1) || (SOILCordition == 1)){
digitalWrite(SPG,1);
lcd.setCursor(12,0);
lcd.print("SPG ON ");
// Serial.println("SPG ON");
tone(BUZZER, 500, 1000);
delay(1000);
noTone(BUZZER);
}
else if ((AIRCordition == 0) && (SOILCordition == 0)){
digitalWrite(SPG,0);
lcd.setCursor(12,0);
lcd.print("SPG OFF");
// Serial.println("SPG OFF");
lcd.setCursor(0,2);
// lcd.print("NORMAL !!");
noTone(BUZZER);
}
else{
}
}
else{
}
// Serial.println((String)AIRCordition +":" +SOILCordition);
AIRCONTROL();
SOILCONTROL();
TIMECONTROL();
}
void AIRCONTROL(){ //DHT22
float humid = dht.readHumidity();
// Read temperature as Celsius (the default)
float temp = dht.readTemperature();
if ((temp >= conTemp)&&(humid <= conHumid)){
lcd.setCursor(0,2);
lcd.print("HIGH TEMP LOW HUMID");
AIRCordition = 1;
}
else if (temp >= conTemp){
lcd.setCursor(0,2);
lcd.print("HIGH TEMP ");
AIRCordition = 0;
}
else if (humid <= conHumid){
lcd.setCursor(0,2);
lcd.print("LOW HUMID ");
AIRCordition = 0;
}
else{
lcd.setCursor(0,2);
lcd.print("AIR NORMAL ");
AIRCordition = 0;
}
}
void SOILCONTROL(){ //SOIL MOISTURE
int SOIL = analogRead(SOILPIN);
int mapSOIL = map(SOIL,0,4095,0,100);
if (mapSOIL <= conSoil){
lcd.setCursor(0,3);
lcd.print("LOW MOISTURE ");
SOILCordition = 1;
}
else{
lcd.setCursor(0,3);
lcd.print((String)"MOISTURE " +mapSOIL +"% NORMAL");
SOILCordition = 0;
}
}
void TIMECONTROL(){ //RTC
DateTime now = rtc.now();
byte hrs = now.hour();
byte mins = now.minute();
byte sec = now.second();
float humid = dht.readHumidity();
float temp = dht.readTemperature(); // Read temperature as Celsius (the default)
if ((hrs == conHrs1)&&(mins == conMins1)&&(sec == conSec1)){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MOR SPG ON ");
digitalWrite(SPG,1);
delay(3000);
lcd.clear();
MANUAL = 1;
}
else if ((hrs == conHrs2)&&(mins == conMins2)&&(sec == conSec2)){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("AFT SPG ON ");
digitalWrite(SPG,1);
delay(3000);
lcd.clear();
MANUAL = 1;
}
else{
lcd.setCursor(10,3);
lcd.setCursor(0,0);
lcd.print((String) hrs +":" +mins +":" +sec +" ");
// lcd.clear();
MANUAL = 0;
}
lcd.setCursor(0,1);
lcd.print((String) "T " +temp +"C : " +"H "+humid +"%");
}