#include "DHTesp.h"
#define DHTTYPE DHT22
#define LDR_PIN 4
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27,20,4);
const int DHT_PIN = 12;
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
DHTesp dhtSensor;
void setup () {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LDR_PIN, INPUT);
lcd.init();
lcd.backlight();
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
delay(1000);
int analogValue = analogRead(4);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.setCursor(0,0);
lcd.println("Date: 22/8/2022");
delay(2000);
lcd.setCursor(0,1);
lcd.println("Time: 9:30 PM");
delay(2000);
lcd.setCursor(2, 2);
lcd.print("Room: ");
if (lux > 50) {
lcd.print("Light!");
} else {
lcd.print("Dark ");
}
lcd.setCursor(2, 3);
lcd.print("Lux: ");
lcd.print(lux);
lcd.print(" ");
delay(100);
}