#include <WiFi.h> //library buat aktifin wifi buat ke esp
#include <WiFiClientSecure.h> //biasanya wifi ada pwnya, buat pwnya
#include <UniversalTelegramBot.h> //nyambungi ke telegram
//include = masukin library di progrma
// identitas wifinya
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD "" //pwnya apa
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "7050221132:AAHU9-JLmxPFuKg1XeCbm707Ey3YPeO9ijo"
const unsigned long BOT_MTBS = 1000; // jeda antara telegram dan esp nya
WiFiClientSecure secured_client; //perintah buat aktifin PW wifinya
UniversalTelegramBot bot(BOT_TOKEN, secured_client); //peintah buat nyambungin bot tele dan pw wifinya
unsigned long bot_lasttime; // kalo wifi mati, biar espnya scan minta terhubung terus ke wifinya
const int ledPin = 2; //
int ledStatus = 0;
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";
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")
{
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 = "Hai sayang, mau nyalain lampu ya?" + from_name + ".\n";//string itu tipe data tulisan
welcome += "Pilih menu di bawaah ini ya ayang.\n\n";
welcome += "/ledon : Untuk menyalakan lampu\n";
welcome += "/ledoff : Untuk mematikan lampu\n";
welcome += "/status : Untuk melihat status lampu\n";
bot.sendMessage(chat_id, welcome, "Markdown"); //APA INI BRUH GAK NGERTI
}
}
}
void setup()
{
Serial.begin(115200);
Serial.println();
pinMode(ledPin, OUTPUT); // buat setting pin nomor 2. initialize digital ledPin as an output.
digitalWrite(ledPin, LOW); // initialize pin as off (active LOW)
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for 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"); // get UTC time via NTP //atur waktu realtime dicetak 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();
}
}