OTA progress on asyncwebserver

Hello,
I'm using ESPAsyncWebServer and I've managed to make OTA webpage working like this:

    WebServer.on("/update", HTTP_GET, [](AsyncWebServerRequest *request)
                 { ProcessUpdate(request); });

    WebServer.on(
        "/update", HTTP_POST, [](AsyncWebServerRequest *request) {},
        [](AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
        { ProcessUpdate(request, filename, index, data, len, final); });

Where processupdate is:

void ProcessUpdate(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
{
    if (!index)
    {
        update_status = "Update started!";
        Serial.println("Update started");
        update_content_len = request->contentLength();

        Update.runAsync(true);
        if (!Update.begin(update_content_len, U_FLASH))
        {
            Update.printError(Serial);
        }
    }

    if (Update.write(data, len) != len)
    {
        Update.printError(Serial);
    }
    else
    {
        update_status = "Progress: " + String((Update.progress() * 100) / Update.size()) + "%";
        Serial.println(update_status);
    }

    if (final)
    {
        AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", "Please wait while the device reboots...");
        response->addHeader("Refresh", "5");
        response->addHeader("Location", "/");
        request->send(response);
        if (!Update.end(true))
        {
            Update.printError(Serial);
        }
        else
        {
            update_status = "Done!";
            Serial.println("Update complete");
            Serial.flush();
            eDM_Config.Flags.RebootESP = 1;
        }
    }
}

How can I return the progress to webpage?
Thanks,

JS

When the MCU is being updated OTA is the MCU running the program you wrote at the same time the code is being updated? Or does the running program get suspended during the time of OTA?

When the firmware is being updated this line/update_status variable is being updated, and I also can see the update on serial port being done:

My issue is how to get it on webpage as I get on serial in real time...
I've done the javascrpit/XMLHttpRequest approach, but as I'm doing it 4 times a seccond, and it takes 2-3 seconds to update, I got several steps, 0-16%-38%-72%-94%, and I'm not getting the done, and stays on 94%, and it's already done and rebooting...
Since I do the request in setInterval(getstatus, 250)...
Thanks!

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