#include <DHTesp.h>
DHTesp dhtsensor;
TempAndHumidity data;
const int dhtpin = 12;
const int trigpin = 25;
const int echopin = 33;
int duration;
int distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(2, OUTPUT);
dhtsensor.setup(dhtpin,DHTesp::DHT22);
}
void loop() {
if (distance<200){
digitalWrite(2, HIGH);
}
else{
digitalWrite(2, LOW);
}
distance = getdistance();
Serial.println("distance"+String(distance));
delay(1000); //this speed up the simulation
data = dhtsensor.getTempAndHumidity();
Serial.println("Temp "+String(data.temperature));
Serial.println("Humidity "+String(data.humidity));
Serial.println("-------------");
delay(1000);
}
long getdistance(){
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
return(duration*0.034/2)+1;
}