Outsource functions to separate Task (ESPAsyncWebServer) | ESP32

In my project I use the ESPAsyncWebServer. If an WebServer-event occurs, I need to call functions. These functions are fairly large and also contain some code, which require some time to execute. Due to that I sometimes receive Watchdog timeouts.

To fix this, I would like to outsource the functions, so they no longer run in the same task, as the webserver.

Here is a short code snippet as an example:

void onEvent(AsyncWebSocket       *server,
             AsyncWebSocketClient *client,
             AwsEventType          type,
             void                 *arg,
             uint8_t              *data,
             size_t                len) {

    switch (type) {
        case WS_EVT_CONNECT:
            Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
            break;
        case WS_EVT_DISCONNECT:
            Serial.printf("WebSocket client #%u disconnected\n", client->id());
            break;
        case WS_EVT_DATA:
            handleWebSocketMessage(arg, data, len);
            break;
        case WS_EVT_PONG:
        case WS_EVT_ERROR:
            break;
    }
}
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
    AwsFrameInfo *info = (AwsFrameInfo*)arg;
    if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {
        data[len] = 0;
        if (strcmp((char*)data, "toggle1") == 0) {            
            function1();
        }
        if (strcmp((char*)data, "toggle2") == 0) {            
            function2();
        }
         if (strcmp((char*)data, "toggle3") == 0) {            
            function3();
        }
...
    }
}

The functions: function1(), function2(), function2() should run on a different task and should be executed if they are called by the handleWebSocketMessage-Function.

I don’t want to use flags, as the functions also contain several parameters, which are passed by the “uint8_t *data”-Element (via ArduinoJson).

Basically, all I want to achieve, is that the function “handleWebSocketMessage” don’t has to wait for the execution of the individual functions.

Has anybody an idea how to achieve that?

you probably need to duplicate the payload to make it available to your new task as the Events ends, its likely that the void *arg and uint8_t *data will no longer be maintained

there are on line examples, here is one (random hit)

What I don’t understand yet, is how I would initiate the execution of the function in the main task and how I would make sure they only run once.
Is it possible leave the infinite for-loop out, so that the task only runs once? And start it again in the main task if it is needed?

If the task is not coded to run as a loop such as a

 while(1) or a for(;;)

then the task will exit when it gets to the end of the code.

At the end of the called run-one-time-task use vTaskDelete( NULL );, which will flag the task to the OS to clean up memory used by the calling task.

Use log_i( " high watermark % d", uxTaskGetStackHighWaterMark( NULL ) ); to get stack space used by the task.

I use the xTaskCreatePinnedToCore( task, "task string name", somenumberoframbytes, NULL, some priority level, NULL, 1 ); function to create a task. Just define a task creation freeRTOS macro, upon the code execution of the task creation macro, the newly created task will spawn, run, and without a loop, die.

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