#include "DHT.h"
// Pin connected to DHT22 data pin
#define DHT_PIN 4
// DHT sensor type
#define DHT_TYPE DHT22
// Number of readings per interval (10 seconds / 200ms = 50 readings)
#define READINGS_PER_INTERVAL 10
// Initialize DHT sensor
DHT dht(DHT_PIN, DHT_TYPE);
// Variables to store temperature statistics
float minTemp = 1000.0; // Minimum temperature, initialized to a high value
float maxTemp = -1000.0; // Maximum temperature, initialized to a low value
float sumTemp = 0.0; // Sum of temperatures
float sqSumTemp = 0.0; // Sum of squares of temperatures
int count = 0; // Count of temperature readings
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize DHT sensor
dht.begin();
}
void loop() {
// Read temperature from DHT sensor
float temp = dht.readTemperature();
// Print current temperature
Serial.print("Temperature Value: ");
Serial.println(temp);
// Update temperature statistics
minTemp = min(minTemp, temp);
maxTemp = max(maxTemp, temp);
sumTemp += temp;
sqSumTemp += temp * temp;
count++;
// If enough readings have been taken for this interval
if (count >= READINGS_PER_INTERVAL) {
// Calculate average and standard deviation
float avgTemp = sumTemp / count;
float variance = (sqSumTemp / count) - (avgTemp * avgTemp);
float stdDev = sqrt(variance);
// Print temperature statistics
Serial.print("Min Temperature: ");
Serial.println(minTemp);
Serial.print("Max Temperature: ");
Serial.println(maxTemp);
Serial.print("Average Temperature: ");
Serial.println(avgTemp);
Serial.print("Standard Deviation: ");
Serial.println(stdDev);
// Reset temperature statistics for next interval
minTemp = 1000.0;
maxTemp = -1000.0;
sumTemp = 0.0;
sqSumTemp = 0.0;
count = 0;
}
// Wait before next reading
delay(1000);
}