#define BLYNK_TEMPLATE_ID "TMPL38HQcYvi8"
#define BLYNK_TEMPLATE_NAME "smart home"
#define BLYNK_AUTH_TOKEN "ZhU-Hmivi9VF2vbOLR4d9_7-4D4qUNbF"

#include <Wire.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int led1 = 2;
int led2 = 18;

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

#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

const int trig =15;
const int echo=19;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(trig,OUTPUT);
pinMode(echo, INPUT);
dht.begin();
  Blynk.begin(auth, ssid, pass);
}

BLYNK_WRITE(V0) {
  int pinValue = param.asInt();  // Corrected from param.asTnt() to param.asInt()
  digitalWrite(led1, pinValue);  // Write the value to the LED pin
}
BLYNK_WRITE(V1){
  int pinValue1=param.asInt();
  digitalWrite(led2,pinValue1);
}

void tempandsonic() {
  // put your main code here, to run repeatedly:
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig,LOW);
  float duration = pulseIn(echo,HIGH);
  int distance= duration*0.0344/2;
  float temp = dht.readTemperature();
  int humi = dht.readHumidity();
  Blynk.virtualWrite(V2,distance);
   Blynk.virtualWrite(V3,temp);
    Blynk.virtualWrite(V4,humi);
}

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