Page 1 of 1

esp32-s2 for multisocket transfer

Posted: Thu Apr 29, 2021 12:56 pm
by andrej32
I moved a working project from ESP 32 to ESP32-S2. I use the stable environment ESP-IDF-v4.2.

The project uses data transfer through 4 sockets at the same time (it determines which socket is free and data is placed there). Everything works fine on ESP32, but it doesn't work on ESP32-S2. After several blocks of data (1408 bytes each) have been sent, function send(...) returns -1 (socket write error).

When using a single socket, data is transmitted normally.

Please help me understand how to fix this?

Code of the data sending function:

Code: Select all

uint8_t PutToSocket(uint8_t * data, uint16_t len_data, uint16_t *len_data_sent)
{
    int len = 0;
    fd_set fd_write_set;
    struct timeval tv;
    int retval;
    uint8_t return_result = SOCKET_WRITE_IN_PROGRESS;
    uint8_t retry_counter = 0;
    uint8_t i = 0;

    retry_counter = 0;
    return_result = SOCKET_WRITE_IN_PROGRESS;

    if (!len_data)
    {
        return return_result;
    }

    while ( (retry_counter < 3) && (return_result != TRANSFER_OK) )
    {
        *len_data_sent = 0;

        retry_counter++;

        FD_ZERO (&fd_write_set);

        for(i = 0; i < num_sockets; i++)
        {
            FD_SET (sock[i], &fd_write_set);
        }

        tv.tv_sec = 10;
        tv.tv_usec = 0;

        retval = select (FD_SETSIZE, NULL, &fd_write_set, NULL, &tv); // Block until input arrives on one or more active sockets.

        if(retval == -1)
        {
            printf("Socket error at write data\n");
            return_result = ERROR_SOCKET_WRITE_SELECT;
        }
        else if(retval == 0)
            {
                printf("Socket timeout %d sec occurred at write\n", (uint8_t)tv.tv_sec);
                return_result = ERROR_SOCKET_WRITE_SELECT_TIMEOUT_OCCURRED;
            }
            else if (retval > 0)
                {
                    for(i = 0; i < num_sockets; i++)
                    {
                        if(FD_ISSET(sock[i], &fd_write_set))
                        {
                            len = send(sock[i], data, len_data, 0);
                            break;
                        }
                    }

                    if ((len) < 0)
                    {
                        printf("Error write to socket %d\n", len);
                        return_result = ERROR_SOCKET_WRITE_SEND;
                    }
                    else if (len == 0)
                        {
                            printf("Write ZERO to socket\n");
                            return_result = ERROR_SOCKET_WRITE_SEND_ZERO;
                        }
                        else if (len > 0)
                        {
                            if (len == len_data)
                            {
                                *len_data_sent = len;
                                return_result = TRANSFER_OK;
                            }
                            else
                            {
                                return_result = SOCKET_WRITE_IN_PROGRESS;
                            }
                        }
                }
    }

    return return_result;

}

Re: esp32-s2 for multisocket transfer

Posted: Fri Apr 30, 2021 1:54 am
by Sprite
FWIW, I can see no relevant error in your code. (Perhaps one that is not, though: your first argument to select is likely wrong. According to my manpage: 'nfds should be set to the highest-numbered file descriptor in any of the three sets, plus 1.')

Re: esp32-s2 for multisocket transfer

Posted: Fri Apr 30, 2021 6:55 am
by andrej32
I'm not a big expert in lwIP, but I think the code is correct, since I spent time debugging it on ESP32 and it works very well there. If one code for transmitting data over TCP over WiFi works on ESP32 and does not work on ESP32-S2, I tend to think that the problem is still in ESP32-S2 or in the implementation of lwIP in ESP-IDF 4.2 for ESP32-S2. I would like to find an answer to this question, and find a solution to the problem : data transfer over TCP to one server via several sockets.

Re: esp32-s2 for multisocket transfer

Posted: Tue May 18, 2021 9:38 am
by andrej32
continue to experiment with simultaneous transmission over multiple sockets.
At the moment, the results are as follows:

Errors only if sending goes through several sockets (the more sockets the faster the error occurs, with two used sockets, the error occurs, but rarely, with 4 almost immediately after sending 8-10 packets of 1410 bytes) and does not depend on the number of open sockets (only on the number of used ones, if we open 4 but send only one, there will be no errors)

Three types of error:

2.1. WiFi just falls off (wifi:bcn_timout,ap_probe_send_start)

2.2. Selec() returns on timeout.

2.3. When Select() returns the response that the socket is ready (returns > 0) to write, then Send() returns error 11 (which
means that an unblocked socket cannot accept all the data to send without blocking - and this seemed normal, but after
this error occurs, it usually never gets released, with rare exceptions, and this is not the normal behavior of the socket).

There is an assumption that ESP32-S2 does not work correctly with several sockets at the same time, or there is not enough memory, or performance, but when working with several sockets, the wifi falls off, then inside the TCP/IP stack, it is mixed and damaged.