Advertisement
TolentinoCotesta

AsyncTelegram reset reason

Mar 7th, 2021
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.24 KB | None | 0 0
  1. /*
  2.   Name:        keyboards.ino
  3.   Created:     20/06/2020
  4.   Author:      Tolentino Cotesta <cotestatnt@yahoo.com>
  5.   Description: a more complex example that do:
  6.                1) if a "/inline_keyboard" text message is received, show the
  7.   inline custom keyboard, if a "/reply_keyboard" text message is received, show
  8.   the reply custom keyboard, otherwise reply the sender with "Try
  9.   /reply_keyboard or /inline_keyboard" message 2) if "LIGHT ON" inline keyboard
  10.   button is pressed turn on the LED and show a message 3) if "LIGHT OFF" inline
  11.   keyboard button is pressed, turn off the LED and show a message 4) if "GitHub"
  12.   inline keyboard button is pressed, open a browser window with URL
  13.   "https://github.com/cotestatnt/AsyncTelegram"
  14. */
  15.  
  16. #include <AsyncTelegram.h>
  17. #include <rom/rtc.h>
  18.  
  19. const char * verbose_print_reset_reason(RESET_REASON reason) {
  20.   switch (reason) {
  21.   case 1:
  22.     return ("Vbat power on reset");
  23.     break;
  24.   case 3:
  25.     return ("Software reset digital core");
  26.     break;
  27.   case 4:
  28.     return ("Legacy watch dog reset digital core");
  29.     break;
  30.   case 5:
  31.     return ("Deep Sleep reset digital core");
  32.     break;
  33.   case 6:
  34.     return ("Reset by SLC module, reset digital core");
  35.     break;
  36.   case 7:
  37.     return ("Timer Group0 Watch dog reset digital core");
  38.     break;
  39.   case 8:
  40.     return ("Timer Group1 Watch dog reset digital core");
  41.     break;
  42.   case 9:
  43.     return ("RTC Watch dog Reset digital core");
  44.     break;
  45.   case 10:
  46.     return ("Instrusion tested to reset CPU");
  47.     break;
  48.   case 11:
  49.     return ("Time Group reset CPU");
  50.     break;
  51.   case 12:
  52.     return ("Software reset CPU");
  53.     break;
  54.   case 13:
  55.     return ("RTC Watch dog Reset CPU");
  56.     break;
  57.   case 14:
  58.     return ("for APP CPU, reseted by PRO CPU");
  59.     break;
  60.   case 15:
  61.     return ("Reset when the vdd voltage is not stable");
  62.     break;
  63.   case 16:
  64.     return ("RTC Watch dog reset digital core and rtc module");
  65.     break;
  66.   default:
  67.     return ("NO_MEAN");
  68.   }
  69. }
  70.  
  71. AsyncTelegram myBot;
  72. ReplyKeyboard myReplyKbd;   // reply keyboard object helper
  73. InlineKeyboard myInlineKbd; // inline keyboard object helper
  74.  
  75. bool isKeyboardActive; // store if the reply keyboard is shown
  76.  
  77. const char *ssid = "xxxxxxx"; // SSID WiFi network
  78. const char *pass = "xxxxxxx"; // Password  WiFi networ
  79. const char* token =  "xxxxxxxxx:AAFuMQ1E2smMxxxxxxxxokzwxgpvNBzJwg"; //
  80. // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
  81.  
  82. #define LIGHT_ON_CALLBACK "lightON" // callback data sent when "LIGHT ON" button is pressed
  83. #define LIGHT_OFF_CALLBACK "lightOFF" // callback data sent when "LIGHT OFF" button is pressed
  84.  
  85. #ifndef LED_BUILTIN
  86. #define LED_BUILTIN 2
  87. #endif
  88.  
  89. const uint8_t LED = 4;
  90.  
  91. #if defined(ESP32)
  92. // WiFi event handler
  93. void WiFiEvent(WiFiEvent_t event) {
  94.   switch (event) {
  95.   case SYSTEM_EVENT_STA_GOT_IP:
  96.     Serial.print("\nWiFi connected! IP address: ");
  97.     Serial.println(WiFi.localIP());
  98.  
  99.     break;
  100.   case SYSTEM_EVENT_STA_DISCONNECTED:
  101.     Serial.println("\nWiFi lost connection");
  102.     WiFi.setAutoReconnect(true);
  103.     myBot.reset();
  104.     break;
  105.   default:
  106.     break;
  107.   }
  108. }
  109. #endif
  110.  
  111. void printHeapStats() {
  112.   time_t now = time(nullptr);
  113.   struct tm tInfo = *localtime(&now);
  114.   static uint32_t infoTime;
  115.   if (millis() - infoTime > 10000) {
  116.     infoTime = millis();
  117. #ifdef ESP32
  118.     Serial.printf("\n%02d:%02d:%02d - Total free: %6d - Max block: %6d\n",
  119.                   tInfo.tm_hour, tInfo.tm_min, tInfo.tm_sec,
  120.                   heap_caps_get_free_size(0),
  121.                   heap_caps_get_largest_free_block(0));
  122. #elif defined(ESP8266)
  123.     uint32_t free;
  124.     uint16_t max;
  125.     ESP.getHeapStats(&free, &max, nullptr);
  126.     Serial.printf("\nTotal free: %5d - Max block: %5d\n", free, max);
  127. #endif
  128.   }
  129. }
  130.  
  131. void setup() {
  132.   pinMode(LED_BUILTIN, OUTPUT);
  133.   pinMode(LED, OUTPUT);
  134.   // initialize the Serial
  135.   Serial.begin(115200);
  136.  
  137.   WiFi.setAutoConnect(true);
  138.   WiFi.mode(WIFI_STA);
  139.  
  140. #if defined(ESP32)
  141.   Serial.printf("setup() running on core  %d\n", xPortGetCoreID());
  142.   WiFi.onEvent(WiFiEvent);
  143. #endif
  144.   Serial.printf("Free heap: %d\n", ESP.getFreeHeap());
  145.   Serial.print("\n\nStart connection to WiFi...");
  146.   delay(100);
  147.  
  148.   // connects to access point
  149.   WiFi.begin(ssid, pass);
  150.   delay(500);
  151.   while (WiFi.status() != WL_CONNECTED) {
  152.     Serial.print('.');
  153.     delay(500);
  154.   }
  155.  
  156.   Serial.println("CPUs reset reason:");
  157.   const char *resetCPU0 = verbose_print_reset_reason(rtc_get_reset_reason(0));
  158.   const char *resetCPU1 = verbose_print_reset_reason(rtc_get_reset_reason(1));
  159.   Serial.println(resetCPU0);
  160.   Serial.println(resetCPU1);
  161.   Serial.println();
  162.  
  163.   // To ensure certificate validation, WiFiClientSecure needs time upadated
  164.   // myBot.setInsecure(false);
  165.   myBot.setClock("CET-1CEST,M3.5.0,M10.5.0/3");
  166.  
  167.   // Set the Telegram bot properies
  168.   myBot.setUpdateTime(1000);
  169.   myBot.setTelegramToken(token);
  170.  
  171.   // Check if all things are ok
  172.   Serial.print("\nTest Telegram connection... ");
  173.   myBot.begin() ? Serial.println("OK") : Serial.println("NOK");
  174.  
  175.   // Add reply keyboard
  176.   isKeyboardActive = false;
  177.   // add a button that send a message with "Simple button" text
  178.   myReplyKbd.addButton("Button1");
  179.   myReplyKbd.addButton("Button2");
  180.   myReplyKbd.addButton("Button3");
  181.   // add a new empty button row
  182.   myReplyKbd.addRow();
  183.   // add another button that send the user position (location)
  184.   myReplyKbd.addButton("Send Location", KeyboardButtonLocation);
  185.   // add another button that send the user contact
  186.   myReplyKbd.addButton("Send contact", KeyboardButtonContact);
  187.   // add a new empty button row
  188.   myReplyKbd.addRow();
  189.   // add a button that send a message with "Hide replyKeyboard" text
  190.   // (it will be used to hide the reply keyboard)
  191.   myReplyKbd.addButton("/hide_keyboard");
  192.   // resize the keyboard to fit only the needed space
  193.   myReplyKbd.enableResize();
  194.  
  195.   // Add sample inline keyboard
  196.   myInlineKbd.addButton("ON", LIGHT_ON_CALLBACK, KeyboardButtonQuery);
  197.   myInlineKbd.addButton("OFF", LIGHT_OFF_CALLBACK, KeyboardButtonQuery);
  198.   myInlineKbd.addRow();
  199.   myInlineKbd.addButton("GitHub",
  200.                         "https://github.com/cotestatnt/AsyncTelegram/",
  201.                         KeyboardButtonURL);
  202.  
  203.   const char *botName = myBot.userName.c_str();
  204.   Serial.printf("Nome del bot: @%s\n%s\n%s", botName, resetCPU0, resetCPU1);
  205.  
  206.   time_t now = time(nullptr);
  207.   struct tm tInfo = *localtime(&now);
  208.  
  209.   char welcome_msg[128];
  210.   snprintf(welcome_msg, 128,
  211.            PSTR("%02d:%02d:%02d - <b>@%s</b> online!\n%s\n%s"), tInfo.tm_hour,
  212.            tInfo.tm_min, tInfo.tm_sec, botName, resetCPU0, resetCPU1);
  213.  
  214.   TBMessage msg;
  215.   msg.sender.id =
  216.       436865110; // You can discover your own chat id, with "Json Dump Bot"
  217.   msg.isHTMLenabled = true;
  218.   myBot.sendMessage(msg, welcome_msg);
  219. }
  220.  
  221. void loop() {
  222.  
  223.   printHeapStats();
  224.  
  225.   // In the meantime LED_BUILTIN will blink with a fixed frequency
  226.   // to evaluate async and non-blocking working of library
  227.   static uint32_t ledTime = millis();
  228.   if (millis() - ledTime > 200) {
  229.     ledTime = millis();
  230.     digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  231.   }
  232.  
  233.   // a variable to store telegram message data
  234.   TBMessage msg;
  235.  
  236.   // if there is an incoming message...
  237.   if (myBot.getNewMessage(msg)) {
  238.     // check what kind of message I received
  239.     String tgReply;
  240.     MessageType msgType = msg.messageType;
  241.  
  242.     switch (msgType) {
  243.     case MessageText:
  244.       // received a text message
  245.       tgReply = msg.text;
  246.       Serial.print("\nText message received: ");
  247.       Serial.println(tgReply);
  248.  
  249.       // check if is show keyboard command
  250.       if (tgReply.equalsIgnoreCase("/reply_keyboard")) {
  251.         // the user is asking to show the reply keyboard --> show it
  252.         myBot.sendMessage(msg, "This is reply keyboard:", myReplyKbd);
  253.         isKeyboardActive = true;
  254.       } else if (tgReply.equalsIgnoreCase("/inline_keyboard")) {
  255.         myBot.sendMessage(msg, "This is inline keyboard:", myInlineKbd);
  256.  
  257.       }
  258.  
  259.       // check if the reply keyboard is active
  260.       else if (isKeyboardActive) {
  261.         // is active -> manage the text messages sent by pressing the reply
  262.         // keyboard buttons
  263.         if (tgReply.equalsIgnoreCase("/hide_keyboard")) {
  264.           // sent the "hide keyboard" message --> hide the reply keyboard
  265.           myBot.removeReplyKeyboard(msg, "Reply keyboard removed");
  266.           isKeyboardActive = false;
  267.         } else {
  268.           // print every others messages received
  269.           myBot.sendMessage(msg, msg.text);
  270.         }
  271.       }
  272.  
  273.       // the user write anything else and the reply keyboard is not active -->
  274.       // show a hint message
  275.       else {
  276.         myBot.sendMessage(msg, "Try /reply_keyboard or /inline_keyboard");
  277.       }
  278.       break;
  279.  
  280.     case MessageQuery:
  281.       // received a callback query message
  282.       tgReply = msg.callbackQueryData;
  283.       Serial.print("\nCallback query message received: ");
  284.       Serial.println(tgReply);
  285.  
  286.       if (tgReply.equalsIgnoreCase(LIGHT_ON_CALLBACK)) {
  287.         // pushed "LIGHT ON" button...
  288.         Serial.println("\nSet light ON");
  289.         digitalWrite(LED, HIGH);
  290.         // terminate the callback with an alert message
  291.         myBot.endQuery(msg, "Light on", true);
  292.       } else if (tgReply.equalsIgnoreCase(LIGHT_OFF_CALLBACK)) {
  293.         // pushed "LIGHT OFF" button...
  294.         Serial.println("\nSet light OFF");
  295.         digitalWrite(LED, LOW);
  296.         // terminate the callback with a popup message
  297.         myBot.endQuery(msg, "Light off");
  298.       }
  299.  
  300.       break;
  301.  
  302.     case MessageLocation:
  303.       // received a location message --> send a message with the location
  304.       // coordinates
  305.       char bufL[50];
  306.       snprintf(bufL, sizeof(bufL), "Longitude: %f\nLatitude: %f\n",
  307.                msg.location.longitude, msg.location.latitude);
  308.       myBot.sendMessage(msg, bufL);
  309.       Serial.println(bufL);
  310.       break;
  311.  
  312.     case MessageContact:
  313.       char bufC[50];
  314.       snprintf(bufC, sizeof(bufC), "Contact information received: %s - %s\n",
  315.                msg.contact.firstName, msg.contact.phoneNumber);
  316.       // received a contact message --> send a message with the contact
  317.       // information
  318.       myBot.sendMessage(msg, bufC);
  319.       Serial.println(bufC);
  320.       break;
  321.  
  322.     default:
  323.       break;
  324.     }
  325.   }
  326. }
  327.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement