#include <CTBot.h>
#include "DHTesp.h"
#define LED 2
const int DHT_PIN = 15;
#define SensorLuz 36
CTBot bot_didactico;
DHTesp dhtSensor;
String TOKEN = "6128628487:AAEtQd6GsjHaOHx1XWnKaB8tYpSXFs2shdc";
String SSID = "Wokwi-GUEST";
String PSWD = "";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
bot_didactico.wifiConnect(SSID, PSWD);
bot_didactico.setTelegramToken(TOKEN);
if(bot_didactico.testConnection()){
Serial.println("Conectado exitosamente al Telegram");
} else{
Serial.println("No hay conexion con Telegram");
}
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
const float GAMMA = 0.7;
const float RL10 = 50;
TBMessage mensaje;
TempAndHumidity data = dhtSensor.getTempAndHumidity();
int lectura = analogRead(SensorLuz);
int valReal = lectura / 4;
float voltage = valReal / 1024.0 * 5.0;
float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, 1.0 / GAMMA);
if (bot_didactico.getNewMessage(mensaje)) {
Serial.println("Hay un nuevo mensaje en el chat!!");
Serial.print("Mensaje leido: ");
Serial.println(mensaje.text);
bot_didactico.sendMessage(mensaje.sender.id, "Hola, soy scp32");
data = dhtSensor.getTempAndHumidity(); // Update temperature and humidity readings
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("Luz: " + String(lux)); // Print light sensor reading
Serial.println("---");
delay(1000);
if (mensaje.text == "Temperatura") {
bot_didactico.sendMessage(mensaje.sender.id, "Temperatura: " + String(data.temperature, 2) + "°C");
if (data.temperature > 40) {
bot_didactico.sendMessage(mensaje.sender.id, "Está muy caliente");
} else if (data.temperature < 15) {
bot_didactico.sendMessage(mensaje.sender.id, "Está muy frío");
} else {
bot_didactico.sendMessage(mensaje.sender.id, "Está ideal");
}
}
if (mensaje.text == "Humedad") {
bot_didactico.sendMessage(mensaje.sender.id, "Humedad: " + String(data.humidity, 1) + "%");
if (data.humidity >= 80 && data.humidity <= 100) {
bot_didactico.sendMessage(mensaje.sender.id, "Está muy húmedo");
} else if (data.humidity >= 0 && data.humidity < 60) {
bot_didactico.sendMessage(mensaje.sender.id, "Está muy seco");
} else {
bot_didactico.sendMessage(mensaje.sender.id, "Está ideal");
}
}
if (mensaje.text == "Luz") {
bot_didactico.sendMessage(mensaje.sender.id, "Luz: " + String(lux));
if (lux < 998) {
bot_didactico.sendMessage(mensaje.sender.id, "Puede que ya sea de noche");
} else if (lux > 1500 && lux < 3500) {
bot_didactico.sendMessage(mensaje.sender.id, "Vaya, parece que ya está amaneciendo");
} else if (lux > 3600) {
bot_didactico.sendMessage(mensaje.sender.id, "Qué gran sol el que está haciendo, puede que ya sea medio día");
}
}
if (mensaje.text == "Estado")
// Send all sensor readings at once
bot_didactico.sendMessage(mensaje.sender.id,"Temper");
}
}