#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
//REd Wifi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//TelegramBot token
const String botToken = "7685692805:AAFp2s6oqgQ49LTtm4NxjnS6zxdBrn5rSdI";
//vars
int botMTBS = 1000;
unsigned long botLastScan;
#define ledRED 23
#define ledGREEN 22
bool status_RED, status_GREEN;
//Onjetos?
WiFiClientSecure secured_client;
UniversalTelegramBot bot(botToken, secured_client);
void setup() {
// put your setup code here, to run once:
pinMode(ledRED, OUTPUT);
pinMode(ledGREEN, OUTPUT);
// Serial monitor
Serial.begin(115200);
// Conexion Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a WiFi..");
}
}
void wifiConnect(){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void loop() {
if (millis() > botLastScan + botMTBS)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages){
Serial.println("Respuesta obtenida!");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
botLastScan = millis();
}
}
void handleNewMessages(int numNewMessages){
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
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 == "/ledREDon"){
bot.sendMessage(chat_id, "Led Red ON", "");
led_RED(true);
}
if (text == "/ledREDoff"){
bot.sendMessage(chat_id, "Led Red OFF", "");
led_RED(false);
}
if (text == "/ledGREENon"){
bot.sendMessage(chat_id, "Led Red ON", "");
led_GREEN(true);
}
if (text == "/ledGREENoff"){
bot.sendMessage(chat_id, "Led Red OFF", "");
led_GREEN(false);
}
if (text == "/statusRED") {
if (digitalRead(ledRED)){
bot.sendMessage(chat_id, "LED Red ON", "");
}
else{
bot.sendMessage(chat_id, "LED Red OFF", "");
}
}
if (text == "/statusGREEN") {
if (digitalRead(ledGREEN)){
bot.sendMessage(chat_id, "LED Green ON", "");
}
else{
bot.sendMessage(chat_id, "LED Green OFF", "");
}
}
if (text == "/start"){
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + "This is Flash Led Bot example.\n";
welcome += "This is Flash Led Bot example.\n\n";
welcome += "/ledREDon : to switch the Led ON \n";
welcome += "/ledREDoff : to switch the Led OFF \n";
welcome += "/ledGREENon : to switch the Led ON \n";
welcome += "/ledGREENoff : to switch the Led OFF \n";
welcome += "/statusRED : Returns current status of LED \n";
welcome += "/statusGREEN : Returns current status of LED \n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void led_RED(bool isOn){
if(isOn){
digitalWrite(ledRED, HIGH);
}
else{
digitalWrite(ledRED, LOW);
}
}
void led_GREEN(bool isOn){
if(isOn){
digitalWrite(ledGREEN, HIGH);
}
else{
digitalWrite(ledGREEN, LOW);
}
}