#define BLYNK_TEMPLATE_ID "TMPL6KCkRYKAb"
#define BLYNK_TEMPLATE_NAME "Duy"
#define BLYNK_AUTH_TOKEN "fDtPbQ6Axa57HMT3FMfW3tAwJ8iImXOz"
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHTesp.h> //Library untuk DHT

char auth[] = BLYNK_AUTH_TOKEN ; //Auth Token

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

const int DHT_PIN = 13;

byte LED_R = 5;
byte LED_G = 2;


DHTesp dht;

BlynkTimer timer;

//function untuk pengiriman sensor
void sendSensor()
{
 TempAndHumidity  data = dht.getTempAndHumidity();

//menampilkan temperature pada Serial monitor
Serial.print("% nhiet do: ");
Serial.print(data.temperature);
Serial.println("C ");
Serial.print("% do am: ");
Serial.print(data.humidity);
Serial.println("% ");

Blynk.virtualWrite(V2, data.temperature); //mengirimkan data temperatur ke Virtual pin VO di Blynk Cloud
Blynk.virtualWrite(V3, data.humidity); //mengirimkan data kelemaban ke Virtual pin V1 di Blynk Cloud
}

BLYNK_WRITE(V0)
{
  int pinValue = param.asInt();
  if(pinValue == 1)
  {
  digitalWrite(LED_R, HIGH);}
  else{
    digitalWrite(LED_R, LOW);
  }

}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();
  if(pinValue == 1)
  {
  digitalWrite(LED_G, HIGH);}
  else
  {
    digitalWrite(LED_G, LOW);
  }

}

void setup()
{
// Debug console
Serial.begin(115200); //serial monitor menggunakan bautrate 9600
dht.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);

Blynk.begin(auth, ssid, pass); //memulai Blynk
timer.setInterval(1000, sendSensor); //Mengaktifkan timer untuk pengiriman data 1000ms
}

void loop()
{

Blynk.run(); //menjalankan blynk
timer.run(); //menjalankan timer
}