/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include "DHTesp.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <LiquidCrystal_I2C.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SOUND_SPEED 0.034
#define distance_ths 100
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHTesp dhtSensor;
const int DHT_PIN = 15;
const int Trig_1=18;
const int Echo_1=19;
const int Trig_2=4;
const int Echo_2=5;
float distance1;
float distance2;
long duration;
int contador_pessoas;
double S1=0;
double S2=0;
int ledPin = 2; // choose the pin for the LED
int inputPin = 27; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0;
int LDR_PIN =25;
void cont_pessoas(){
// Clears the trigPin
digitalWrite(Trig_1, LOW);
digitalWrite(Trig_2, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(Trig_1, HIGH);
digitalWrite(Trig_2, HIGH);
delayMicroseconds(20);
digitalWrite(Trig_1, LOW);
duration = pulseIn(Echo_1, HIGH);
distance1= duration * SOUND_SPEED/2;
digitalWrite(Trig_2, LOW);
duration = pulseIn(Echo_2, HIGH);
distance2=duration * SOUND_SPEED/2;
if(distance1<distance_ths && S1==0){
S1=millis();
}
if(distance2< distance_ths && S2==0){
S2=millis();
}
if(S1>0 || S2>0){
if(S1<S2 && S1>0){ //Entrada
contador_pessoas++;
//Reset variaveis
S1=0;
S2=0;
}
if(S2<S1 && S2>0){ //Saida
contador_pessoas--;
if (contador_pessoas <=0){
contador_pessoas=0;
}
//Reset variaveis
S1=0;
S2=0;
}
//reset caso seja falso positivo
double act_time=millis();
if(S1+10000<act_time){
S1=0;
}
if(S2+10000<act_time){
S2=0;
}
}
Serial.println("Distancia1: " + String(distance1,5 ));
Serial.println("Distancia2: " + String(distance2,5));
Serial.print("Pessoas: ");
Serial.println(contador_pessoas);
delay(1000);
}
void humidity_and_light(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.print("Room: ");
if(digitalRead(LDR_PIN) == LOW) {
Serial.println("Light!");
} else {
Serial.println("Dark");
}
Serial.println("---");
delay(10);
}
void sensor_luz(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
delay(10);
}
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(Trig_1, OUTPUT); // Sets the trigPin as an Output
pinMode(Echo_1, INPUT); // Sets the echoPin as an Input
pinMode(Trig_2, OUTPUT); // Sets the trigPin as an Output
pinMode(Echo_2, INPUT); // Sets the echoPin as an Input
pinMode(LDR_PIN, INPUT);
Serial.begin(9600); // Starts the serial communication
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println("Hello, world!");
display.display();
}
void loop() {
humidity_and_light();
sensor_luz();
cont_pessoas();
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 10);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
display.print("Temperatura: ");
display.println(data.temperature);
display.println();
display.print("Humidade: ");
display.println(data.humidity);
display.println();
display.print("Num pessoas: ");
display.println(contador_pessoas);
display.println("");
display.setTextSize(2);
display.display();
delay(100);
}