#define BLYNK_TEMPLATE_ID "TMPL6bG1F4hxQ"
#define BLYNK_TEMPLATE_NAME "Lampu Otomatis"
#define BLYNK_AUTH_TOKEN "W33XxFLXFM3VI5JM7WX6At50bw2UTGHV"
//Include the library files
#include <LiquidCrystal_I2C.h>
#include <BlynkSimpleEsp32.h>
#define BLYNK_PRINT Serial
//Init the LCD Display
LiquidCrystal_I2C lcd(0x27, 16,2);
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
#define LDRPin 27
#define LEDPin 2
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.setBacklight(HIGH);
pinMode(LDRPin, INPUT);
pinMode(LEDPin, OUTPUT);
Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
timer.setInterval(100L, sendSensorData);
}
void sendSensorData() {
int lightValue = analogRead(LDRPin);
float voltage = lightValue * 5/4095.0;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Nilai Cahaya: ");
Serial.println(lux);
Blynk.virtualWrite(V0, lux);
lcd.setCursor(0, 0);
lcd.print("Cahaya: ");
lcd.print(lux);
lcd.setCursor(0,1);
if(lux <= 200) {
lcd.setCursor(0,1);
digitalWrite(LEDPin, HIGH);
lcd.print("LED NYALA");
}
else if(lux > 200) {
lcd.setCursor(0,1);
digitalWrite(LEDPin, LOW);
lcd.print("LED MATI ");
}
}
void loop() {
Blynk.run();
timer.run();
}