#include<DHT.h>
#define d1pin 4
#define d2pin 2
#define LED 21
#define TRIG1_PIN 18 // ESP32 pin GIOP23 connected to Ultrasonic Sensor's TRIG pin
#define ECHO1_PIN 19 // ESP32 pin GIOP22 connected to Ultrasonic Sensor's ECHO pin
#define TRIG2_PIN 23 // ESP32 pin GIOP23 connected to Ultrasonic Sensor's TRIG pin
#define ECHO2_PIN 22 // ESP32 pin GIOP22 connected to Ultrasonic Sensor's ECHO pin
#define DHTTYPE1 DHT22 // defining type of DHT sensor to DHT 11
#define DHTTYPE2 DHT22
DHT dht1(d1pin,DHTTYPE1); // DHT
DHT dht2(d2pin,DHTTYPE2);
float duration_us1, distance_cm1;
float duration_us2, distance_cm2;
int count = 9;
void setup() {
Serial.begin(9600);
delay(10);
dht1.begin();
dht2.begin();
// configure the trigger pin to output mode
pinMode(TRIG1_PIN, OUTPUT);
// configure the echo pin to input mode
pinMode(ECHO1_PIN, INPUT);
// configure the trigger pin to output mode
pinMode(TRIG2_PIN, OUTPUT);
// configure the echo pin to input mode
pinMode(ECHO2_PIN, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
// code to sense data using dht11
float t1=dht1.readTemperature();
float h1=dht1.readHumidity();
float t2=dht2.readTemperature();
float h2=dht2.readHumidity();
Serial.print("Temperature");
Serial.print((t1+t2)/2);
Serial.print("*C ");
Serial.print("humidity");
Serial.print((h1+h2)/2);
Serial.print("%");
Serial.print("\n");
delay(2000);
digitalWrite(TRIG1_PIN, HIGH);
digitalWrite(TRIG2_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG1_PIN, LOW);
digitalWrite(TRIG2_PIN, LOW);
// measure duration of pulse from ECHO pin
duration_us1 = pulseIn(ECHO1_PIN, HIGH);
duration_us2 = pulseIn(ECHO2_PIN, HIGH);
// calculate the distance
distance_cm1 = 0.017 * duration_us1;
distance_cm2 = 0.017 * duration_us2;
if(distance_cm1 <35 && distance_cm2 >=35){
count++;
}
else if( distance_cm1>=35 && distance_cm2 <35){
count--;
}
if(((t1+t2)/2)>=25){
digitalWrite(LED, HIGH);
}
else{
digitalWrite(LED, LOW);
}
Serial.print("No. of people in the room are ");
Serial.print(count);
Serial.print("\n");
}