#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// SSID dan Password WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Telegram BOT
#define BOTtoken "7601682558:AAE1gog7eZd86A-HbOZDqZa4mZasT_ylywM"
#define CHAT_ID "7866627819"
#define pin_merah 15
#define pin_kuning 13
#define pin_hijau 4
#define pin_biru 5
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Cek Pesan Setiap 1 detik
int interval = 1000;
unsigned long waktu_terakhir;
bool status_merah;
bool status_kuning;
bool status_hijau;
bool status_biru;
void setup() {
// atur mode pin menjadi output
pinMode(pin_merah, OUTPUT);
pinMode(pin_kuning, OUTPUT);
pinMode(pin_hijau, OUTPUT);
pinMode(pin_biru, OUTPUT);
// Serial monitor
Serial.begin(115200);
// Hubungkan ke Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Sedang menghubungkan ke WiFi..");
}
// IP Address
Serial.println(WiFi.localIP());
Serial.print("Atur Waktu: ");
configTime(0, 0, "pool.ntp.org");
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop() {
cek_pesan();
}
// Cek Pesan Terbaru
void cek_pesan(){
if (millis() > waktu_terakhir + interval){
int banyakPesan = bot.getUpdates(bot.last_message_received + 1);
while(banyakPesan) {
Serial.println("got response");
handleNewMessages(banyakPesan);
banyakPesan = bot.getUpdates(bot.last_message_received + 1);
}
waktu_terakhir = millis();
}
}
// Memproses pesan yang diterima
void handleNewMessages(int numNewMessages) {
for (int i=0; i<numNewMessages; i++) {
// Cek Chat ID
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Terima pesan dari telegram
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Selamat Datang, " + from_name + ".\n";
// Komen berikut untuk kontrol LED
welcome += "/merah_on \n"; //untuk LED Merah ON
welcome += "/merah_off \n"; // untuk LED Merah OFF
welcome += "/status_merah\n"; // mendapatkan status LED Merah
welcome += "/kuning_on \n"; //untuk LED Merah ON
welcome += "/kuning_off \n"; // untuk LED Merah OFF
welcome += "/status_kuning\n"; // mendapatkan status LED Merah
welcome += "/hijau_on \n"; //untuk LED Merah ON
welcome += "/hijau_off \n"; // untuk LED Merah OFF
welcome += "/status_hijau\n"; // mendapatkan status LED Merah
welcome += "/biru_on \n"; //untuk LED Merah ON
welcome += "/biru_off \n"; // untuk LED Merah OFF
welcome += "/status_biru\n"; // mendapatkan status LED Merah
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/merah_on")
{
bot.sendMessage(chat_id, "LED Merah ON", "");
led_merah(true);
}
if (text == "/merah_off")
{
bot.sendMessage(chat_id, "LED Merah OFF", "");
led_merah(false);
}
if (text == "/status_merah")
{
if (digitalRead(pin_merah))
{
bot.sendMessage(chat_id, "LED Merah ON", "");
}
else
{
bot.sendMessage(chat_id, "LED Merah OFF", "");
}
}
if (text == "/kuning_on")
{
bot.sendMessage(chat_id, "LED kuning ON", "");
led_kuning(true);
}
if (text == "/kuning_off")
{
bot.sendMessage(chat_id, "LED kuning OFF", "");
led_kuning(false);
}
if (text == "/status_kuning")
{
if (digitalRead(pin_kuning))
{
bot.sendMessage(chat_id, "LED kuning ON", "");
}
else
{
bot.sendMessage(chat_id, "LED kuning OFF", "");
}
}
if (text == "/hijau_on")
{
bot.sendMessage(chat_id, "LED hijau ON", "");
led_hijau(true);
}
if (text == "/hijau_off")
{
bot.sendMessage(chat_id, "LED hijau OFF", "");
led_hijau(false);
}
if (text == "/status_hijau")
{
if (digitalRead(pin_hijau))
{
bot.sendMessage(chat_id, "LED hijau ON", "");
}
else
{
bot.sendMessage(chat_id, "LED hijau OFF", "");
}
}
if (text == "/biru_on")
{
bot.sendMessage(chat_id, "LED biru ON", "");
led_biru(true);
}
if (text == "/biru_off")
{
bot.sendMessage(chat_id, "LED biru OFF", "");
led_biru(false);
}
if (text == "/status_biru")
{
if (digitalRead(pin_biru))
{
bot.sendMessage(chat_id, "LED biru ON", "");
}
else
{
bot.sendMessage(chat_id, "LED biru OFF", "");
}
}
}
}
void led_merah(bool isOn){
if(isOn){
digitalWrite(pin_merah, HIGH);
}
else{
digitalWrite(pin_merah, LOW);
}
}
void led_kuning(bool isOn){
if(isOn){
digitalWrite(pin_kuning, HIGH);
}
else{
digitalWrite(pin_kuning, LOW);
}
}
void led_hijau(bool isOn){
if(isOn){
digitalWrite(pin_hijau, HIGH);
}
else{
digitalWrite(pin_hijau, LOW);
}
}
void led_biru(bool isOn){
if(isOn){
digitalWrite(pin_biru, HIGH);
}
else{
digitalWrite(pin_biru, LOW);
}
}