#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Dados do WiFi
#define WIFI_SSID "Bancadas_02"
#define WIFI_PASSWORD "Kits@801"
// Telegram BOT Token (Botfather)
#define BOT_TOKEN "7061326486:AAGu4jrqbl-hfPOgl__ucaaJHXWfsDtjQpc"
// Use @myidbot (IDBot) para saber qual o seu ID
#define CHAT_ID "-1002493561972"
// Definir o pino do LED e do potenciômetro
#define LED_PIN 2
#define POT_PIN 34 // Pino ADC do ESP32 para o potenciômetro
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
// Variável para armazenar a última verificação de mensagens
unsigned long lastTimeBotRan;
const int botDelay = 1000; // Checar mensagens a cada 1 segundo
void handleNewMessages(int numNewMessages) {
Serial.print("Recebendo mensagens: ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
Serial.print("Mensagem recebida: ");
Serial.println(text);
// Comando /on para ligar o LED
if (text == "/on") {
digitalWrite(LED_PIN, HIGH); // Liga o LED
if (digitalRead(LED_PIN) == HIGH) {
bot.sendMessage(chat_id, "LED ligado!", "");
Serial.println("LED ligado com sucesso.");
} else {
bot.sendMessage(chat_id, "Erro ao ligar o LED.", "");
Serial.println("Não foi possível ligar o LED.");
}
}
// Comando /off para desligar o LED
else if (text == "/off") {
digitalWrite(LED_PIN, LOW); // Desliga o LED
if (digitalRead(LED_PIN) == LOW) {
bot.sendMessage(chat_id, "LED desligado!", "");
Serial.println("LED desligado com sucesso.");
} else {
bot.sendMessage(chat_id, "Erro ao desligar o LED.", "");
Serial.println("Não foi possível desligar o LED.");
}
}
// Comando /check para verificar o valor do potenciômetro
else if (text == "/check") {
int potValue = analogRead(POT_PIN); // Ler o valor do potenciômetro
String message = "O valor atual do potenciômetro é: " + String(potValue);
bot.sendMessage(chat_id, message, ""); // Enviar o valor para o Telegram
Serial.println(message); // Exibir no monitor serial
}
// Mensagem não reconhecida
else {
bot.sendMessage(chat_id, "Comando não reconhecido.", "");
}
}
}
void setup() {
Serial.begin(115200);
// Configuração do LED
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Começa com o LED desligado
// Configuração do potenciômetro
pinMode(POT_PIN, INPUT);
// Conexão com a rede WiFi
Serial.print("Conectando ao WiFi SSID: ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Define o certificado do Telegram
// Aguarda conexão com a rede WiFi
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi conectado. Endereço IP: ");
Serial.println(WiFi.localIP());
// Sincroniza o tempo via NTP
configTime(0, 0, "pool.ntp.org"); // Obter UTC via NTP
time_t now = time(nullptr);
while (now < 24 * 3600) {
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
// Envia uma mensagem de boas-vindas
if (bot.sendMessage(CHAT_ID,
"Olá! Bem-vindo ao BOT Manutenção Senai!,\n\n"
"Aqui você poderá monitorar:\n"
"- Temperatura\n"
"- Tempo de Funcionamento\n"
"- Status Operacional\n\n"
"Mantenha-se informado e sempre no controle de suas operações, garantindo o melhor desempenho dos seus equipamentos.\n\n"
"Para começar, utilize os comandos abaixo ou aguarde os relatórios automáticos.\n"
"/on - Ligue o LED\n"
"/off - Desligue o LED\n"
"/check - Verificar valor do potenciômetro", "")) {
Serial.println("Mensagem de boas-vindas enviada com sucesso!");
} else {
Serial.println("Falha ao enviar a mensagem de boas-vindas.");
}
lastTimeBotRan = millis();
}
void loop() {
// Verifica se há novas mensagens no bot a cada 1 segundo
if (millis() - lastTimeBotRan > botDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
}