#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ESP32Servo.h>
#include <time.h>
// Definiciones de pines y objetos
const int pin_led = 17;
const int servoPin = 18;
Servo myservo;
// Credenciales y configuraciones
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const String botToken = "6936919099:AAHZGFIjh4t0j6t8BhHl3jztSSN6E8R8rOQ";
const unsigned long botMTBS = 1000;
unsigned long botLastScan;
WiFiClientSecure secured_client;
UniversalTelegramBot bot(botToken, secured_client);
bool automatico = true;
void setup() {
// Configuración inicial
myservo.attach(servoPin);
pinMode(pin_led, OUTPUT);
Serial.begin(115200);
connectToWiFi();
setTime();
}
void loop() {
if (millis() - botLastScan > botMTBS) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
botLastScan = millis();
}
}
void connectToWiFi() {
Serial.print("Conectando a WiFi");
WiFi.begin(ssid, password);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Conectado!");
}
void setTime() {
configTime(-5 * 3600, 0, "pool.ntp.org");
while (!time(nullptr)) {
delay(1000);
Serial.println(" Actualizando hora...");
}
Serial.println(" Hora actualizada!");
}
void handleNewMessages(int numNewMessages) {
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
for (int i = 0; i < numNewMessages; i++) {
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") {
from_name = "Guest";
}
if (text == "/comida") {
dispenseFood(chat_id);
} else if (text == "/activar") {
automatico = true;
bot.sendMessage(chat_id, "Dispensador automático activado", "");
} else if (text == "/desactivar") {
automatico = false;
bot.sendMessage(chat_id, "Dispensador automático desactivado", "");
} else if (text == "/start") {
sendWelcomeMessage(chat_id);
}
}
}
void dispenseFood(String chat_id) {
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
if (automatico || (timeinfo.tm_hour == 5 || timeinfo.tm_hour == 13 || timeinfo.tm_hour == 19)) {
for (int i = 0; i < 3; i++) { // Hacer que el servo gire 3 veces
myservo.write(180); // Girar en sentido horario
delay(2000); // Ajustar según sea necesario
}
digitalWrite(pin_led, HIGH);
delay(3000); // Mantener el LED encendido durante 3 segundos
digitalWrite(pin_led, LOW);
bot.sendMessage(chat_id, "Se depositó comida", "");
} else {
bot.sendMessage(chat_id, "No se puede dispensar en este momento", "");
}
}
void sendWelcomeMessage(String chat_id) {
String welcome = "Welcome ALIMENTA A TU MASCOTA BOT. \n";
welcome += "Estos son los comandos que puedes elegir.\n\n";
welcome += "/comida : Se dispensa comida. \n";
welcome += (automatico == false) ? "/activar : Dispensador automático desactivado" : "/desactivar : Dispensador automático activado";
welcome += " (5am - 1pm - 7pm) \n";
bot.sendMessage(chat_id, welcome, "Markdown");
}