#define BLYNK_TEMPLATE_ID "TMPL3sEFkpMVI"
#define BLYNK_TEMPLATE_NAME "Office Lighting System"
#define BLYNK_AUTH_TOKEN "ms_E6IPSXChfNbkVxcMNjPc3_bgN7b-J"
#define BLYNK_PRINT Serial
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h> // Include Blynk library for ESP32
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
BlynkTimer timer;
float lux;
int kondisiRelay1;
int kondisiRelay2;
int pirValue;
int ldrValue;
const int RelayPin1 = 18;
const int RelayPin2 = 15;
const float GAMMA = 0.7;
const float RL10 = 50;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int ldrPin = 34;
const int pirPin = 2;
#define timeSeconds 10
unsigned long lastTrigger = 0;
char auth[] = "ms_E6IPSXChfNbkVxcMNjPc3_bgN7b-J"; // Replace with your Blynk auth token
char ssid[] = "Wokwi-GUEST"; // Replace with your WiFi SSID
char pass[] = ""; // Replace with your WiFi password
bool blynkRelayState1 = false; // Track relay state from Blynk command
bool blynkRelayState2 = false; // Track relay state from Blynk command
void kirimdata(){
Blynk.virtualWrite(V1, lux);
Blynk.virtualWrite(V2, kondisiRelay1);
Blynk.virtualWrite(V3, kondisiRelay2);
}
void setup() {
pinMode(ldrPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
// Set initial relay states to OFF
//digitalWrite(RelayPin1, LOW);
//digitalWrite(RelayPin2, LOW);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, kirimdata);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 17);
display.println("LUX Meter");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(28, 40);
display.println("by : Husein");
display.display();
delay(5000);
}
void loop() {
Blynk.run(); // Allow Blynk to handle communication
timer.run();
pirValue = digitalRead(pirPin);
ldrValue = analogRead(ldrPin);
kondisiRelay1 = digitalRead(RelayPin1);
kondisiRelay2 = digitalRead(RelayPin2);
float voltage = ldrValue / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// ... (sensor readings)
// OLED display update
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(33, 28);
display.println("LUX : ");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(62, 28);
display.println(lux);
display.display();
// Check if Blynk has sent an ON command
if (!blynkRelayState1) {
digitalWrite(RelayPin1, HIGH); // Turn on the relay
Serial.println("Blynk Command: LED 1 ON");
} else {
// LED control based on Lux and PIR
if (lux < 300 && pirValue == HIGH) {
digitalWrite(RelayPin1, HIGH); // Turn on the relay
Serial.println("Motion Detected! LED 1 ON");
}
// LED turn-off condition
else if (lux > 600 || (pirValue == LOW && millis() - lastTrigger > (timeSeconds * 1000))) {
digitalWrite(RelayPin1, LOW); // Turn off the relay
Serial.println("LED 1 OFF");
}
if (pirValue == HIGH){
lastTrigger = millis();
}
delay(500); // Delay for stability
}
if (!blynkRelayState2) {
digitalWrite(RelayPin2, HIGH); // Turn on the second relay
Serial.println("Blynk Command: LED 2 ON");
} else {
// LED control based on Lux and PIR for the second relay
if (lux < 100 && pirValue == HIGH) {
digitalWrite(RelayPin2, HIGH); // Turn on the second relay
Serial.println("Motion Detected! LED 2 ON");
}
else if (lux > 1000 || (pirValue == LOW && millis() - lastTrigger > (timeSeconds * 1000))) {
digitalWrite(RelayPin2, LOW); // Turn off the second relay
Serial.println("LED 2 OFF");
}
if (pirValue == HIGH) {
lastTrigger = millis();
}
delay(500); // Delay for stability
}
}
BLYNK_WRITE(V0) { // Blynk virtual pin for relay control
int relayControl = param.asInt(); // Get the value from the Blynk app
if (relayControl == 1) {
// Set the relay state from Blynk command
blynkRelayState1 = true;
Serial.println("Blynk Command: LED 1 ON");
}
else if (relayControl == 0) {
// Set the relay state from Blynk command
blynkRelayState1 = false;
Serial.println("Blynk Command: LED 1 OFF");
digitalWrite(RelayPin1, LOW);
Serial.println("LED 1 OFF");
}
}
BLYNK_WRITE(V4) { // Blynk virtual pin for the second relay control
int relayControl = param.asInt(); // Get the value from the Blynk app
if (relayControl == 1) {
// Set the relay state from Blynk command for the second relay
blynkRelayState2 = true;
Serial.println("Blynk Command: LED 2 ON");
} else if (relayControl == 0) {
// Set the relay state from Blynk command for the second relay
blynkRelayState2 = false;
Serial.println("Blynk Command: LED 2 OFF");
digitalWrite(RelayPin2, LOW);
Serial.println("LED 2 OFF");
}
}