#define BLYNK_TEMPLATE_ID "TMPLX8HAwhVr"
#define BLYNK_DEVICE_NAME "BlynkESP32"
#define BLYNK_AUTH_TOKEN "v-3VOmdSlL2Lq7oZyRt2E0FlU5PMMWNX"

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define BLYNK_PRINT Serial

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

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

#include <DHTesp.h>
#define DHT_PIN 15
DHTesp dhtSensor;
float t,h;

#define BUTTON1 V1
#define BUTTON2 V2
#define BUTTON3 V3
#define BUTTON4 V4
#define LED1 23  
#define LED2 19
#define LED3 4  
#define LED4 2

BlynkTimer timer;
BLYNK_WRITE(BUTTON1){
  digitalWrite(LED1,param.asInt());
  lcd.setCursor(1,3);
  lcd.print(param.asInt());
}
BLYNK_WRITE(BUTTON2){
  digitalWrite(LED2,param.asInt());
  lcd.setCursor(6,3);
  lcd.print(param.asInt());
}
BLYNK_WRITE(BUTTON3){
  digitalWrite(LED3,param.asInt());
  lcd.setCursor(11,3);
  lcd.print(param.asInt());
}
BLYNK_WRITE(BUTTON4){
  digitalWrite(LED4,param.asInt());
  lcd.setCursor(16,3);
  lcd.print(param.asInt());
}

void readDHT()
{
  TempAndHumidity data = dhtSensor.getTempAndHumidity();
  t = data.temperature;
  h = data.humidity;
  //Serial.println(t);
  Blynk.virtualWrite(V5,t);
  Blynk.virtualWrite(V6,h);
}

void setup() {
  Serial.begin(9600);
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(LED3,OUTPUT);
  pinMode(LED4,OUTPUT);
  lcd.init();
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(5000L, readDHT);
}

void loop() {
  Blynk.run();
  timer.run();
  lcd.setCursor(0,0);
  lcd.print("T:");
  lcd.print(t);
  lcd.print("*C");
  lcd.print("--H:");
  lcd.print(h);
  lcd.print("%");
  lcd.setCursor(0,2);
  lcd.print("LED1 LED2 LED3 LED4");
  delay(500);
}