ElectronicaAplicada

Control alumeado bot telegram e ESP

May 6th, 2021
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Marga L. Periago/  @Margaperi
  3.   Complete project details at https://RandomNerdTutorials.com/telegram-group-esp32-esp8266/
  4.  
  5.   Project created using Brian Lough's Universal Telegram Bot Library: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
  6. */
  7.  
  8. #ifdef ESP32
  9.   #include <WiFi.h>
  10. #else
  11.   #include <ESP8266WiFi.h>
  12. #endif
  13. #include <WiFiClientSecure.h>
  14. #include <UniversalTelegramBot.h>   // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
  15. #include <ArduinoJson.h>
  16.  
  17. // Replace with your network credentials
  18. const char* ssid = "aquí o nome da rede";
  19. const char* password = "aquí o seu contrasinal";
  20.  
  21. // Initialize Telegram BOT
  22. #define BOTtoken "17XXXXX563:AAG4kMA00jJ_jXXXXl-E1O0UJTkaaHVFWvw"  // o teu token de bot (obtén de Botfather)
  23.  
  24. // Usa @myidbot para descubrir o ID de chat dun individuo
  25. // Ten en conta tamén que hai que facer clic en "iniciar" nun bot antes de que poida
  26. // envíate unha mensaxe
  27. //Para coñecer o ID dun grupo, abre telegram web e copia os números precedidos de un -
  28. #define CHAT_ID "aquí o ID"
  29.  
  30. #ifdef ESP8266
  31.   X509List cert(TELEGRAM_CERTIFICATE_ROOT);
  32. #endif
  33.  
  34. WiFiClientSecure client;
  35. UniversalTelegramBot bot(BOTtoken, client);
  36.  
  37. // Checks for new messages every 1 second.
  38. int botRequestDelay = 1000;
  39. unsigned long lastTimeBotRan;
  40.  
  41. //const int ledPin = 2;
  42. const int led12=12;
  43. const int led14=14;
  44. bool estadoled12 = LOW;
  45. bool estadoled14 = LOW;
  46.  
  47.  
  48. void xestionarNovasMensaxes (int numMensaxesNovas) {
  49.   Serial.println("xestionar novas mensaxes ");
  50.   Serial.println(String(numMensaxesNovas));
  51.  
  52.   for (int i=0; i<numMensaxesNovas; i++) {
  53.     // ID de chat do solicitante
  54.     String chat_id = String(bot.messages[i].chat_id);
  55.     if (chat_id != CHAT_ID){
  56.       bot.sendMessage(chat_id, "Usuario non autorizado", "");
  57.       continue;
  58.     }
  59.    
  60.     // Print the received message
  61.     String text = bot.messages[i].text;
  62.     Serial.println(text);
  63.  
  64.     String from_name = bot.messages[i].from_name;
  65.  
  66.     if (text == "/AcendeLuz1") {
  67.       bot.sendMessage(chat_id, "Luz 1 acesa ", "");
  68.       estadoled14 = HIGH;
  69.       digitalWrite(led14, estadoled14);
  70.     }
  71.    
  72.     if (text == "/ApagaLuz1") {
  73.       bot.sendMessage(chat_id, "Luz 1 Apagada", "");
  74.       estadoled14 = LOW;
  75.       digitalWrite(led14, estadoled14);
  76.     }
  77.  
  78.      if (text == "/AcendeLuz2") {
  79.       bot.sendMessage(chat_id, "Luz 2 Acesa ", "");
  80.       estadoled12 = HIGH;
  81.       digitalWrite(led12, estadoled12);
  82.     }
  83.    
  84.     if (text == "/ApagaLuz2") {
  85.       bot.sendMessage(chat_id, " Luz 2 Apagada", "");
  86.       estadoled12 = LOW;
  87.       digitalWrite(led12, estadoled12);
  88.     }
  89.    
  90.     if (text == "/Estado") {
  91.       if (digitalRead(estadoled14)){
  92.         bot.sendMessage(chat_id, "Luz 1 acesa", "");
  93.       }
  94.       else{
  95.         bot.sendMessage(chat_id, "Luz 1 apagada", "");
  96.       }
  97.       if (digitalRead(estadoled12)){
  98.         bot.sendMessage(chat_id, "Luz 2 acesa", "");
  99.       }
  100.       else{
  101.         bot.sendMessage(chat_id, "Luz 2 apagada", "");
  102.       }
  103.     }
  104.  
  105.  
  106.  
  107.  if (text == "/Axuda")
  108.     {
  109.       String axuda = "Benvida o sistema de control de luces con Esp32 ou ESP8266, " ".\n";
  110.       axuda += "Estas son as túas opcións.\n\n";
  111.       axuda += "/AcendeLuz1: para acender a Luz 1 \n";
  112.       axuda += "/ApagaLuz1: para apagar a luz 1 \n";
  113.       axuda += "/AcendeLuz2: para acender a Luz 2 \n";
  114.       axuda += "/ApagaLuz2: para apagar a luz 2 \n";
  115.       axuda += "/Estado : devolve o estado actual das luces\n";
  116.       axuda += "/Axuda: Imprime este menú \n";
  117.       axuda += "Lembra co sistema distingue entre maiúculas e minúsculas \n";
  118.       bot.sendMessage(chat_id, axuda, "");
  119.     }
  120.  
  121.  
  122.    
  123.   }
  124.  
  125. }
  126.  
  127. void setup() {
  128.   Serial.begin(115200);
  129.  
  130.   #ifdef ESP8266
  131.     configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
  132.     client.setTrustAnchors(&cert); //  Agregar certificado raíz para API.telegram.org
  133.   #endif
  134.  
  135.   pinMode(led12, OUTPUT);
  136.   pinMode(led14, OUTPUT);
  137.   digitalWrite(led12, estadoled12);
  138.   digitalWrite(led14, estadoled14);
  139.  
  140.   // Connect to Wi-Fi
  141.   WiFi.mode(WIFI_STA);
  142.   WiFi.begin(ssid, password);
  143.   #ifdef ESP32
  144.     client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Engade certificado raíz para api.telegram.org
  145.   #endif
  146.   while (WiFi.status() != WL_CONNECTED) {
  147.     delay(1000);
  148.     Serial.println("Conectando á WiFi..");
  149.   }
  150.   // Print ESP32 Local IP Address
  151.   Serial.println(WiFi.localIP());
  152.  
  153.   bot.sendMessage(CHAT_ID, "Bot funcionando", "");
  154. }
  155.  
  156. void loop() {
  157.   if (millis() > lastTimeBotRan + botRequestDelay) {
  158.     int numMensaxesNovas = bot.getUpdates(bot.last_message_received + 1);
  159.  
  160.     while(numMensaxesNovas) {
  161.       Serial.println("houbo  resposta ");
  162.       xestionarNovasMensaxes (numMensaxesNovas);
  163.       numMensaxesNovas = bot.getUpdates(bot.last_message_received + 1);
  164.     }
  165.     lastTimeBotRan = millis();
  166.   }
  167. }
  168.    
Advertisement
Add Comment
Please, Sign In to add comment