#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define BOTtoken "put yor bot token"
#define CHAT_ID "write your id telegram"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
const long interval = 1000; // 1 seconde
long lastTime = 0;
// GPIO 4 pour la LED
const int ledPin = 4;
bool ledStatus = false;
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++)
{
String text = bot.messages[i].text;
// Commande pour allumer la LED
if (text == "/on")
{
digitalWrite(ledPin, HIGH); // Allume la LED
ledStatus = true;
bot.sendMessage(CHAT_ID, "LED ON", "");
Serial.println("LED allumée");
}
// Commande pour éteindre la LED
if (text == "/off")
{
digitalWrite(ledPin, LOW); // Éteint la LED
ledStatus = false;
bot.sendMessage(CHAT_ID, "LED OFF", "");
Serial.println("LED éteinte");
}
// Commande pour vérifier l'état de la LED
if (text == "/status")
{
if (ledStatus)
{
bot.sendMessage(CHAT_ID, "LED is ON", "");
}
else
{
bot.sendMessage(CHAT_ID, "LED is OFF", "");
}
}
// Commande d'aide
if (text == "/help")
{
String help = "Commands:\n";
help += "/on - Turn ON the LED \n";
help += "/off - Turn OFF the LED 🔦\n";
help += "/status - Check LED status\n";
help += "/help - Show this message";
bot.sendMessage(CHAT_ID, help, "");
}
}
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("\n\n================================");
Serial.println("ESP32 Telegram LED Controller");
Serial.println("================================\n");
// Configuration de la broche LED comme sortie
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // LED éteinte au démarrage
Serial.println("LED pin configurée sur GPIO " + String(ledPin));
// Connexion WiFi
Serial.print("Connexion à WiFi: ");
Serial.println(WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20)
{
Serial.print(".");
delay(500);
attempts++;
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("\nWiFi connecté!");
Serial.print("Adresse IP: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("\nConnexion WiFi échouée");
}
// Configuration du client sécurisé
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
// Synchronisation de l'heure (important pour HTTPS)
Serial.println("Synchronisation de l'heure NTP...");
configTime(0, 0, "pool.ntp.org");
delay(2000);
// Message de démarrage
bot.sendMessage(CHAT_ID, " ESP32 LED Bot démarré!\nTapez /help pour les commandes", "");
Serial.println(" Bot Telegram prêt!\n");
}
void loop() {
// Vérifier les nouveaux messages toutes les 1 seconde
if (millis() - lastTime > interval)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages)
{
Serial.println("Réponse de Telegram reçue");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTime = millis();
}
}