// this programme is to read the temperature and humidity using a DHT22 sensor
#include <DHTesp.h> // To import DHT22 header file
int DHT_PIN = 21; // Assign GPIO 21 to DHT22 sensor
int LED_PIN = 14; // Assign GPIO 14 to LED
DHTesp dhtSensor; // create an instance from DHTesp library
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // Open serial port, sets data rate to 9600bps
pinMode(DHT_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); // To declare the sensor will be attached in which pin and sensor model
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity(); // To get the temperature and humidity from the sensor
Serial.println("Temp: " + String(data.temperature, 2) + "°C"); // Print the temperature
Serial.println("Humidity: " + String(data.humidity, 2) + "%"); // Print the humidity
Serial.println("---");
if (data.temperature >= 40){
digitalWrite(LED_PIN, HIGH);
delay(1000); // This speeds up simulation
}
else {
digitalWrite(LED_PIN, LOW);
delay(2000); // Wait for new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}
}