#include <WiFi.h> //Untuk mengaktifkan Wi-Fi ke ESP32
#include <WiFiClientSecure.h> //Untuk mengakses kata sandi pada Wi-Fi
#include <UniversalTelegramBot.h> //Untuk menghubungkan ke telegram
//Dalam program, gunakan perintah 'include' untuk menyertakan atau memasukkan library yang diperlukan
// Mengisi Identitas Wi-FI
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD "" //input kata sandi Wi-Fi
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "6802700397:AAGcS3BdTEokXX_CZXfHyQLZPeMhM1krAII"
const unsigned long BOT_MTBS = 1000; // jeda proses antara Telegram dan ESP32
WiFiClientSecure secured_client; //perintah untuk mengaktifkan kata sandi Wi-Fi
UniversalTelegramBot bot(BOT_TOKEN, secured_client); //perintah uuntuk membuat mengkoneksi BotTele dan kata sandi Wi - Fi
unsigned long bot_lasttime;
const int ledPin = 2; //untuk menyambungkan program ke pin 2 pada ESP
int ledStatus = 0; //input ledstatus (0 kondisi LED OFF dan 1 kondisi LED ON)
void handleNewMessages(int numNewMessages)
{
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Guest";
//untuk membaca ketika kondisi LED ON
if (text == "/ledon")
{
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
ledStatus = 1;
bot.sendMessage(chat_id, "Turn ON LED", "");
Serial.println("Turn ON LED\n\n");
}
if (text == "/ledoff")
{
//untuk membaca ketika kondisi LED OFF
ledStatus = 0;
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level)
bot.sendMessage(chat_id, "Turn OFF LED", "");
Serial.println("Turn OFF LED\n\n");
}
if (text == "/status")
{
if (ledStatus)
{
bot.sendMessage(chat_id, "LED is ON", "");
Serial.println("LED is ON\n\n");
}
else
{
bot.sendMessage(chat_id, "LED is OFF", "");
Serial.println("LED is OFF\n\n");
}
}
if (text == "/start")
{
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
//Untuk memunculkan kata-kata di BotTele
welcome += "This is Flash Led Bot switch.\n\n";
welcome += "/ledon : to switch the Led ON\n";
welcome += "/ledoff : to switch the Led OFF\n";
welcome += "/status : Returns current status of LED\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup()
{
Serial.begin(115200);
Serial.println();
pinMode(ledPin, OUTPUT); //Untuk mengatur pin nomor 2 sebagai output
digitalWrite(ledPin, LOW); //untuk mengatur awal ketika LED diberi sumber kondisi LED dalam keadaan mati
//Mencoba untuk terhubung ke jaringan WiFi:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Menambahkan root certificate ke api.telegram.org
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); //Mengatur waktu realtime di cetak ke Serial Monitor setiap detik
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop()
{
if (millis() - bot_lasttime > BOT_MTBS)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages)
{
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
}
}