#define BLYNK_AUTH_TOKEN "CwaUOQKEPAcdArPR-CLEZav2rZp6Rkrt"
#define BLYNK_TEMPLATE_ID "TMPLhZhcIny_"
#define BLYNK_DEVICE_NAME "HCSR04"
char auth[] = "CwaUOQKEPAcdArPR-CLEZav2rZp6Rkrt";
#define BLYNK_PRINT Serial
#include <ESP32Servo.h>
Servo myservo;
#include "DHTesp.h"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>



#define Echo 15
#define Trig 4
#define Merah 14
#define Kuning 12
#define Hijau 13
int DHT_PIN = 18;
DHTesp dhtSensor;

char ssid[] = "Wokwi-GUEST";
char pass[] = "";

BlynkTimer timer;

float distance = 0;
float ketinggian = 0;
//TempAndHumidity  data = dhtSensor.getTempAndHumidity();

void ukur_jarak(){
  digitalWrite(Trig,LOW);
  delay(2);
  digitalWrite(Trig, HIGH);
  delay(10);
  digitalWrite(Trig, LOW);

  int duration = pulseIn(Echo, HIGH);
  distance = duration * 0.034 / 2;
  ketinggian = 350 - distance ;

}

void kirim_blynk(){
Blynk.virtualWrite(V0, ketinggian);
Blynk.virtualWrite(V4, ketinggian);

if (distance < 100){
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 0);
  Blynk.virtualWrite(V3, 1);
}
else if (distance >= 100 && distance <= 150){
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 1);
  Blynk.virtualWrite(V3, 0);
}
else if (distance > 150){
  Blynk.virtualWrite(V1, 1);
  Blynk.virtualWrite(V2, 0);
  Blynk.virtualWrite(V3, 0);
}

}

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(Merah, OUTPUT);
  pinMode(Kuning, OUTPUT);
  pinMode(Hijau, OUTPUT);
  myservo.attach(5);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  timer.setInterval(2000L,kirim_blynk);
}

void loop() {
   
  digitalWrite(Merah, LOW);
  digitalWrite(Hijau, LOW);
  digitalWrite(Kuning, LOW);
  
  //ukur ketinggian air
  ukur_jarak();
  Serial.print("Jarak: ");
  Serial.print(distance);
  Serial.println(" CM");
  Serial.print("Ketinggian Air : ");
  Serial.print(ketinggian);
  Serial.println(" CM");
 
 //ukur suhu dan kelmbaban
 TempAndHumidity  data = dhtSensor.getTempAndHumidity();
 //Serial.println("Suhu: " + String(data.temperature, 2) + "°C");
 //Serial.println("Kelembaban " + String(data.humidity, 1) + "%");
 Blynk.virtualWrite(V6, data.temperature);
 Blynk.virtualWrite(V7, data.humidity);
 Serial.println(data.humidity, 1);
 Serial.println("---");
 
 //int kelembaban;
 //kelembaban = data.humidity;
//if (data.humidity>40){
 // Serial.println("Tanah basah");
//}
 //kondisi ketinggian
//if( distance > 100 && distance < 150)
   if( data.humidity > 50 && data.humidity < 80){
    
   Blynk.virtualWrite(V6, 0);
   Blynk.virtualWrite(V7, 1);
    //Serial.println("Tanah basah")
    Serial.println("Kapasitas air cukup");
    digitalWrite(Kuning, HIGH);
    myservo.write(0);
    delay(500);
    myservo.write(45);
    //delay(500);
    //myservo.write(0);
    //delay(500);
  }
  //else if (distance > 0 && distance < 100)
  else if( data.humidity > 0 && data.humidity < 50)
  {
    Serial.println("Kapasitas air banyak");
    digitalWrite(Merah, HIGH);
    Blynk.virtualWrite(V6, 1);
    Blynk.virtualWrite(V7, 0);
    myservo.write(0);
    delay(500);
    myservo.write(90);
    //delay(500);
    //myservo.write(0);
    //delay(500);
  }
  else 
  {
    //else( kelembaban > 50 && Kelembaban < 80)
    //
    Serial.println("Kapasitas air sedikit");
    digitalWrite(Hijau, HIGH);
  }

  
  Blynk.run();
  timer.run();
  delay(500);
}