Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /* Adaptado de el ejemplo original por Cesar Riojas
  2.     www.arduinocentermx.blogspot.mx
  3.  * */
  4. #include <ESP8266WiFi.h>
  5. #include <WiFiClientSecure.h>
  6. #include <UniversalTelegramBot.h>
  7.  
  8. // Inicializamos la conexion WIFI con el Router
  9. char ssid[] = "TUSSID";     // el nombre de tu Red
  10. char password[] = "TUPASSWORD"; // la clave de tu Red
  11.  
  12. // Inicializar Telegram BOT
  13. #define BOTtoken "TUTOKEN"  // el token de tu BOT, lo obtenemos de BotFather
  14.  
  15. WiFiClientSecure client;
  16. UniversalTelegramBot bot(BOTtoken, client);
  17.  
  18. int Bot_mtbs = 50; //Tiempo medio entre el escaneo de mensajes
  19. long Bot_lasttime;   //exploracion de el ultimo mensaje
  20. bool Start = false;
  21. String chat_id2;
  22. bool flag = false;
  23.  
  24. void handleNewMessages(int numNewMessages) {
  25.   Serial.println("handleNewMessages");
  26.   Serial.println(String(numNewMessages));
  27.  
  28.   for (int i = 0; i < numNewMessages; i++) {
  29.     String chat_id = String(bot.messages[i].chat_id);
  30.     chat_id2 = chat_id;
  31.     String text = bot.messages[i].text;
  32.  
  33.     String from_name = bot.messages[i].from_name;
  34.     if (from_name == "") from_name = "Guest";
  35.  
  36.     if (text == "/start") {
  37.       String welcome = "Bienvenido a el BOT de Arduino Center, " + from_name + ".\n";
  38.       welcome += "Escribe /opciones si quieres ver lo que puedo hacer.\n\n";
  39.       //welcome += "/send_test_action : to send test chat action message\n";
  40.       bot.sendMessage(chat_id, welcome);
  41.     }
  42.  
  43.     if (text == "/opciones") {
  44.       String keyboardJson = "[[\"/ledOn\", \"/ledOff\"],[\"/sensor\"]]";
  45.       bot.sendMessageWithReplyKeyboard(chat_id, "Selecciona una de las siguientes opciones:", "", keyboardJson, true);
  46.     }
  47.  
  48.     if (text == "/ledOn") {
  49.       digitalWrite(13, HIGH);
  50.       bot.sendMessage(chat_id, "Led Encendido!");
  51.     }
  52.  
  53.     if (text == "/ledOff") {
  54.       digitalWrite(13, LOW);
  55.       bot.sendMessage(chat_id, "Led Apagado!");
  56.     }
  57.  
  58.     if (text == "/sensor") {
  59.       if (digitalRead(4) == 0) {
  60.         bot.sendMessage(chat_id, "Sensor en Reposo!");
  61.       } else {
  62.         bot.sendMessage(chat_id, "Sensor Activado!!");
  63.       }
  64.     }
  65.   }
  66. }
  67.  
  68.  
  69. void setup() {
  70.   Serial.begin(115200);
  71.  
  72.   // Establecer el modo WiFi y desconectarse de un AP si fue Anteriormente conectada
  73.   WiFi.mode(WIFI_STA);
  74.   WiFi.disconnect();
  75.   delay(100);
  76.  
  77.   // Intentar conectarse a la red
  78.   Serial.print("Conectando al Wifi: ");
  79.   Serial.println(ssid);
  80.   WiFi.begin(ssid, password);
  81.  
  82.   while (WiFi.status() != WL_CONNECTED) {
  83.     Serial.print(".");
  84.     delay(500);
  85.   }
  86.  
  87.   Serial.println("");
  88.   Serial.println("WiFi conectada");
  89.   pinMode(13, OUTPUT);
  90.   pinMode(4, INPUT_PULLUP);
  91. }
  92.  
  93. void loop() {
  94.   if (millis() > Bot_lasttime + Bot_mtbs)  {
  95.     int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  96.  
  97.     while (numNewMessages) {
  98.       Serial.println("got response");
  99.       handleNewMessages(numNewMessages);
  100.       numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  101.     }
  102.  
  103.     if (digitalRead(4) == 1) {
  104.       if (flag == false) {
  105.         bot.sendMessage(chat_id2, "Se Activo el Sensor!!!");
  106.         flag = true;
  107.       }
  108.     } else {
  109.       flag = false;
  110.     }
  111.  
  112.  
  113.     Bot_lasttime = millis();
  114.   }
  115. }