#define BLYNK_TEMPLATE_ID "TMPLzElfAnWi"
#define BLYNK_DEVICE_NAME "dht22"
#define BLYNK_AUTH_TOKEN "W3zfKcUwOnuc1YwANvF_kq7QqmVeixDH"
#define led_merah 13

char auth[] =  "W3zfKcUwOnuc1YwANvF_kq7QqmVeixDH";

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


//Membaca Virtual Pin dari Server Blynk
BLYNK_CONNECTED() {
   Blynk.syncVirtual(V2); //Sesuaikan dengan Virutal Pin yg dipakai
}
bool toggleState_1 = 0;
BLYNK_WRITE(V2) { //Sesuaikan dengan Virutal Pin yg dipakai
  toggleState_1 = param.asInt();
  if(toggleState_1 == 1){
    digitalWrite(13, HIGH);
  }
  else { 
    digitalWrite(13, LOW);
  }
}

#define LED 2
#define BLYNK_PRINT Serial

const int DHT_PIN = 15;
DHTesp dhtSensor;

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

BlynkTimer timer;

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(V0, data.temperature); 
 Blynk.virtualWrite(V1, data.humidity);//mengirimkan data ke Virtual pin di Blynk Cloud
 digitalWrite(LED, HIGH);
 delay(100);
}

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

void loop()
{
  Blynk.run();
  timer.run();
  digitalWrite(LED, LOW);
}