#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ESP32Servo.h>
// WiFi credentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Telegram Bot Token
#define BOT_TOKEN "7879243156:AAHfIulhSk0D5Dlj9W0c_Xdhc1PFxPUkWBI"
// Access Password
#define ACCESS_PASSWORD "1234"
const unsigned long BOT_MTBS = 1000; // Bot message check interval
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime = 0;
Servo myServo;
const int servoPin = 18;
bool servoStatus = false; // false = closed, true = open
bool waitingForPassword = false; // expecting password input
String passwordRequestChatId = "";
void handleNewMessages(int 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";
Serial.println("Received: " + text);
if (text == "/start") {
String welcome = "Welcome to Secure Servo Door Bot\n\n";
welcome += "/start - Show Commands\n";
welcome += "/status - Show Status\n";
welcome += "/servo_on - Open Door\n";
welcome += "/servo_off - Close Door\n";
welcome += "/password 1234 - Authenticate\n";
bot.sendMessage(chat_id, welcome, "");
}
else if (text == "/status") {
String statusMsg = servoStatus ? "Door is OPEN\n" : "Door is CLOSED\n";
statusMsg += waitingForPassword ? "Waiting for password input..." : "Access: ";
bot.sendMessage(chat_id, statusMsg, "");
}
else if (text == "/servo_on") {
bot.sendMessage(chat_id, "Enter your password using /password <your_password>", "");
waitingForPassword = true;
passwordRequestChatId = chat_id;
}
else if (text.startsWith("/password ")) {
String enteredPassword = text.substring(10);
if (enteredPassword == ACCESS_PASSWORD) {
if (waitingForPassword && chat_id == passwordRequestChatId) {
myServo.write(90); // Open the door
servoStatus = true;
bot.sendMessage(chat_id, "✅ Correct password\nDoor is now OPEN", "");
waitingForPassword = false;
} else {
bot.sendMessage(chat_id, "✅ Password accepted, but no action requested.", "");
}
} else {
bot.sendMessage(chat_id, "❌ Wrong password", "");
waitingForPassword = false;
}
}
else if (text == "/servo_off") {
myServo.write(0); // Close the door
servoStatus = false;
waitingForPassword = false;
bot.sendMessage(chat_id, "🔒 Door is now CLOSED", "");
}
else {
bot.sendMessage(chat_id, "❓ Unknown command. Type /start for help.", "");
}
}
}
void setup() {
Serial.begin(115200);
myServo.attach(servoPin);
myServo.write(0); // Initial position = door closed
Serial.print("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setInsecure(); // Disable SSL certificate validation
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected.");
Serial.println("IP: " + WiFi.localIP().toString());
}
void loop() {
if (millis() - bot_lasttime > BOT_MTBS) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
}
}