#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "CISCO";
const char* password = "IVc1sc0%";
const char* botToken = "8GV2UJTY2R6T0LX5";
const int botId = 2284882;
const int ledPin = 25;
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHT_PIN 15
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
bool ledOn = false;
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a la red Wi-Fi...");
}
Serial.println("Conexión exitosa a la red Wi-Fi");
}
void loop() {
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
Serial.print("Temperatura actual: ");
Serial.println(temperature);
if (temperature > 26.0) {
sendMessage("La temperatura actual es superior a 26 grados Celsius. ¿Desea encender el ventilador? (Sí/No)");
waitForUserConfirmation();
}
}
delay(1000);
}
void sendMessage(const String& message) {
HTTPClient http;
String url = "https://api.telegram.org/bot" + String(botToken) + "/sendMessage";
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String body = "chat_id=" + String(botId) + "&text=" + message;
int httpResponseCode = http.POST(body);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Respuesta del servidor Telegram:");
Serial.println(response);
} else {
Serial.print("Error en la solicitud HTTP. Código de respuesta: ");
Serial.println(httpResponseCode);
}
http.end();
}
void waitForUserConfirmation() {
while (true) {
if (Serial.available()) {
String messageText = Serial.readString();
messageText.trim();
if (messageText.equalsIgnoreCase("Sí") || messageText.equalsIgnoreCase("Si")) {
digitalWrite(ledPin, HIGH);
Serial.println("LED encendido.");
ledOn = true;
return;
}
if (messageText.equalsIgnoreCase("No")) {
Serial.println("No se ha encendido el LED.");
return;
}
}
}
}