#define BLYNK_TEMPLATE_ID "TMPL2H8r7eZqi"
#define BLYNK_TEMPLATE_NAME "Test"
#define BLYNK_AUTH_TOKEN "d6Y97PPfpGL33ePMvwhb35tRtrk_GmwF"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
#define BLYNK_PRINT Serial
const int DHT_PIN = 15;
unsigned long timeStart=0;
DHTesp dhtSensor;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
unsigned long timestart=0;
unsigned long previousMillis=0;
unsigned long i=0;
unsigned int potValue=0;
BlynkTimer timer;

BLYNK_CONNECTED(){
 // Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  //Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  //Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
  Blynk.virtualWrite(V3, 1);	// best practice
  digitalWrite(2, HIGH);
}

BLYNK_WRITE(V1) {
  Serial.println("Send Message");
  digitalWrite(4, param.asInt());
}
BLYNK_WRITE(V2)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  Serial.println("PinValue = "+String(pinValue));
  // process received value
}
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V3, millis() / 1000);
    Serial.println("PinValue = "+String(analogRead(34)));
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
        /*Serial.println("Temp: " + String(data.temperature, 2) + "°C");
        Serial.println("Humidity: " + String(data.humidity, 1) + "%");
       */Blynk.virtualWrite(V4,data.temperature);
       Blynk.virtualWrite(V5,data.humidity);
  
}

void setup() {
  // put your setup code here, to run once:
 pinMode(2, OUTPUT);
 pinMode(4, OUTPUT);
  digitalWrite(2,LOW);
    dhtSensor.setup(DHT_PIN, DHTesp::DHT22);

  Serial.begin(115200);
  Serial.println("Hello, ESP32!");

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 8080);
    
    timestart=millis();
    previousMillis=millis();
    timer.setInterval(1000L, myTimerEvent);
}

void loop() {
  bool result = Blynk.connected();
        
      // put your main code here, to run repeatedly:
     // Serial.println("Connected");
      /*if(millis()-timestart>2000){
          Blynk.virtualWrite(V3, i);	// best practic
          timestart=millis();
          i++;
      }*/
      /*if ((WiFi.status() != WL_CONNECTED) && (millis() - previousMillis >=5000)) {
          digitalWrite(2, LOW);
          Serial.print(millis());
          Serial.println("Reconnecting to WiFi...");
          WiFi.disconnect();
          WiFi.reconnect();
          //previousMillis = millis();
          Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 8080);
      }*/
      Blynk.run();
    timer.run(); // Initiates BlynkTimer


}