/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

/* Fill-in information from Blynk Device Info here */

#define BLYNK_TEMPLATE_ID "TMPL65Dd1EU8s"
#define BLYNK_TEMPLATE_NAME "WEMOS D1 MINI"
#define BLYNK_AUTH_TOKEN "zwUC-7buJXWlF0Yl_pxUIrQpvGMM9cA7"
#include "DHTesp.h"

const int DHT_PIN = 15;
DHTesp dhtSensor;
int suhu;
int kelembaban;

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
int led1=2;
int led2=4;
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  suhu=data.temperature;
  kelembaban=data.humidity;
  Blynk.virtualWrite(V2, suhu);
  Blynk.virtualWrite(V3, kelembaban);
  Serial.println("Temp: " + String(data.temperature, 2) + "°C");
  Serial.println("Humidity: " + String(data.humidity, 1) + "%");
  Serial.println("---");
  }

void setup() {
  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);

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

void loop() {
  
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  suhu = data.temperature;
  if (suhu >= 30)
  {
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);

    }
  else if (suhu >=25){
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);

    }
  else if (suhu<25){
  digitalWrite(led2, HIGH);
  digitalWrite(led1, LOW);

 
  }
  
  
}