Page 1 of 1

Problems with ESP32 Webserver after http requests

Posted: Sun May 19, 2024 1:41 pm
by Schlossi1234
Hey everyone :)

I set up an ESP32 Webserver based on the example "restful server" of the ESP32-IDF. I also created a small webpage that sends a get request every 1 seconds to get some sensordata. Everything is working fine, however after some time (I think after 40 to 50 minutes) I get some error massages and the server or wifi crashes.
I added the debug in the attachments.

It seems like, that the connections or the sockets are not closed in a correct way.

Does anyone have an idea, what this cause
s the troubles?

Thank you for you help.

Best regards
Lukas
Bildschirmfoto vom 2024-05-19 13-22-13.png
Bildschirmfoto vom 2024-05-19 13-22-13.png (257.44 KiB) Viewed 1440 times
Bildschirmfoto vom 2024-05-19 13-22-32.png
Bildschirmfoto vom 2024-05-19 13-22-32.png (273.66 KiB) Viewed 1440 times

That is the setup for httpd_uri_t:

Code: Select all

httpd_uri_t sensorvalue_get = {
        .uri = "/sensorX",
        .method = HTTP_GET,
        .handler = sensorvalue_json_handler, // sensorvalue_handler,
        .user_ctx = NULL};
That is my handler for the get requests

Code: Select all

esp_err_t sensorvalue_json_handler(httpd_req_t *req)
{
    // ESP_LOGI(TAG, "sensorvalue_json_handler");
    httpd_resp_set_type(req, "application/json");
    char json_string[300];
    create_json_from_sensordata(json_string, sensors.sensor1, sensors.sensor2, sensors.sensor3, sensors.timestamp);
    // ESP_LOGI(TAG, "%s", json_string);
    // httpd_resp_send(req, json_string, HTTPD_RESP_USE_STRLEN);

    httpd_resp_sendstr(req, json_string);
    httpd_resp_sendstr(req, NULL);

    return ESP_OK;
}
That is the JS code that sends the get requests

Code: Select all

setInterval(function () {
        var xhttp = new XMLHttpRequest();
        console.log("still running");
        xhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                console.log(this.responseText);
                //nums = this.responseText.split('+');
                obj = JSON.parse(this.responseText);
                document.getElementById('s1').innerHTML = obj.sensor1;
                document.getElementById('s2').innerHTML = obj.sensor2;
                document.getElementById('s3').innerHTML = obj.sensor3;
            }
        };
        console.log("send get");
        xhttp.open("GET", "/sensorX", true);
        xhttp.send();
    }, 1000);

Re: Problems with ESP32 Webserver after http requests

Posted: Tue May 21, 2024 3:12 am
by chegewara
Suggestion: check free memory and/or memory leaks.

Re: Problems with ESP32 Webserver after http requests

Posted: Fri May 24, 2024 10:04 pm
by Schlossi1234
Hi,
thank you for the advice.

Shouldn't be there a reset, when there is a memory lecker?

However, i added a while Look in the Main function that prints test every 5 sec to Check in the Core stops working. I found out that the core is still running after the crash

Re: Problems with ESP32 Webserver after http requests

Posted: Sun May 26, 2024 10:56 am
by Schlossi1234
Hi,

It seems that you were right. I checken the heap and the heap goes down. I figured out that the cjson_print causes the problem (Not Sure why). However i wrote my own json_print and now it runs

Thanks

Re: Problems with ESP32 Webserver after http requests

Posted: Mon May 27, 2024 12:52 am
by ESP_Sprite
Schlossi1234 wrote:
Sun May 26, 2024 10:56 am
It seems that you were right. I checken the heap and the heap goes down. I figured out that the cjson_print causes the problem (Not Sure why).
Likely because cjson_print returns a C string, and those don't destroy themselves when they go out of scope. You probably aren't calling free() on the resulting string after you're done with it.