/*[email protected]*/
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
 

const int DHT_PIN = 15;
const int WATER_LEVEL_PIN = 34;

const int SPRINKLER_CONTROL_PIN = 13;

const int BUZZER_PIN = 12;

const int TEMPERATURE_WARNING_LED = 25;
const int WATER_LEVEL_WARNING_LED = 26;



const float TEMPERATURE_LIMIT_UPPER = 45.0;
const float TEMPERATURE_LIMIT_LOWER = 15.0;

const float HUMIDITY_LIMIT_UPPER = 92.0;
const float HUMIDITY_LIMIT_LOWER = 60.0;

const int WATER_LEVEL_LIMIT_UPPER = 3135;
const int WATER_LEVEL_LIMIT_LOWER = 2165;

LiquidCrystal_I2C lcd(0x27,16,2);


DHTesp dhtSensor;
void setup()
{
  pinMode(SPRINKLER_CONTROL_PIN, OUTPUT);
  pinMode(TEMPERATURE_WARNING_LED, OUTPUT);
  pinMode(WATER_LEVEL_WARNING_LED, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(SPRINKLER_CONTROL_PIN, LOW);
  noTone(BUZZER_PIN);
  Wire.begin(23, 22);
  Serial.begin(9600);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  lcd.init();
  lcd.backlight();
}

void loop()
{ 
  noTone(BUZZER_PIN);
  TempAndHumidity  dht_data = dhtSensor.getTempAndHumidity();
  int16_t water_level = analogRead(WATER_LEVEL_PIN);
  String water_level_msg = water_level < WATER_LEVEL_LIMIT_LOWER ? "LOW" : "NORMAL";
  String temeparature_level_msg = dht_data.temperature > TEMPERATURE_LIMIT_UPPER ? "HIGH" :"NORMAL";
  String humidity_level_msg = dht_data.humidity < HUMIDITY_LIMIT_LOWER ? "LOW" : "NORMAL";
  lcd.clear();
  if(water_level_msg=="LOW" && temeparature_level_msg=="HIGH"){
    lcd.setCursor(0, 0);
    lcd.print("----- WARNING! -----");
    lcd.setCursor(0, 1);
    lcd.print("--- SPRINKLER ON ---");
    digitalWrite(SPRINKLER_CONTROL_PIN, HIGH);
    digitalWrite(WATER_LEVEL_WARNING_LED, HIGH);
    digitalWrite(TEMPERATURE_WARNING_LED, HIGH);
    tone(BUZZER_PIN, 100);
  }
  else if(water_level_msg=="LOW"){
    digitalWrite(SPRINKLER_CONTROL_PIN, LOW);
    digitalWrite(WATER_LEVEL_WARNING_LED, HIGH);
    digitalWrite(TEMPERATURE_WARNING_LED, LOW);
    lcd.setCursor(0, 0);
    lcd.print("------ [INFO] ------");
    lcd.setCursor(0, 1);
    lcd.print("- LOW WATER LEVEL! -");
    tone(BUZZER_PIN, 500);
  }
  else if(temeparature_level_msg=="HIGH"){
    digitalWrite(SPRINKLER_CONTROL_PIN, LOW);
    digitalWrite(WATER_LEVEL_WARNING_LED, LOW);
    digitalWrite(TEMPERATURE_WARNING_LED, HIGH);
    lcd.setCursor(0, 0);
    lcd.print("------ [INFO] ------");
    lcd.setCursor(0, 1);
    lcd.print("- HIGH TEMPERATURE -");
    tone(BUZZER_PIN, 500);
  }
  else{
    lcd.setCursor(0, 0);
    lcd.print("Temperature: ");
    lcd.print(temeparature_level_msg);
    lcd.setCursor(0, 1);
    lcd.print("Humidity   : ");
    lcd.print(humidity_level_msg);
    lcd.setCursor(0, 2);
    lcd.print("Water Level: ");
    lcd.print(water_level_msg);
    digitalWrite(SPRINKLER_CONTROL_PIN, LOW);
    digitalWrite(WATER_LEVEL_WARNING_LED, LOW);
    digitalWrite(TEMPERATURE_WARNING_LED, LOW);
    noTone(BUZZER_PIN);
  }
  
  delay(500); 
}


/*[email protected]*/
Soil SensorBreakout