#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for I2C LCD
#include <RTClib.h> // Library for Real-time Clock
#include <WiFi.h> // Library for WiFi functionality
#include <HTTPClient.h> // Library for HTTP communication
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Accessing LCD address
const int BalkonDanTerasRelay = 19; // Pin D13 for balcony and terrace lights relay
const int RuangTengahDanKamarRelay = 18; // Pin D12 for living room and bedroom lights relay
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Google Apps Script web app URL
const char* webAppUrl = "https://script.google.com/a/macros/mhs.univbinainsan.ac.id/s/AKfycbxB8IkFYwMgmHq9FiTH0-knYGtPLwTbuc7uPC5rBZTBSBh4-Dbj-VJUw26FaY4NvWSV/exec";
void setup() {
Serial.begin(115200);
delay(100);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
pinMode(BalkonDanTerasRelay, OUTPUT);
pinMode(RuangTengahDanKamarRelay, OUTPUT);
lcd.init();
rtc.begin();
lcd.backlight();
rtc.now();
}
void loop() {
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Tgl : "); lcd.print(now.day()); lcd.print("/"); lcd.print(now.month()); lcd.print("/"); lcd.print(now.year());
lcd.setCursor(0, 1);
lcd.print("Jam : "); lcd.print(now.hour()); lcd.print(":"); lcd.print(now.minute()); lcd.print(":"); lcd.print(now.second());
if (now.hour() == 14 && now.minute() > 17 && now.second()>=0) {
digitalWrite(BalkonDanTerasRelay, HIGH);
send(String("lampu_teras_dinyalakann"));
} else if (now.hour() == 12 && now.minute() == 16 && now.second()==0) {
digitalWrite(BalkonDanTerasRelay, LOW);
send(String("lampu+teras+dimatikan"));
}
// Kondisi untuk menyalakan lampu Ruang Tengah
if (now.hour() == 23 && now.minute() == 0 && now.second()==0) {
digitalWrite(RuangTengahDanKamarRelay, HIGH);
send(String("lampu+ruang+tengah+dinyalakan"));
} else if (now.hour() == 7 && now.minute() == 0 && now.second()==0) {
digitalWrite(RuangTengahDanKamarRelay, LOW);
send(String("lampu+ruang+tengahr+dimatikan"));
}
}
void send(String status) {
HTTPClient http;
String serverPath = String(webAppUrl) + "?status=" + String(status);
Serial.print("Connecting to server: ");
Serial.println(serverPath);
if (http.begin(serverPath)) {
int httpCode = http.GET();
if (httpCode > 0) {
Serial.print("Server response code: ");
Serial.println(httpCode);
} else {
Serial.print("HTTP GET request failed with error code: ");
Serial.println(httpCode);
}
http.end();
} else {
Serial.println("Unable to connect to the server");
}
}