Ethernet library working only once

I have esp32 wroom 32 chip and w5500 ethernet module. I want to open socket for TCP transmission, to receive 5 bytes long configuration data. The problem is my code is working only 1 time.

Here is the server task which I am currently calling in endless arduino loop function, nothing else is called only this task:

Serv_Task([](serv_config_t *config) {
                if (config->address != 0) {
                    Serial.println(config->power.ch1);
                    Serial.println(config->power.ch2);
                    Serial.println(config->power.ch3);
                    Serial.println(config->power.ch4);
                } else {
                    //Icom_Send(config->address, config->power);
                }
            });

And here is the function definition:

void Serv_Task(serv_configReceived_t callback) {

    // check for new client connecting
    EthernetClient newClient = server.accept();
    if (newClient) {
        if (!client) {
            newClient.println("Connected");
            client = newClient;
        } else {
            newClient.println("Another client already connected");
            newClient.stop();
        }
    }

#ifdef WORKING_CODE
    while (client && client.available() > 0) {
        // read incoming data from the client
        Serial.write(client.read());
    }
#else
    // check for incoming data
    if (client && client.available() >= sizeof(serv_config_t)) {
        serv_config_t config = {0};
        for (uint8_t i = 0; i < sizeof(serv_config_t); i++) {
            ((uint8_t *)&config)[i] = client.read();
        }
        // call callback with received data
        callback(&config);
    }
#endif

    // stop client if disconnect
    if (client && !client.connected()) {
        // read out client buffer?
        client.stop();
    }
}

When I define macro WORKING_CODE everything is working as it should, I can connect, disconnect as many times as I want. When I send data they appear in serial console.
But the other part when WORKING_CODE is not defined is not working. Basically I just want to collect data from ethernet buffer as 5 bytes chunks. After connection is made I can send data only 1 time (I use putty).
This is putty console:

Connected
0123456789
0123456789
0123456789

And this is the serial output from esp:

49
50
51
52
54
55
56
57

No matter what I send it will not show in console. Only data from the first line will appear. And when I disconnect and connect again I see my "Another client already connected" message.
Can you help? What is going on?

what should ((uint8_t *)&config)[i] = client.read(); do?

It is just making the code more scalable. In case when I decide to change serv_config_t I don't have to edit Serv_Task function because it copies received bytes into this structure one by one. Currently my serv_config_t looks like this:

typedef struct {
    uint8_t address;
    struct {
        uint8_t ch1;
        uint8_t ch2;
        uint8_t ch3;
        uint8_t ch4;
    } power;
} serv_config_t;

So sizeof returns 5, so this coping loop is running 5 times.

you send 10 characters with ascii code 48 to 57. the output prints the ascii codes skipping the codes which were assigned to address field in the struct.

I know, that is correct, but I have sent 3x 0123456789 but received it only for 1. time.
I found out that client.available() is not updating as data comes. You have to read client and then client.available() function updates it's result. I don't know if this is feature or bug.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.