#include "DHTesp.h"
class Sensor {
public:
virtual void setup() = 0;
virtual void measure() = 0;
virtual ~Sensor() {}
};
class LedSensor : public Sensor {
private:
int led_pin;
public:
LedSensor(int pin) : led_pin(pin) {}
void setup() {
pinMode(led_pin, OUTPUT);
}
void measure() {
Serial.println("led working started");
digitalWrite(led_pin, HIGH);
delay(2000);
digitalWrite(led_pin, LOW);
delay(2000);
Serial.println("led working completed");
}
};
class DHTSensor : public Sensor {
private:
int DHT_PIN;
DHTesp dhtSensor;
public:
DHTSensor(int pin) : DHT_PIN(pin) {}
void setup() {
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Serial.begin(9600);
}
void measure() {
Serial.println("dht22 sensor working started");
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
delay(2000);
Serial.println("dht22 sensor working completed");
}
};
class SoilSensor : public Sensor {
private:
int soil_pin;
public:
SoilSensor(int pin) : soil_pin(pin) {}
void setup() {
pinMode(soil_pin, INPUT);
}
void measure() {
Serial.println("soil sensor working started");
int16_t value = analogRead(soil_pin);
if (value < 300) {
Serial.println(value);
Serial.println(" WET");
} else if (value > 700) {
Serial.println(value);
Serial.println("DRY");
} else {
Serial.println(value);
Serial.println("NORMAL");
}
delay(2000);
Serial.println("soil snsor working completed");
}
};
class BuzzerSensor : public Sensor {
private:
int buzzer_pin;
int button_pin;
public:
BuzzerSensor(int buzzerPin, int buttonPin) : buzzer_pin(buzzerPin), button_pin(buttonPin) {}
void setup(){
pinMode(buzzer_pin, OUTPUT);
pinMode(button_pin, INPUT);
}
void measure(){
if (digitalRead(button_pin) == HIGH) {
Serial.println("Button not pressed");
} else {
Serial.println("Button pressed. Buzzer sound simulated");
tone(buzzer_pin, 1000);
delay(1000);
noTone(buzzer_pin);
delay(100);
}
}
};
void setup() {
Serial.begin(9600);
}
void loop() {
LedSensor led(5);
DHTSensor dht(12);
SoilSensor soil(4);
BuzzerSensor buzzer(14, 15); // buzzer connected to pin 14, button connected to pin 15
led.setup();
led.measure();
dht.setup();
dht.measure();
soil.setup();
soil.measure();
buzzer.setup();
buzzer.measure();
delay(5000); // Delay between loops for better simulation
}