#include <ESP32Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
Servo servo;
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(128, 64, &Wire, -1);
#define DHT_PIN 13 // GPIO Pin for DHT sensor
#define DHT_TYPE DHT22 // DHT sensor type, choose DHT11, DHT22, or DHT21
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
servo.attach(18); // GPIO Pin for Servo
servo.write(0);
delay(2000);
Serial.begin(9600);
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display(); // Display startup message
delay(2000);
display.clearDisplay();
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Reads the temperature and humidity from the DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Prints the temperature and humidity on the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
display.clearDisplay();
display.setTextSize(1); // Adjust text size
display.setTextColor(SSD1306_WHITE);
// Rotate the servo motor in a 360-degree loop when humidity reaches 75 percent and above
if (humidity >= 75.0) {
for (int angle = 0; angle <= 360; angle += 10) {
servo.write(angle);
delay(50); // Adjust the delay as needed
}
} else {
servo.write(0);
}
display.setCursor(0, 0); // Adjust text position
display.print("Temp: ");
display.print(temperature);
display.setCursor(0, 10); // Move to the next line
display.print("Humid: ");
display.print(humidity);
display.print("%");
display.display();
delay(0);
}