#include <Wire.h>
#include <DHTesp.h>
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 15
#define GAS_LEVER 34
#define buzzer 13
#define TRIG_PIN 5
#define ECHO_PIN 18
DHTesp dhtsensor;
float duration_us, distance_cm;
const int motionSensor = 13; // Adjusted to a compatible pin for attachInterrupt
AccelStepper stepper(AccelStepper::DRIVER, 19, 2, 4, 23); // Adjust pins as per your setup
// Initialize the LCD display using LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the I2C address if needed
void setup() {
Serial.begin(115200);
Serial.println("ESP32 collecting sensors data");
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(motionSensor, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
dhtsensor.setup(DHTPIN, DHTesp::DHT22);
pinMode(GAS_LEVER, INPUT);
pinMode(buzzer, OUTPUT);
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.print("Sensor Data:");
}
void loop() {
TempAndHumidity data = dhtsensor.getTempAndHumidity();
float t = data.temperature;
float h = data.humidity;
float g = map(int(analogRead(GAS_LEVER)), 0, 4095, 200, 2000);
lcd.setCursor(0,0);
lcd.println("Temp: " + String(data.temperature, 2) + "*C");
lcd.setCursor(0,1);
lcd.println("Humidity: " + String(data.humidity, 1) + "%");
lcd.println("---");
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 is 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);
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);
if (g > 500 | t > 35) {
tone(buzzer, 1000);
stepper.moveTo(1000);
stepper.runToPosition();
} else {
noTone(buzzer);
}
delay(1000);
}
void detectsMovement() {
Serial.println("Motion Detected!!!");
delay(2000); // Add a delay to avoid multiple trigger events
}