#define BLYNK_TEMPLATE_ID "TMPL6SerM3IJ6"
#define BLYNK_TEMPLATE_NAME "Pintu Otomatis"
#define BLYNK_AUTH_TOKEN "S4xiXSsHJDXPkgDhoIWcC9ruKUZ6Z8bl"
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// BlynkTimer timer;
// Blynk
char auth[] = "S4xiXSsHJDXPkgDhoIWcC9ruKUZ6Z8bl";
char ssid[] = "Wokwi-GUEST"; // Ganti dengan SSID WiFi Anda
char pass[] = ""; // Ganti dengan Password WiFi Anda
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS1307 rtc;
// Potensio
const int potensio_pin1 = 14;
const int potensio_pin2 = 12;
int potensio_value1;
int potensio_value2;
int open_time;
int close_time;
const int motor_pin = 5;
const int red_LED = 19;
const int green_LED = 18;
int hour;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Inisialisasi Blynk
Blynk.begin(auth, ssid, pass);
Serial.println("Hello, ESP32!");
if(!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
delay(2000);
oled.clearDisplay(); // clear display
oled.setTextSize(1); // Atur ukuran text
oled.setTextColor(WHITE); // Atur warna text
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
DateTime now = rtc.now();
hour = now.hour();
pinMode(potensio_pin1, INPUT);
pinMode(potensio_pin2, INPUT);
pinMode(red_LED, OUTPUT);
pinMode(green_LED, OUTPUT);
pinMode(motor_pin, OUTPUT);
check();
}
void check() {
DateTime now = rtc.now();
int current_hour = now.hour();
if (current_hour >= open_time && current_hour < close_time) {
digitalWrite(red_LED, LOW);
digitalWrite(green_LED, HIGH);
oled.print("Buka");
} else {
digitalWrite(green_LED, LOW);
digitalWrite(red_LED, HIGH);
oled.print("Tutup");
}
// int red_LED_state = digitalRead(red_LED);
// int green_LED_state = digitalRead(green_LED);
// Blynk.virtualWrite(V2, red_LED_state);
// Blynk.virtualWrite(V3, green_LED_state);
}
void print_time() {
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
oled.setCursor(0, 56);
oled.print("Jam : ");
oled.print(hour);
oled.print(":");
oled.print(minute);
oled.print(":");
oled.print(second);
}
void print_status() {
int is_open = digitalRead(green_LED);
oled.setCursor(0, 43);
oled.print("Status >>> ");
if (is_open) {
oled.print("Buka");
} else {
oled.print("Tutup");
}
}
void potensio_check() {
int current_potensio1 = analogRead(potensio_pin1);
int current_potensio2 = analogRead(12);
int potensio_24 = 178.04;
open_time = current_potensio1/potensio_24 + 1;
close_time = current_potensio2/potensio_24 + 1;
oled.setCursor(0, 0);
if(open_time < 10) {
oled.println("Jam pintu dibuka :");
oled.setCursor(0, 10);
oled.print("=> 0");
oled.print(open_time);
oled.print(":00");
} else {
oled.print("Jam pintu dibuka :");
oled.setCursor(0, 10);
oled.print("=> ");
oled.print(open_time);
oled.print(":00");
}
oled.setCursor(0, 20);
if(close_time < 10) {
oled.print("Jam pintu ditutup :");
oled.setCursor(0, 30);
oled.print("=> 0");
oled.print(close_time);
oled.print(":00:00");
} else {
oled.print("Jam pintu ditutup :");
oled.setCursor(0, 30);
oled.print("=> ");
oled.print(close_time);
oled.println(":00:00");
}
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
timer.run();
DateTime now = rtc.now();
print_time();
print_status();
potensio_check();
check();
int current_hour = now.hour();
if (hour != current_hour) {
check();
hour = current_hour;
}
oled.display();
delay(1000); // this speeds up the simulation
oled.clearDisplay();
}