#include <DHT.h>
#include <ESP32Servo.h>
#define DHT_PIN 22 // Replace with the GPIO pin connected to the DHT22 data pin
#define SERVO_PIN 27 // Replace with the GPIO pin connected to the servo motor signal pin
#define DHT_TYPE DHT22 // Change to DHT11 if you are using DHT11 sensor
DHT dht(DHT_PIN, DHT_TYPE);
Servo servoMotor;
void setup() {
Serial.begin(115200);
dht.begin();
servoMotor.attach(SERVO_PIN);
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Code to control the servo motor based on the temperature/humidity values
// Replace the threshold values and servo position according to your requirements
if (temperature > 25) {
servoMotor.write(180); // Rotate servo to a specific angle (e.g., 180 degrees)
} else {
servoMotor.write(0); // Rotate servo to another angle (e.g., 0 degrees)
}
}
delay(2000); // Wait for a few seconds before reading the sensor again
}