#define BLYNK_TEMPLATE_ID "TMPL6KQMMaQjM"
#define BLYNK_TEMPLATE_NAME "ac"
#define BLYNK_AUTH_TOKEN "Ln6WrlNoNFToCq0orvaKzgt8ie2gttLp"

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

#define DHTPIN 4
#define DHTTYPE DHT22
#define TRIGPIN 13
#define ECHOPIN 12

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
char auth[] = "Ln6WrlNoNFToCq0orvaKzgt8ie2gttLp";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

void setup() {
    Serial.begin(115200);
    dht.begin();

    Blynk.begin(auth, ssid, pass);
    timer.setInterval(2000L, sendSensorData);
}

void sendSensorData() {
    float temp = dht.readTemperature();
    Blynk.virtualWrite(V2, temp);

    // Tampilkan suhu ruangan pada konsol
    Serial.print("Suhu Ruangan: ");
    Serial.print(String (temp) + " °C");
}

long getDistance() {
    digitalWrite(TRIGPIN, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIGPIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIGPIN, LOW);
    long duration = pulseIn(ECHOPIN, HIGH);
    long distance = (duration/2) / 29.1;
    return distance;
}

BLYNK_WRITE(V0) {
    int buttonState = param.asInt();
    if (buttonState) {
        Blynk.virtualWrite(V1, HIGH);
        Serial.println("AC HIDUP");
    } else {
        Blynk.virtualWrite(V1, LOW);
        Serial.println("AC MATI");
    }
}

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