#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
//#include <ArduinoJson.h>
// Wifi network station credentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "7008936872:AAGbkFqgB9izIzE6Qz_yHc9X8qsZ_CZKX_M"
#define relay1 0
#define relay2 2
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
unsigned long bot_lasttime = 0; // last time messages' scan has been done
String idBot = "718396295";
String idDevice = "Saklar On / Off";
void setup() {
Serial.begin(115200);
Serial.println("Start");
client.setInsecure();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
pinMode(1, OUTPUT);
int wifi_attempts = 0; // Counter to track the number of connection attempts
while (WiFi.status() != WL_CONNECTED && wifi_attempts < 20) { // Limit the number of attempts
digitalWrite(1, LOW);
delay(500);
digitalWrite(1, HIGH);
delay(500);
Serial.print(".");
wifi_attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println();
Serial.print("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());
String startUp = idDevice + " Online!\n";
bot.sendMessage(idBot, startUp, "");
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
digitalWrite(relay1, HIGH); // Start with lamp 1 off
digitalWrite(relay2, HIGH); // Start with lamp 2 off
} else {
Serial.println();
Serial.println("Failed to connect to WiFi");
}
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
if (text == "/start") {
String welcome = "Silakan Pilih Perintah!";
bot.sendMessage(chat_id, welcome, "");
} else if (text == "/comand1") {
digitalWrite(relay1, HIGH);
bot.sendMessage(chat_id, "Lampu 1 NYALA", "");
} else if (text == "/comand2") {
digitalWrite(relay1, LOW);
bot.sendMessage(chat_id, "Lampu 1 MATI", "");
} else if (text == "/comand3") {
digitalWrite(relay2, HIGH);
bot.sendMessage(chat_id, "Lampu 2 NYALA", "");
} else if (text == "/comand4") {
digitalWrite(relay2, LOW);
bot.sendMessage(chat_id, "Lampu 2 MATI", "");
} else if (text == "/comand5") {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
bot.sendMessage(chat_id, "Lampu 1 & 2 NYALA", "");
} else if (text == "/comand6") {
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
bot.sendMessage(chat_id, "Lampu 1 & 2 MATI", "");
}
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
if (millis() - bot_lasttime > 1000UL) {
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();
}
delay(50);
} else {
Serial.println("WiFi not connected. Reconnecting...");
WiFi.disconnect();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
delay(1000);
}
}