#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <Preferences.h>
#include "time.h"
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* botToken = "7164767848:AAG-enx254qOmqMEzopTWHMIeS_sMWQAm30";
const String groupChatID = "-4220169840";
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
Preferences preferences;
unsigned long lastTimeBotRan;
const int botRequestDelay = 500;
int ledStates[2] = { LOW, LOW };
int autoat = false;
int pinss[2] = { 14, 26 }; // Pin untuk masing-masing tombol
String chat_id = "";
int Last_msg_id = 0;
void setup() {
Serial.begin(115200);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
wifi_setup();
pinMode(pinss[0], OUTPUT);
pinMode(pinss[1], OUTPUT);
digitalWrite(pinss[0], LOW);
// Inisialisasi Preferences
preferences.begin("ledStates", false);
ledStates[1] = preferences.getBool("led2", LOW);
autoat = preferences.getBool("ledauto", false);
digitalWrite(pinss[1], ledStates[1]);
setupNTP();
}
// Fungsi untuk mengatur NTP
void setupNTP() {
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600; // Offset waktu untuk UTC+8 (8 jam)
const int daylightOffset_sec = 0;
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
// Add a timeout to prevent blocking forever
struct tm timeinfo;
if (!getLocalTime(&timeinfo, 10000)) { // 10-second timeout
Serial.println("Failed to obtain time");
} else {
Serial.println("NTP time synchronization done.");
}
}
// Mendefinisikan enum untuk format waktu
enum TimeFormat {
YEAR, // 0: YYYY
MONTH, // 1: MM
DAY, // 2: DD
DAY_NAME, // 3: Nama hari
HOUR, // 4: JAM
MINUTE, // 5: MENIT
SECOND // 6: DETIK
};
String Get_time(int format) {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return ""; // Kembalikan string kosong jika gagal
}
String daysOfWeek[] = { "Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu" };
switch (format) {
case YEAR: return String(timeinfo.tm_year + 1900);
case MONTH: return String(timeinfo.tm_mon + 1);
case DAY: return String(timeinfo.tm_mday);
case DAY_NAME: return daysOfWeek[timeinfo.tm_wday];
case HOUR: return String(timeinfo.tm_hour);
case MINUTE: return String(timeinfo.tm_min);
case SECOND: return String(timeinfo.tm_sec);
default: return "";
}
}
void wifi_setup() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
Serial.println("Connected to WiFi");
}
}
void loop() {
wifi_setup();
unsigned long currentMillis = millis();
String date = Get_time(DAY) + "/" + Get_time(MONTH) + "/" + Get_time(YEAR) + " - " + Get_time(HOUR) + ":" + Get_time(MINUTE) + ":" + Get_time(SECOND);
if (autoat) {
if ((Get_time(HOUR).toInt() >= 17 && Get_time(HOUR).toInt() <= 22) && !ledStates[1]) {
digitalWrite(pinss[1], HIGH);
ledStates[1] = true;
//Serial.println("on");
preferences.putBool("led2", ledStates[1]);
String msg = "AUTO LAMP : " + date;
String keyboardJson = "[[{ \"text\" : \"PC\", \"callback_data\" : \"PC\" }],";
keyboardJson += "[{ \"text\" : \"LAMP : " + String(ledStates[1] ? "ON" : "OFF") + "\", \"callback_data\" : \"LAMP\" }],";
keyboardJson += "[{ \"text\" : \"AUTO : " + String(autoat ? "ON" : "OFF") + "\", \"callback_data\" : \"AUTO\" }]]";
bot.sendMessageWithInlineKeyboard(chat_id, msg, "Markdown", keyboardJson, Last_msg_id);
} else {
if (ledStates[1]) {
digitalWrite(pinss[1], LOW);
ledStates[1] = false;
//Serial.println("off");
preferences.putBool("led2", ledStates[1]);
String msg = "AUTO LAMP : " + date;
String keyboardJson = "[[{ \"text\" : \"PC\", \"callback_data\" : \"PC\" }],";
keyboardJson += "[{ \"text\" : \"LAMP : " + String(ledStates[1] ? "ON" : "OFF") + "\", \"callback_data\" : \"LAMP\" }],";
keyboardJson += "[{ \"text\" : \"AUTO : " + String(autoat ? "ON" : "OFF") + "\", \"callback_data\" : \"AUTO\" }]]";
bot.sendMessageWithInlineKeyboard(chat_id, msg, "Markdown", keyboardJson, Last_msg_id);
}
}
}
if (millis() - lastTimeBotRan > botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
if (numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
chat_id = String(bot.messages[i].chat_id);
Last_msg_id = bot.messages[i].message_id;
String from_name = bot.messages[i].from_name;
if (from_name == "") {
from_name = "Guest";
}
//Serial.println("Chat ID: " + chat_id);
if (bot.messages[i].type == "callback_query") {
String callbackData = bot.messages[i].text;
//Serial.println(chat_id);
//Serial.println(bot.messages[i].message_id);
//Serial.print("Callback data received: ");
//Serial.println(callbackData);
preferences.putString("chat_id", chat_id);
preferences.putInt("Last_msg_id", Last_msg_id);
if (callbackData == "PC") {
digitalWrite(pinss[0], HIGH);
delay(200);
digitalWrite(pinss[0], LOW);
String msg = "PC : " + date;
} else if (callbackData == "LAMP" && !autoat) {
ledStates[1] = !ledStates[1];
digitalWrite(pinss[1], ledStates[1]);
preferences.putBool("led2", ledStates[1]);
String msg = "LAMP : " + date;
} else if (callbackData == "AUTO") {
autoat = !autoat;
preferences.putBool("ledauto", autoat);
String msg = "AUTO : " + date;
}
String keyboardJson = "[[{ \"text\" : \"PC\", \"callback_data\" : \"PC\" }],";
keyboardJson += "[{ \"text\" : \"LAMP : " + String(ledStates[1] ? "ON" : "OFF") + "\", \"callback_data\" : \"LAMP\" }],";
keyboardJson += "[{ \"text\" : \"AUTO : " + String(autoat ? "ON" : "OFF") + "\", \"callback_data\" : \"AUTO\" }]]";
bot.sendMessageWithInlineKeyboard(chat_id, msg, "Markdown", keyboardJson, bot.messages[i].message_id);
} else {
if (bot.messages[i].text == "/start") {
String msg = "Hi " + from_name + "!\n";
msg += "You can control the buttons below:\n";
String keyboardJson = "[[{ \"text\" : \"PC\", \"callback_data\" : \"PC\" }],";
keyboardJson += "[{ \"text\" : \"LAMP : " + String(ledStates[1] ? "ON" : "OFF") + "\", \"callback_data\" : \"LAMP\" }],";
keyboardJson += "[{ \"text\" : \"AUTO : " + String(autoat ? "ON" : "OFF") + "\", \"callback_data\" : \"AUTO\" }]]";
bot.sendMessageWithInlineKeyboard(chat_id, msg, "Markdown", keyboardJson);
}
}
}
}
lastTimeBotRan = millis();
}
}