#include <DHT.h>
#define DHT_PIN 26 // Pin to which the DHT sensor is connected
#define DHT_TYPE DHT22 // Change to DHT11 if you are using that sensor
#define RED_LED_PIN 22 // GPIO pin for the red LED
#define BLUE_LED_PIN 23 // GPIO pin for the blue LED
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
dht.begin();
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
}
void loop() {
// Measure temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if the readings are valid
if (!isnan(temperature) && !isnan(humidity)) {
// Print the readings
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Check temperature thresholds
if (temperature > 40.0 || temperature < 20.0) {
// Blink the red LED
digitalWrite(RED_LED_PIN, HIGH);
delay(1000);
digitalWrite(RED_LED_PIN, LOW);
}
// Check humidity threshold
if (humidity > 80.0) {
// Blink the blue LED
digitalWrite(BLUE_LED_PIN, HIGH);
delay(1000);
digitalWrite(BLUE_LED_PIN, LOW);
}
// Wait for 3 minutes (180000 milliseconds)
delay(1800);
} else {
Serial.println("Failed to read from DHT sensor. Check the wiring.");
}
}