#include <LiquidCrystal_I2C.h>
#include <DHTesp.h>
#define ldrPin 13
#define buzz 15
#define dhtPin 12
const int pinLampu = 19;
const float gama = 0.7;
const float rl10 = 50;
DHTesp sensorDHT;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
sensorDHT.setup(dhtPin, DHTesp::DHT22);
pinMode(ldrPin, INPUT);
pinMode(pinLampu, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(2, 0);
lcd.println("Welcome Faiz");
lcd.setCursor(2, 1);
lcd.print("210605110171");
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000); // this speeds up the simulation
lcd.clear();
int nilaiLDR = analogRead(ldrPin);
nilaiLDR = map(nilaiLDR, 4095, 0, 1024, 0);
float voltase = nilaiLDR / 1024.*5;
float resistansi = 2000 * voltase / (1-voltase/5);
float kecerahan = pow(rl10*1e3*pow(10,gama)/resistansi,(1/gama));
Serial.print("Kecerahan = ");
Serial.println(kecerahan);
lcd.print("Kecerahan :"+String(kecerahan));
delay(1000);
lcd.clear();
TempAndHumidity data = sensorDHT.getTempAndHumidity();
lcd.setCursor(0,0);
lcd.println("Suhu: "+String(data.temperature)+"C");
lcd.setCursor(0,1);
lcd.println("Kelembaban: "+String(data.humidity)+"%");
digitalWrite(buzz, HIGH);
delay(1000);
if(kecerahan > 400){
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Waktu Cerah.");
lcd.setCursor(0, 1);
lcd.println("lampu dimatikan!");
digitalWrite(pinLampu, LOW);
noTone(buzz);
}
else{
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Waktu Gelap.");
lcd.setCursor(0, 1);
lcd.println("Lampu dihidupkan!");
digitalWrite(pinLampu, HIGH);
tone(buzz, 100);
}
}