cotestatnt / AsyncTelegram2

Powerful, flexible and secure Arduino Telegram BOT library. Hardware independent, it can be used with any MCU capable of handling an SSL connection.
MIT License
83 stars 25 forks source link

Timeout on ESP32 #97

Closed MudiStar closed 1 year ago

MudiStar commented 1 year ago

Hi it is me again,

so I started to log the main loop on my esp32, to see if something is blocking. What I found is, that the loop is blocked every ~5 mins for approximately 650 ms. Sometimes even longer, up to 8s.

The esp32 tolerates this behavior but not the esp8266 but this is a different topic. Is there a function which is called every 5 mins?

Timeout

cotestatnt commented 1 year ago

Hi @MudiStar, the AsyncTelegram2 library, in order to minimize the connection time to the Telegram server leaves the WiFiClient client; active without closing the socket (client.stop();)

Unfortunately the server closes the communication after a while and therefore it's necessary call the connect() method again which unfortunately is blocking (and beyond the control of the library itself).

This timeout enforced by the api.telegram.org server is just about 5 minutes

If you have other processes that are excessively affected by this, I suggest you run everything related to Telegram on a dedicated task (unfortunately it's only possible with ESP32)

static void checkTelegram(void* args) {
  while (true) {
    if (WiFi.status() == WL_CONNECTED) {
        TBMessage msg;
        // if there is an incoming message...
        if (myBot.getNewMessage(msg)) {
          MessageType msgType = msg.messageType;

          if (msgType == MessageText) {
              // Received a text message
              String replyStr = "Message received: ";
              replyStr += msg.text;
              Serial.println(replyStr);
              myBot.sendMessage(msg, replyStr);
            }
        }
    }    
    // feed the watchdog
    delay(100);
  }
  // Delete this task on exit (should never occurs)
  vTaskDelete(NULL);
}

.....

TaskHandle_t TelegramTaskHandle;

void setup() {
.....
xTaskCreatePinnedToCore(checkTelegram, "checkTelegram", 10000, NULL, 8, &TelegramTaskHandle, 1);
....
}
MudiStar commented 1 year ago

Ok thanks Got it