/**
   ESP32 + DHT22 Example for Wokwi
   
   https://wokwi.com/arduino/projects/322410731508073042
*/

#define BLYNK_TEMPLATE_ID "TMPL6yyD5RGF3"
#define BLYNK_TEMPLATE_NAME "ESP32 DHT 11"
#define BLYNK_AUTH_TOKEN "yNiEgwwGOpyJyaxahY0LdUTBgS89Wgbv"

#define BLYNK_PRINT Serial

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

#include "DHTesp.h"
char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "Wokwi-GUEST"; //nama hotspot yang digunakan
char pass[] = ""; //password hotspot yang digunakan

BlynkTimer timer;
const int DHT_PIN = 15;
#define ledkuning 13
#define ledhijau 12
#define ledbiru 14
#define ledmerah 27
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); 


DHTesp dhtSensor;

#include<RTClib.h> 
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};


void setup() {
  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  pinMode(ledkuning, OUTPUT);
    pinMode(ledhijau, OUTPUT);
      pinMode(ledbiru, OUTPUT);
        pinMode(ledmerah, OUTPUT);
          Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
lcd.init();
  lcd.backlight();


if (! rtc.begin()){
  lcd.print("Couldn't find RTC"); 
  while (1);
}
if (! rtc.isrunning()){
  lcd.print("RTC is NOT running!");
}
  lcd.setCursor(0, 0); 
  lcd.print("I PUTU JUNA ARYAWAN"); 
  lcd.setCursor(0, 1); lcd.print("1915344010"); 
  lcd.setCursor(0, 2); lcd.print("6B Teknik Otomasi"); 
  DateTime now = rtc.now(); lcd.setCursor(0, 3); 
  lcd.print(now.day());
  lcd.print('/');
  lcd.print(now.month());
  lcd.print('/');
  lcd.print(now.year());
  lcd.print(";");
  lcd.print(" Time ");
  lcd.print(now.hour());
  lcd.print(':'); lcd.print(now.minute()); 
  lcd.print(':'); delay(2000); 
  lcd.clear();
}




void loop() {
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  Serial.println("Temp: " + String(data.temperature, 2) + "°C");
  Serial.println("Humidity: " + String(data.humidity, 1) + "%");
  Serial.println("---");


  lcd.setCursor(1, 0);
  lcd.print("Temp : ");
  lcd.print(data.temperature);
  lcd.print(" C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity :");
  lcd.print(data.humidity);
  lcd.print("%");

if ( data.temperature <= 20){
    digitalWrite(ledhijau, HIGH);
     digitalWrite(ledmerah, LOW);
     digitalWrite(ledbiru, LOW);
     digitalWrite(ledkuning, LOW);
    Serial.println("NO TONE - DINGIN - LED HIJAU");

    Blynk.virtualWrite(V0,data.temperature); 
    Blynk.virtualWrite(V1, data.humidity);
    Blynk.virtualWrite(V2, 1);
    Blynk.virtualWrite(V3, 0);
    Blynk.virtualWrite(V4, 0);
    Blynk.virtualWrite(V5, 0);

}
   if ( 20<=  data.temperature && data.temperature <=30 ){
  tone(26, 100, 2000);
    digitalWrite(ledhijau, LOW);
     digitalWrite(ledmerah, LOW);
     digitalWrite(ledbiru, HIGH);
     digitalWrite(ledkuning, LOW);
  Serial.println("BUZZER ON - NORMAL - LED BIRU");
  delay(2000);

      Blynk.virtualWrite(V0,data.temperature); 
    Blynk.virtualWrite(V1, data.humidity);
    Blynk.virtualWrite(V2, 0);
    Blynk.virtualWrite(V3, 1);
    Blynk.virtualWrite(V4, 0);
    Blynk.virtualWrite(V5, 0);
}
   if ( 30<= data.temperature && data.temperature <=40 ){
  tone(26, 500, 1000);
      digitalWrite(ledhijau, LOW);
     digitalWrite(ledmerah, LOW);
     digitalWrite(ledbiru, LOW);
     digitalWrite(ledkuning, HIGH);
  Serial.println("BUZZER ON - HANGAT - LED KUNING ");
  delay(1000);
        Blynk.virtualWrite(V0,data.temperature); 
    Blynk.virtualWrite(V1, data.humidity);
    Blynk.virtualWrite(V2, 0);
    Blynk.virtualWrite(V3, 0);
    Blynk.virtualWrite(V4, 1);
    Blynk.virtualWrite(V5, 0);
  }
 if (data.temperature >= 40 ){
  tone(26, 1000, 100);
      digitalWrite(ledhijau, LOW);
     digitalWrite(ledmerah, HIGH);
     digitalWrite(ledbiru, LOW);
     digitalWrite(ledkuning, LOW);
  Serial.println("BUZZER ON - PANAS - LED MERAH");
  delay(100);
    Blynk.virtualWrite(V0,data.temperature); 
    Blynk.virtualWrite(V1, data.humidity);
    Blynk.virtualWrite(V2, 0);
    Blynk.virtualWrite(V3, 0);
    Blynk.virtualWrite(V4, 0);
    Blynk.virtualWrite(V5, 1);

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


}
GND5VSDASCLSQWRTCDS1307+