#include "DHTesp.h"

const int DHT_PIN = 15;
const int d_buttonPin = 21;

DHTesp dhtSensor; //instance cretation
int temp;
int hum;

#define BLYNK_TEMPLATE_ID "TMPL6NsbACGX0"
#define BLYNK_TEMPLATE_NAME "Temperature Humidity Monitor"
#define BLYNK_AUTH_TOKEN "cv5-VIIdK0oBnn91ZpFfpGqz-hwB7A08"



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

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

#define RELAY1_PIN 25
#define VIRTUAL_RELAY1 V2
 #define VIRTUAL_TEMP V0
#define VIRTUAL_HUM V1
#define VIRTUAL_DOOR V3


BlynkTimer timer;

BLYNK_WRITE(VIRTUAL_RELAY1) {
  int value = param.asInt();
  digitalWrite(RELAY1_PIN, value);
  Serial.println(value ? "Relay 1 ON (LED 1)" : "Relay 1 OFF (LED 1)");
}

void myTimerEvent() {
  TempAndHumidity data = dhtSensor.getTempAndHumidity();
  temp = data.temperature;
  hum = data.humidity;

  Blynk.virtualWrite( VIRTUAL_TEMP, temp);
   Blynk.virtualWrite(VIRTUAL_HUM , hum);

  Serial.println("Temp: " + String(data.temperature, 2) + "°C");
  Serial.println("Humidity: " + String(data.humidity, 1) + "%");
   Serial.println("---");

  // if (temp >= 37) {
  //   digitalWrite(RELAY1_PIN, LOW);
  // } else if (temp < 30) {
  //   digitalWrite(RELAY1_PIN, HIGH);
  // }

  int buttonState = digitalRead(d_buttonPin);  // Read the button state
  if (buttonState == HIGH) {  // Button pressed (LOW due to pull-up)
    Blynk.virtualWrite(VIRTUAL_DOOR,"door open"); 
    Serial.println("door open");
  } 
  else{
     Blynk.virtualWrite(VIRTUAL_DOOR,"button pressed door closed"); //door open
     Serial.println("button pressed door closed"); 
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(RELAY1_PIN, OUTPUT);
  pinMode(d_buttonPin, INPUT_PULLUP); 
  digitalWrite(RELAY1_PIN, LOW);


  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
}

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