#define BLYNK_TEMPLATE_ID "TMPL6jL3gPvCM"
#define BLYNK_TEMPLATE_NAME "kontrol lampu jarak jauh"
#define BLYNK_AUTH_TOKEN "13NrslMQ_tsVlVQfvWFyaULngR7KyU10"
#define BLYNK_PRINT Serial
#define BLYNK_WRITE

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

//set ssid wifi
char ssid[] = "Wokwi-GUEST";
char passw[] = "";

BLYNK_WRITE(V4) {
  int tombolAC = param.asInt();
  if (tombolAC == 1) {
    Blynk.virtualWrite(V5, HIGH);
    Serial.println("AC : ON");
  }
  else {
    Blynk.virtualWrite(V5, LOW);
    Serial.println("AC : OFF");
  }
}


BLYNK_WRITE (V1) //untuk lampu
{
  int tombol = param.asInt();

//kondisikan tombol dengan lampu
  if (tombol==1){
    digitalWrite(25, HIGH);
  }  
  else {
    digitalWrite(25, LOW);
  }
}

const int DHT_PIN = 14;
DHTesp dhtSensor;
float temperature = 0;
float humidity = 0;


//---------end kondisi tombol----------
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); //9600 bps bit per second
  Blynk.begin(BLYNK_AUTH_TOKEN,ssid,passw);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  pinMode(25, OUTPUT);
}

void loop() {
  Blynk.run();
   //*********DHT22, membaca suhu dan kelembaban******************
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  temperature=data.temperature;
  humidity=data.humidity;
  Blynk.virtualWrite(V2, temperature);
  Blynk.virtualWrite(V3, humidity);

  //*****SERIAL MONITOR, menampilkan data suhu dan kelembaban****
  Serial.print("Temp : ");
  Serial.print(temperature);
  Serial.println("C");
  Serial.print("Humidity : ");
  Serial.print(humidity);
  Serial.println("%");

  if(temperature>25)
  {
    digitalWrite(25, HIGH);
  }
  else 
  {
    digitalWrite(25, LOW);
  }
  delay(100);
}