shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
147 stars 34 forks source link

No exit from function getNewMessage, testConnection if no internet or no correct work (glitch) telegram api #40

Open hamster66 opened 4 years ago

hamster66 commented 4 years ago

Hello. I’m doing a small project on ESP8266 using your library. Faced with the suspension of cycle loop() on functions getNewMessage, testConnection in the event of the sudden disappearance of the Internet or glitches on the Telegram api server. Is it possible to add a timeout to the library to perform these functions? Is there any other solution that can prevent freezing on these functions? Thanks in advance for your reply.

shurillu commented 4 years ago

Hello hamster66, the problem you faced is dued to the WiFiClientSecure class. With a very quick googling you can found this and this. So a solution to your problem could be adding something like that in the ctbot.cpp:

String CTBot::sendCommand(String command, String parameters)
{
#if CTBOT_USE_FINGERPRINT == 0
    WiFiClientSecure telegramServer;
#else
    BearSSL::WiFiClientSecure telegramServer;
    telegramServer.setFingerprint(m_fingerprint);
#endif  

    // ----------> ADD THE FOLLOWING LINE (timeout in milliseconds).
    telegramServer.setTimeout(1000);

    // check for using symbolic URLs
    if (m_useDNS) {
        // try to connect with URL
        if (!telegramServer.connect(TELEGRAM_URL, TELEGRAM_PORT)) {

Let me know if it work. Cheers,

Stefano

hamster66 commented 4 years ago

Hi Stefano. I'm sorry, but apparently this solution does not work. It is possible that hangs when disconnecting / connecting the Internet became a little less, but they are definitely present. Most likely there is still a place where the function may hang

shurillu commented 4 years ago

Mmmm I see. Have you tired to check the connection BEFORE calling the getNewMessage() method? Something like this:

if (WiFi.status() != WL_CONNECTED) {
   // not connected, do connection stuff or wait the connection is up
}
// call getNewMessage() method

Stefano

hamster66 commented 4 years ago

The problem is not in wifi. My situation is when there is a normal wifi connection but the link to the Telegram server suddenly disappears or the Telegram server temporarily does not work correctly (failure). I already make a separate ping of api.telegram.org using my own resources and if it does not pass I do not call the functions of your library. This greatly improved the situation, but if the Internet disappears during the call getNewMessage or testConnection, there is a high probability of freezing in these functions.

hamster66 commented 4 years ago

Is there really no more ideas? :(

HyperConnect1 commented 4 years ago

Hello, same problem any new solution ?

hamster66 commented 4 years ago

I had to radically change the algorithm of work. Now I am using reboot on WDT in case of freezing when calling these routines. This is definitely a crutch, but it works well :) We are waiting for a more elegant solution from Stefano