#include <DHTesp.h>
// Constants for DHT sensor
const int DHT_PIN = 15;
// Constants for photoresistor calibration
const float GAMMA = 0.7;
const float RL10 = 50;
// Creating an instance of the DHTesp class
DHTesp dhtSensor;
// Define the pin connected to the photoresistor
const int PHOTO_RESISTOR_PIN = 2; // Example pin, adjust according to your setup
void setup() {
// Initializing serial communication
Serial.begin(115200);
// Configuring the DHT sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
// Reading temperature and humidity from DHT sensor
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Printing temperature and humidity
Serial.print("Temperature: ");
Serial.print(data.temperature, 2); // Two decimal places for temperature
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(data.humidity, 1); // One decimal place for humidity
Serial.println("%");
// Reading analog value from photoresistor pin
int analogValue = analogRead(PHOTO_RESISTOR_PIN);
// Convert analog value to voltage (assuming 12-bit ADC and 3.3V reference)
float voltage = analogValue / 4095.0 * 3.3;
// Calculate resistance of the photoresistor using voltage divider formula
float resistance = 2000 * voltage / (3.3 - voltage);
// Calculate lux using the resistance and predefined calibration values
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// Print the lux value
Serial.print("Lux: ");
Serial.println(lux / 10);
// Adding a delay for readability
delay(2000);
}