#include <WiFi.h>
#include <UniversalTelegramBot.h>
#include <WiFiClientSecure.h>
// Substitua pelos seus dados de rede WiFi
const char* ssid = "Bancadas_02";
const char* password = "Kits@801";
// Informações do bot do Telegram
const char* botToken = "7061326486:AAGu4jrqbl-hfPOgl__ucaaJHXWfsDtjQpc";
const char* chatID = "-4598707459";
// Inicializa o cliente do Telegram
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
// Define o pino do LED
const int ledPin = 2; // Substitua pelo pino que você está usando
// Variável para armazenar a última atualização
int lastUpdate = 0;
void setup() {
Serial.begin(115200);
// Configura o pino do LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Inicializa o LED apagado
// Conecta-se à rede WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Conectado ao WiFi!");
// Configura a conexão segura do cliente
client.setInsecure();
// Envia uma mensagem inicial para o chat
bot.sendMessage(chatID, "Bot iniciado! Use /ligar para ligar o LED e /desligar para desligá-lo.", "");
}
void loop() {
// Verifica se há novas mensagens
int now = millis();
if (now - lastUpdate > 5000) { // Verifica a cada 5 segundos
lastUpdate = now;
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
for (int i = 0; i < numNewMessages; i++) {
String text = bot.messages[i].text;
String from = bot.messages[i].chat_id;
if (from == chatID) { // Verifica se a mensagem é do chat esperado
if (text == "/ligar") {
digitalWrite(ledPin, HIGH); // Liga o LED
bot.sendMessage(chatID, "LED ligado!", "");
}
else if (text == "/desligar") {
digitalWrite(ledPin, LOW); // Desliga o LED
bot.sendMessage(chatID, "LED desligado!", "");
}
}
}
}
}