#include "DHTesp.h" // Include the DHTesp library for ESP32 and DHT sensors
#define DHT_PIN 15 // Define the data pin where DHT22 is connected to ESP32 (e.g., GPIO 15)
DHTesp dht; // Create an instance of the DHTesp class to interface with the DHT sensor
void setup() {
Serial.begin(115200); // Initialize serial communication with baud rate 115200
dht.setup(DHT_PIN, DHTesp::DHT22); // Set up the DHT sensor (DHT22 type) on the defined pin
Serial.println("DHT22 Sensor Test Start"); // Output a startup message
}
void loop() {
// Read temperature and humidity values from the DHT22 sensor
TempAndHumidity data = dht.getTempAndHumidity();
// Check if the readings are valid
if (isnan(data.temperature) || isnan(data.humidity)) {
Serial.println("Failed to read from DHT22 sensor, please check the wiring!");
} else {
// Print the temperature value to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(data.temperature);
Serial.println(" °C");
// Print the humidity value to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(data.humidity);
Serial.println(" %");
Serial.println("----------------------"); // Separator for better readability
}
delay(2000); // Wait for 2 seconds before taking the next reading
}