#define BLYNK_PRINT Serial //harus taro atas

#define BLYNK_TEMPLATE_ID   "TMPLwMvbvLR8"
#define LED 2
#define REL 13

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h> //tambah liblaries
#include "DHTesp.h" //tambah liblaries

const int DHT_PIN = 15;
DHTesp dhtSensor;

char auth[] = "QpZ_2eCuzfOrZLEl42WKjxKZggubfah_";

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

BlynkTimer timer;

BLYNK_WRITE(V1) //menyalakan LED
{
 int pinValue = param.asInt();
 digitalWrite(LED,pinValue);
}

BLYNK_WRITE(V3) //menyalakan RELAY
{
 int pinValue = param.asInt();
 digitalWrite(REL,pinValue);
}

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); //mengirimkan data ke Virtual pin di Blynk Cloud
 Blynk.virtualWrite(V2, data.humidity); //mengirim data ke V2 (Kelembaban)
}

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

void loop()
{
  Blynk.run();
  timer.run();
}