#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// === WiFi Settings ===
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// === Telegram Bot ===
#define BOT_TOKEN "8403154383:AAF0m21mhUwFKZ9M8TMxiyhoJguXnX-a1Kg"
#define CHAT_ID "-4944057888" // sama seperti node sensor
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
// === Alarm Configuration ===
const int ledPin = 2; // pin LED
const int alarmDuration = 30000; // 30 detik (ms)
unsigned long lastChecked = 0;
const unsigned long checkDelay = 2000;
void triggerAlarm() {
Serial.println("šØ Trigger received! Turning ON LED for 30 seconds...");
digitalWrite(ledPin, HIGH);
bot.sendMessage(CHAT_ID, "šØ LED ALARM ON (30 seconds)", "");
delay(alarmDuration);
digitalWrite(ledPin, LOW);
Serial.println("ā
LED OFF (Alarm ended)");
bot.sendMessage(CHAT_ID, "ā
LED ALARM OFF", "");
}
void handleTelegramMessages() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
for (int i = 0; i < numNewMessages; i++) {
String text = bot.messages[i].text;
String chat_id = bot.messages[i].chat_id;
String from_name = bot.messages[i].from_name;
Serial.println("š„ Received command: " + text + " from " + from_name);
if (text == "/triggerbuzz") {
bot.sendMessage(chat_id, "š Alarm triggered by command", "");
triggerAlarm();
}
}
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
WiFi.begin(ssid, password);
client.setInsecure(); // Non-SSL validation
Serial.print("š Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nā
WiFi Connected!");
bot.sendMessage(CHAT_ID, "š¢ Alarm Node Online and Ready", "");
}
void loop() {
unsigned long now = millis();
if (now - lastChecked > checkDelay) {
handleTelegramMessages();
lastChecked = now;
}
}