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

#include "DHTesp.h"

const int DHT_PIN = 15;

DHTesp dhtSensor;
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPLIS1i35ro"
#define BLYNK_TEMPLATE_NAME "ESP32 Test"
#define BLYNK_AUTH_TOKEN "gLAAf8ahtSpBGM7haoP9wb-jJoq40_EA"

/* 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 = 13;
int buttonPin1 = 13;
int buttonState1 = 1;

BLYNK_WRITE(V3)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if(pinValue==1)
  digitalWrite(13, HIGH);
  
else{
  digitalWrite(13, LOW);
  
  }
}

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}

void Test()
{
  pinMode(buttonPin1, INPUT);// กำหนดขา buttonPin เป็นโหมด INPUT
  
  buttonState1 = digitalRead(buttonPin1); // อ่านค่าสถานะขา3
  if (buttonState1 == 1 ) {
    Blynk.virtualWrite(V4,1);
  } 
  else {Blynk.virtualWrite(V4,0);}
}

void loop() {

  Blynk.run();
  Test();
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  Serial.println("Temp: " + String(data.temperature, 2) + "°C");
  Serial.println("Humidity: " + String(data.humidity, 1) + "%");
  Serial.println("---");
  Blynk.virtualWrite(V0,data.temperature);
   Blynk.virtualWrite(V1,data.humidity);
  delay(1000);
}