/**
   ESP32 + DHT22 Example for Wokwi
   
   https://wokwi.com/arduino/projects/322410731508073042
*/

#include "DHTesp.h"

const int DHT_PIN = 15;

DHTesp dhtSensor;
int temp;
int hum;

//blynk
/*************************************************************

  This sketch shows how to write values to Virtual Pins

  NOTE:
  BlynkTimer provides SimpleTimer functionality:
    http://playground.arduino.cc/Code/SimpleTimer

  App dashboard setup:
    Value Display widget attached to Virtual Pin V5
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
// #define BLYNK_TEMPLATE_ID "TMPL6t5sk_Oeg"
// #define BLYNK_TEMPLATE_NAME "Lora Gateway"
// #define BLYNK_AUTH_TOKEN "R15XxlH6-mdz2Q4EYf6CymqgXVCYJBnl"

#define BLYNK_TEMPLATE_ID "TMPL6NsbACGX0"
#define BLYNK_TEMPLATE_NAME "temprature humidity monitor"
#define BLYNK_AUTH_TOKEN "cv5-VIIdK0oBnn91ZpFfpGqz-hwB7A08"

/* 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[] = "";

#define RELAY1_PIN 25 
#define VIRTUAL_RELAY1 V2 

BlynkTimer timer;

BLYNK_WRITE(VIRTUAL_RELAY1) {
 
  int value = param.asInt();  // Get the value from the Blynk app (0 or 1)
  digitalWrite(RELAY1_PIN, value);  // Set relay state based on Blynk input
  Serial.println(value ? "Relay 1 ON (LED 1)" : "Relay 1 OFF (LED 1)");
}


// 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();
  temp=data.temperature;
  hum=data.humidity;
  Blynk.virtualWrite(V0, temp);
  Blynk.virtualWrite(V1, hum);
  Serial.println("Temp: " + String(data.temperature, 2) + "°C");
  Serial.println("Humidity: " + String(data.humidity, 1) + "%");
  Serial.println("---");
  
}



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

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

void loop() {
  
  Blynk.run();
 timer.run(); // Initiates BlynkTimer

//  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
//   TempAndHumidity  data = dhtSensor.getTempAndHumidity();
//   temp=data.temperature;
//   hum=data.humidity;
//   Blynk.virtualWrite(V0, temp);
//   Blynk.virtualWrite(V1, hum);
//   Serial.println("Temp: " + String(data.temperature, 2) + "°C");
//   Serial.println("Humidity: " + String(data.humidity, 1) + "%");
//   Serial.println("---");
//   delay(1000);
  
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
led1:A
led1:C
NOCOMNCVCCGNDINLED1PWRRelay Module
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO