#include <WiFi.h>
#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
#define TRIG_PIN 5 // ESP32 pin GPIO23 connected to Ultrasonic Sensor's TRIG pin
#define ECHO_PIN 18 // ESP32 pin GPIO22 connected to Ultrasonic Sensor's ECHO pin
#define LED_PIN 32
#define DHT_PIN 15
#define timeSeconds 10
DHTesp dhtSensor;
float duration_us, distance_cm;
const int motionSensor = 13;
const int led = 26;
// Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;
boolean motion = false;
// Checks if motion was detected, sets LED HIGH and starts a timer
void IRAM_ATTR detectsMovement() {
digitalWrite(led, HIGH);
startTimer = true;
lastTrigger = millis();
}
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
//setup for serial communication
Serial.begin(9600);
Serial.print("ESP32 collecting sensors data");
// configure the trigger pin to output mode
pinMode(TRIG_PIN, OUTPUT);
// configure the echo pin to input mode
pinMode(ECHO_PIN, INPUT);
//setup for dht sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
//config LED_PIN output
pinMode(LED_PIN, OUTPUT);
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
// Set LED to LOW
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Hello, world!");
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
distance_cm = (duration / 2) / 29.09;
Serial.println(distance_cm);
if(distance_cm >= 100){
Serial.println("the tank is full ");
}
else if(distance_cm >= 50 && distance_cm < 100){
Serial.println(("the tank if at 50 % of capacity "));
}
else if(distance_cm >=20 && distance_cm < 50 ){
Serial.println(("the tank is near to empty"));
}
else{
Serial.println(("the tank is empty"));
}
delay(3000);
// Current time
now = millis();
if((digitalRead(led) == HIGH) && (motion == false)) {
Serial.println("MOTION DETECTED!!!");
motion = true;
}
// Turn off the LED after the number of seconds defined in the timeSeconds variable
if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
Serial.println("Motion stopped...");
digitalWrite(led, LOW);
startTimer = false;
motion = false;
}
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: " + String(data.temperature, 2) + "C");
lcd.setCursor(0,1);
lcd.print("Humidity: " + String(data.humidity, 2) + "%");
delay(2000);
digitalWrite(LED_PIN, HIGH);
delay(500); // TODO: Build something amazing!
digitalWrite(LED_PIN, LOW);
delay(500);
}