#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "esp_camera.h"
#include <UniversalTelegramBot.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <esp32-hal-timer.h>
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String chatId = "";
String BOTtoken = "";
WiFiClientSecure clientTCP;
UniversalTelegramBot bot(BOTtoken, clientTCP);
int botRequestDelay = 1000;
long lastTimeBotRan;
int scheduledHour = 12;
int scheduledMinute = 0;
volatile bool flagTimer = false;
hw_timer_t *timer = NULL;
void IRAM_ATTR onTimer() {
flagTimer = true;
}
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
clientTCP.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("ESP32-CAM IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Conectando ao servidor NTP...");
if (timeClient.update()) {
Serial.println(" Conectado!");
time_t currentTime = timeClient.getEpochTime();
struct tm *timeinfo = localtime(¤tTime);
char formattedTime[20];
strftime(formattedTime, sizeof(formattedTime), "%d/%m/%Y %H:%M:%S", timeinfo);
Serial.print("Hora do sistema: ");
Serial.println(formattedTime);
} else {
Serial.println(" Falha na conexão.");
}
timeClient.setTimeOffset(-3 * 3600);
timer = timerBegin(0, 80, true); // Timer 0, div 80
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 60000000, true);
timerAlarmEnable(timer);
}
void loop() {
if (millis() > lastTimeBotRan + botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
if (flagTimer) {
sendScheduledMessage();
flagTimer = false;
}
delay(1000);
}
void handleNewMessages(int numNewMessages) {
Serial.print("Handle New Messages: ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != chatId) {
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
String text = bot.messages[i].text;
Serial.println(text);
if (text == "/flash") {
bot.sendMessage(chatId, "Flash");
} else if (text == "/photo") {
bot.sendMessage(chatId, "Photo");
} else if (text.startsWith("/settime ")) {
handleSetTime(text);
} else if (text == "/start") {
String welcome = "Bem-vindo(a), use os seguintes comandos para interagir com a câmera:\n";
welcome += "/photo : Tira uma foto.\n";
welcome += "/flash : Ativa/desativa o flash.\n";
welcome += "/settime HH:MM : Agenda um horário para tirar fotos.\n\n";
welcome += "Você receberá uma foto nova no horário agendado..\n";
bot.sendMessage(chatId, welcome, "");
}
}
}
void handleSetTime(String text) {
text.remove(0, 9);
int setHour = text.substring(0, 2).toInt();
int setMinute = text.substring(3, 5).toInt();
scheduledHour = setHour;
scheduledMinute = setMinute;
String confirmation = "Foto agendada para " + String(scheduledHour) + ":" + String(scheduledMinute);
bot.sendMessage(chatId, confirmation, "");
}
void sendScheduledMessage() {
time_t currentTime = timeClient.getEpochTime();
struct tm *timeinfo = localtime(¤tTime);
int currentHour = timeinfo->tm_hour;
int currentMinute = timeinfo->tm_min;
char formattedDate[20];
strftime(formattedDate, sizeof(formattedDate), "%d/%m/%Y", timeinfo);
if (currentHour == scheduledHour && currentMinute == scheduledMinute) {
String message = "Foto tirada às " + String(scheduledHour) + ":" + String(scheduledMinute) + " do dia " + formattedDate + ".";
bot.sendMessage(chatId, message, "");
}
}