#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "TMPL6o2vvlL9V"
#define BLYNK_TEMPLATE_NAME "Monitoring DHT22"
#define BLYNK_AUTH_TOKEN "wFC0sPlK0KHXI7kNxL7R-M4t4X3580yN"

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

int buzzer = 12;
const int DHT_PIN = 15;
int enStat, setTemp;

DHTesp dhtSensor;

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

BlynkTimer timer;
BLYNK_WRITE(V2)
{
  int enValue = param.asInt();
  enStat = enValue;
}

BLYNK_WRITE(V4)
{
  int setValue = param.asInt();
  setTemp = setValue;
}

void sendData(){
    TempAndHumidity  data = dhtSensor.getTempAndHumidity();
 
    Blynk.virtualWrite(V0, data.temperature); 
    Blynk.virtualWrite(V1, data.humidity);

    if ( enStat == HIGH && setTemp <= data.temperature)
    {
        digitalWrite(buzzer, HIGH);
        Blynk.virtualWrite(V3, HIGH);
    }
  else
    {
      digitalWrite(buzzer, LOW);
      Blynk.virtualWrite(V3, LOW);
    }

    //Serial.println(enStat);
    //Serial.println(setTemp);
    //Serial.println(data.temperature);
  }

void setup() {
  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  Blynk.begin(auth, ssid, pass);
  pinMode(buzzer, OUTPUT);
  pinMode(DHT_PIN, INPUT);
  timer.setInterval(200L, sendData);
}

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