#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
// ===== CONFIGURACIÓN DE PINES =====
#define DHTPIN 4
#define DHTTYPE DHT22
#define LED_VERDE 18
#define LED_ROJO 5
#define RELE_PIN 2
// ===== CONFIGURACIÓN WIFI =====
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ===== CONFIGURACIÓN TELEGRAM =====
#define BOT_TOKEN "8767504923:AAGzQrL-XsA0pHeIu3KwQYcrZ_OfoFxyexc"
#define CHAT_ID "876504923"
// ===== CONFIGURACIÓN DE TEMPERATURA =====
float TEMP_UMBRAL = 24.0;
float TEMP_ALERTA = 27.0;
// ===== OBJETOS =====
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
// ===== VARIABLES =====
unsigned long lastTime = 0;
unsigned long timerDelay = 5000;
bool alertaEnviada = false;
void setup() {
Serial.begin(115200);
// ===== CONFIGURAR PINES =====
pinMode(LED_VERDE, OUTPUT);
pinMode(LED_ROJO, OUTPUT);
pinMode(RELE_PIN, OUTPUT);
digitalWrite(LED_VERDE, HIGH);
digitalWrite(LED_ROJO, LOW);
digitalWrite(RELE_PIN, LOW);
// ===== INICIAR SENSOR =====
dht.begin();
delay(2000);
// ===== INICIAR LCD =====
lcd.init(); // ← ¡IMPORTANTE!
lcd.backlight(); // ← ¡IMPORTANTE!
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor de Temp");
lcd.setCursor(0, 1);
lcd.print("Inteligente");
delay(3000);
// ===== CONECTAR WIFI =====
lcd.clear();
lcd.print("Conectando WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi conectado!");
// ===== CONFIGURACIÓN CRÍTICA PARA WOKWI =====
client.setInsecure(); // ← ¡OBLIGATORIO!
lcd.clear();
lcd.print("Sistema Listo");
lcd.setCursor(0, 1);
lcd.print("Temp: --.- C");
// ===== ENVIAR MENSAJE DE INICIO =====
bot.sendMessage(CHAT_ID, "✅ Sistema de temperatura iniciado.\nComandos: /temperatura, /estado, /vent_on, /vent_off", "");
Serial.println("✅ Mensaje enviado a Telegram");
delay(2000);
}
void loop() {
// ===== LEER SENSOR =====
float temp = dht.readTemperature();
float hum = dht.readHumidity();
// ===== SI EL SENSOR FALLA =====
if (isnan(temp) || isnan(hum)) {
lcd.clear();
lcd.print("Error sensor");
Serial.println("Error al leer sensor");
delay(2000);
return;
}
// ===== LEER MENSAJES DE TELEGRAM =====
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
if (chat_id == CHAT_ID) {
if (text == "/start") {
bot.sendMessage(chat_id,
"🤖 ¡Bienvenido!\n\n"
"Comandos:\n"
"/temperatura - Ver temp\n"
"/estado - Ver estado\n"
"/vent_on - Encender\n"
"/vent_off - Apagar", "");
}
else if (text == "/temperatura" || text == "/temp") {
String respuesta = "🌡️ " + String(temp, 1) + "°C\n💧 " + String(hum, 1) + "%";
bot.sendMessage(chat_id, respuesta, "");
}
else if (text == "/estado") {
String respuesta = "📊 ESTADO\n━━━━━━━━━━━\n";
respuesta += "🌡️ Temp: " + String(temp, 1) + "°C\n";
respuesta += "💧 Hum: " + String(hum, 1) + "%\n";
respuesta += "🌀 Vent: " + String(digitalRead(RELE_PIN) ? "ON" : "OFF");
bot.sendMessage(chat_id, respuesta, "");
}
else if (text == "/vent_on") {
digitalWrite(RELE_PIN, HIGH);
bot.sendMessage(chat_id, "🌀 Ventilador encendido", "");
}
else if (text == "/vent_off") {
digitalWrite(RELE_PIN, LOW);
bot.sendMessage(chat_id, "🌀 Ventilador apagado", "");
}
else {
bot.sendMessage(chat_id, "❓ Envía /start para ver comandos", "");
}
}
}
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
// ===== CONTROL DE TEMPERATURA =====
if ((millis() - lastTime) > timerDelay) {
// Control de ventilador
if (temp > TEMP_UMBRAL) {
digitalWrite(RELE_PIN, HIGH);
digitalWrite(LED_VERDE, LOW);
digitalWrite(LED_ROJO, HIGH);
} else {
digitalWrite(RELE_PIN, LOW);
digitalWrite(LED_VERDE, HIGH);
digitalWrite(LED_ROJO, LOW);
}
// Alerta por temperatura crítica
if (temp > TEMP_ALERTA && !alertaEnviada) {
String mensaje = "🚨 ¡ALERTA!\n🌡️ " + String(temp, 1) + "°C";
bot.sendMessage(CHAT_ID, mensaje, "");
alertaEnviada = true;
} else if (temp <= TEMP_UMBRAL) {
alertaEnviada = false;
}
// Mostrar en LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp, 1);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(hum, 1);
lcd.print("%");
if (digitalRead(RELE_PIN)) {
lcd.setCursor(13, 1);
lcd.print("ON");
} else {
lcd.setCursor(13, 1);
lcd.print("OFF");
}
Serial.print("Temp: ");
Serial.print(temp);
Serial.print("°C - Hum: ");
Serial.print(hum);
Serial.println("%");
lastTime = millis();
}
}