#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLdUf0kMbd"
#define BLYNK_DEVICE_NAME "DHT22"

char auth[] = "x9SNnLfpgZetKrs-kYY1xT5TXkDoAshg";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"

const int DHT_PIN = 15;
#define Led 13
#define Tombol 14
DHTesp dhtSensor;
BlynkTimer timer;
int Izin=LOW;


void sendSensor() {
 TempAndHumidity  data = dhtSensor.getTempAndHumidity();
 Serial.println("Temp: " + String(data.temperature, 2) + "°C");
 Serial.println("Humidity: " + String(data.humidity, 1) + "%");
 Serial.println("---");
 Blynk.virtualWrite(V1, data.temperature); 
 Blynk.virtualWrite(V0, data.temperature); 
Blynk.virtualWrite(V3, data.humidity); 
Blynk.virtualWrite(V4, data.humidity); 
 //mengirimkan data ke Virtual pin di Blynk Cloud
}

void setup()
{
  pinMode(Led,OUTPUT);
  pinMode(Tombol,OUTPUT);
  Serial.begin(9600);
  digitalWrite(Led,LOW);
  Blynk.begin(auth, ssid, pass);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  timer.setInterval(2000L, sendSensor);
}


void loop() {
while (Izin==LOW){
   int StatusTbl=digitalRead(Tombol);
    if (StatusTbl==HIGH) 
    {  Izin=HIGH;
     digitalWrite(Led,HIGH);
    }
    }
      Blynk.run();
      timer.run();
}

BLYNK_WRITE(V2){
  if (param.asInt() ){
    digitalWrite(Led,HIGH);
    Izin=LOW;

  }else {
    digitalWrite(Led,LOW);
    Izin=LOW;
  }
}