#include <WiFi.h>
#include <UniversalTelegramBot.h>
#include <WiFiClientSecure.h>
#include <DHT.h>
// Defina as credenciais da rede WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Defina o token do bot do Telegram
#define BOTtoken "7469625990:AAHxEzrRH5-fPoGAp5fJZeptW1iqc3GGTt0"
// Defina o pino do sensor DHT22 e o tipo de sensor
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Defina os pinos dos LEDs
#define LED1 5
#define LED2 18
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
unsigned long lastTime = 0;
unsigned long timerDelay = 2000;
void setup() {
// Inicie a comunicação serial
Serial.begin(115200);
// Conecte-se à rede WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando ao WiFi...");
}
Serial.println("Conectado ao WiFi");
// Configure os pinos dos LEDs como saída
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
// Inicie o sensor DHT22
dht.begin();
// Configure a conexão segura
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
}
void handleNewMessages(int numNewMessages) {
Serial.print("Mensagens recebidas: ");
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;
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Bem-vindo ao bot de controle de LEDs e leitura de sensor DHT22.\n\n";
welcome += "/led1_on - Ligar LED 1\n";
welcome += "/led1_off - Desligar LED 1\n";
welcome += "/led2_on - Ligar LED 2\n";
welcome += "/led2_off - Desligar LED 2\n";
welcome += "/status - Obter temperatura e umidade\n";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/led1_on") {
digitalWrite(LED1, HIGH);
bot.sendMessage(chat_id, "LED 1 ligado", "");
}
if (text == "/led1_off") {
digitalWrite(LED1, LOW);
bot.sendMessage(chat_id, "LED 1 desligado", "");
}
if (text == "/led2_on") {
digitalWrite(LED2, HIGH);
bot.sendMessage(chat_id, "LED 2 ligado", "");
}
if (text == "/led2_off") {
digitalWrite(LED2, LOW);
bot.sendMessage(chat_id, "LED 2 desligado", "");
}
if (text == "/status") {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
bot.sendMessage(chat_id, "Falha ao ler do sensor DHT22!", "");
} else {
String status = "Temperatura: " + String(t) + "°C\n";
status += "Umidade: " + String(h) + "%";
bot.sendMessage(chat_id, status, "");
}
}
}
}
void loop() {
if (millis() - lastTime > timerDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTime = millis();
}
}