// Include the DHTesp library
// This library is optimized for ESP32 and ESP8266 boards
#include "DHTesp.h"
// Create an object named dhtSensor to control the DHT sensor
DHTesp dhtSensor;
// Create a variable 'data' to store temperature and humidity values
// TempAndHumidity is a structure provided by the DHTesp library
TempAndHumidity data;
// Define the GPIO pin number where the DHT22 data pin is connected
const int DHT_PIN = 15;
void setup() {
// Start serial communication at 115200 baud rate
// Used to display temperature and humidity values on Serial Monitor
Serial.begin(115200);
// Initialize the DHT sensor
// DHT_PIN → GPIO pin number
// DHTesp::DHT22 → specifies the sensor type as DHT22
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
// Read both temperature and humidity from the DHT sensor
// The values are stored in the 'data' structure
data = dhtSensor.getTempAndHumidity();
// Print humidity value with 1 decimal place
Serial.println("Humidity: " + String(data.humidity, 1));
// Print temperature value with 2 decimal places
Serial.println("Temperature: " + String(data.temperature, 2));
// Print a separator line for better readability
Serial.println("***-----***");
// Delay of 1 second before next reading
// DHT22 requires a minimum delay between readings
delay(1000);
}