#define BLYNK_TEMPLATE_ID "TMPLz_DgiUoN"
#define BLYNK_TEMPLATE_NAME "DHT22 ESP32 AIS"
#define BLYNK_AUTH_TOKEN "HCsIK8DanuB-HCF6BocNgZYWTCyYNiMi"

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

#define LED1 13
#define LED2 12
#define LED3 27
#define LED4 14
#define BUZZ 26
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);


char auth[] = "HCsIK8DanuB-HCF6BocNgZYWTCyYNiMi";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

BlynkTimer timer;

#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); 



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Monitoring Suhu");
  Serial.println("=================================================");
  dht.begin();
  Blynk.begin(auth, ssid, pass);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(BUZZ, OUTPUT);
  lcd.init(); 
  lcd.backlight(); 
  lcd.setCursor(0, 0); 
  lcd.print("I Gede Aiswarya Pandit"); 
  lcd.setCursor(0, 1); 
  lcd.print("2015344022"); 
  delay(2000);

}


void loop() {
  float temp = dht.readTemperature(); 
  float humi = dht.readHumidity(); 
  lcd.setCursor(0, 0); 
  lcd.print("Temp : "); 
  lcd.print(temp);
  lcd.print(" C"); 
  lcd.setCursor(0, 1); 
  lcd.print("Humidity: "); 
  lcd.print(humi); 
  lcd.print("%");


  if(temp < 20){
    Serial.println("Status Suhu : DINGIN");
    Serial.println("Green Light On");
    Serial.println("Buzzer Off");
    Serial.println("=================================================");
    digitalWrite(LED3,HIGH);
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,LOW);
    digitalWrite(LED4,LOW);
    Blynk.virtualWrite(V2, HIGH);
    Blynk.virtualWrite(V1, LOW);
    Blynk.virtualWrite(V3, LOW);
    Blynk.virtualWrite(V4, LOW);
    Blynk.virtualWrite(V5, LOW);
    Blynk.virtualWrite(V6, LOW);
    tone(BUZZ,0,0);
  } else if(temp >= 20 && temp < 30 ){
    Serial.println("Status Suhu : NORMAL");
    Serial.println("Blue Light On");
    Serial.println("Buzzer Fequencies 100Hz...");
    Serial.println("=================================================");
    digitalWrite(LED4, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED1, LOW);
    Blynk.virtualWrite(V3, HIGH);
    Blynk.virtualWrite(V4, LOW);
    Blynk.virtualWrite(V2, LOW);
    Blynk.virtualWrite(V1, LOW);
    Blynk.virtualWrite(V5, LOW);
    Blynk.virtualWrite(V6, HIGH);
    tone(BUZZ,100,2000);
  } else if(temp >= 30 && temp < 40){
    Serial.println("Status : WARM");
    Serial.println("Yellow Light On");
    Serial.println("Buzzer Fequencies 500Hz...");
    Serial.println("=================================================");
    digitalWrite(LED2, HIGH);
    digitalWrite(LED1, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    Blynk.virtualWrite(V4, HIGH);
    Blynk.virtualWrite(V3, LOW);
    Blynk.virtualWrite(V2, LOW);
    Blynk.virtualWrite(V1, LOW);
    Blynk.virtualWrite(V5, LOW);
    Blynk.virtualWrite(V6, HIGH);
    tone(BUZZ,500,1000);
  } else if(temp >= 40){
    Serial.println("Status : HOT");
    Serial.println("Red Light On");
    Serial.println("Buzzer Fequencies 1000Hz...");
    Serial.println("=================================================");
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    Blynk.virtualWrite(V5, HIGH);
    Blynk.virtualWrite(V2, LOW);
    Blynk.virtualWrite(V3, LOW);
    Blynk.virtualWrite(V4, LOW);
    Blynk.virtualWrite(V1, HIGH);
    Blynk.virtualWrite(V6, HIGH);
    tone(BUZZ,1000,100);
  }

  Blynk.virtualWrite(V0,temp);

  Serial.print("Temperature : ");;
  Serial.print(temp);
  Serial.println(" C");

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