#define BLYNK_TEMPLATE_ID "TMPL6bG1F4hxQ"
#define BLYNK_TEMPLATE_NAME "Lampu Otomatis"
#define BLYNK_AUTH_TOKEN "W33XxFLXFM3VI5JM7WX6At50bw2UTGHV"
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BLYNK_CONNECTED() {
Blynk.syncAll();
}
#define LDR_PIN 32
#define LED_PIN 2
const float GAMMA = 0.7; //slope of the log(R) / log(lux) graph
const float RL10 = 50; //resistance of the LDR at illumination level of 10 lux
void sendDataSensor()
{
int nilaiADC = analogRead(LDR_PIN);
float voltage = nilaiADC * 5/4095.0;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Nilai ADC = ");
Serial.println(nilaiADC);
Serial.print("Lux = ");
Serial.println(lux);
Serial.print("Tegangan = ");
Serial.println(voltage);
Blynk.virtualWrite(V0, lux);
if(lux < 200) {
digitalWrite(LED_PIN, HIGH);
}
else if(lux > 200){
digitalWrite(LED_PIN, LOW);
}
delay(2000);
}
void setup() {
Serial.begin(115200);
pinMode(LDR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, sendDataSensor);
}
void loop() {
Blynk.run();
timer.run();
}