// Wokwi ESP32 DevKitC V4 Board
// Relay Module connected to GPIO 26
// LED connected to GPIO 27 with a 220 ohm resistor
#include <WiFi.h>
#include <WiFiClient.h>
#include <UniversalTelegramBot.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Telegram Bot token
const char* botToken = "7057686957:AAGPfJcK_7uJbo4vupF6tlR5vCf_tPJnEqc";
WiFiClient client;
UniversalTelegramBot bot(botToken, client);
const int relayPin = 26; // Relay Module connected to GPIO 26
const int ledPin = 27; // LED connected to GPIO 27
void setup() {
Serial.begin(115200);
// Initialize WiFi
WiFi.begin(ssid, password);
while (WiFi.status()!= WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Initializing Telegram Bot...");
// Initialize Telegram Bot
bot.sendMessage("Wokwi ESP32 DevKitC V4", "Bot started!");
// Initialize Relay and LED pins as outputs
pinMode(relayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("Received message");
String chat_id = String(bot.messages[0].chat_id);
String text = bot.messages[0].text;
if (text == "/turn_on") {
digitalWrite(relayPin, HIGH);
digitalWrite(ledPin, HIGH);
bot.sendMessage(chat_id, "Turned on!");
} else if (text == "/turn_off") {
digitalWrite(relayPin, LOW);
digitalWrite(ledPin, LOW);
bot.sendMessage(chat_id, "Turned off!");
}
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
}