J-Rios / uTLGBotLib

Universal Telegram Bot library to develop Telegram Bots in C++. You can use it on multiple type of devices, from ESP8266 and ESP32 microcontrollers to windows or linux devices.
GNU General Public License v3.0
21 stars 9 forks source link

E (384059) esp-tls: read error :-76: #3

Closed doryashar closed 4 years ago

doryashar commented 4 years ago

hi, after few minutes of connection i am getting a lot of errors like this one: E (384059) esp-tls: read error :-76:

J-Rios commented 4 years ago

Hi,

I assume you are using v1.0.1 release and not the master branch right?

What device (ESP8266 or ESP32) are you using?

What framework (arduino/esp-idf)?

It happens with the examples codes? Code to replicate this issue will be nice...

Regards.

doryashar commented 4 years ago

Hi, i am using v1.0.4 from master branch (at least that what it says in the httpsmulticlient), with ESP32 (ESP-IDF) under windows. i was changing the ECHO example. also, i am beginning to thing that the problem is coming from the httpsmulticlient.

// Main Function 
void tlgrm_connect(void * parameter )
{
    int retry=0;
    char buffer[50] ;
    char ** msg_words = (char **)malloc(sizeof(char) * 4097);
    // Create Bot object

    TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
    TIMERG0.wdt_feed=1;
    TIMERG0.wdt_wprotect=0;

    // Main loop
    while(1)
    {
        // Wait 1s for next iteration
        vTaskDelay(1000/portTICK_PERIOD_MS);

        // Check and handle any received message
        while(Bot.getUpdates())
        {
            UserKey *user = get_user_by_user_id(Bot.received_msg.from.id);
            int num_args;
            //std::string text = (string)Bot.received_msg.text;
            ESP_LOGI(TAG,"Message received from %s[%s]: %s\n", Bot.received_msg.from.first_name,Bot.received_msg.chat.id,Bot.received_msg.text);
            num_args = SplitBufferToArray(Bot.received_msg.text, " ", msg_words);
            handle_cmd(Bot.received_msg,msg_words,num_args-1,user);
        }

       // Send new messages from array
        while (tlgrm_num_messages != tlgrm_messages_pointer && strcmp(last_msg_id,"") ) {
            if (retry > 20) {
                tlgrm_messages_pointer = tlgrm_messages_pointer + 1 % MAX_TLG_MSG_BFR;
                continue;
            }

            UserKey * user = get_user_by_uid(tlgrm_send_messages[tlgrm_messages_pointer].uid);
            char* send_to;
            if (!user || !strcmp(user->get_user_id(),"")) {
                ESP_LOGW(TAG,"Can't send message to non-existing user.\nNo user with uid %d.",tlgrm_send_messages[tlgrm_messages_pointer].uid);
            }
            else {
                send_to = user->get_user_id();
                ESP_LOGI(TAG,"Sending %s to [%s]\n", tlgrm_send_messages[tlgrm_messages_pointer].msg_data,send_to); 
                if(!Bot.sendMessage(send_to, tlgrm_send_messages[tlgrm_messages_pointer].msg_data)) {
                    retry++;
                    ESP_LOGI(TAG,"Send fail.\n");
                    continue;
                }
            }

            //success!
            retry = 0;
            tlgrm_messages_pointer = tlgrm_messages_pointer + 1 % MAX_TLG_MSG_BFR;
            vTaskDelay(200/portTICK_PERIOD_MS);
        }

        //upgrade if needed (added outside of the loop since it causes multiple OTAs)
        if (upgrade_needed) simple_ota_example_task();
    }
}
J-Rios commented 4 years ago

Note that master branch has a work in progress development and it is not a stable version. Please use a released version like v1.0.1, that one is well tested.

About the error, it comes from mbedtls, and code -76 means that the TCP socket read has fail for an unknown reason. It can be related to the network or even Telegram Server closing the socket by some reason... Maybe reset the socket when this error fires is the way to solve the issue, needs to get a replicable error environment and test this workaround, if it works then the fix will be added to next release version.

Also, can you check/debug if the error comes from uTLGBotLib or it comes from OTA? You can enable uTLGBotLib debug by calling Bot.set_debug(2);.

You can try to disconnect and connect again the Bot when error occurs...