3

I am using 3 Esp32 modules. One being master and other two being nodes. Nodes are connecting to the master's AP. Master is connected with GSM module to execute the web api. Master esp is having a Async Webserver which takes the http requests from the nodes and executes the appropriate web api. This web api returns the result and finally this result is sent back to the node as the response to the http request.

While running this code, Randomly on any request i face wdt reset.

Hence my question is, how should i handle the wdt resets and keep my program running softly.

E (32706) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (32706) task_wdt: - async_tcp (CPU 0/1)
E (32706) task_wdt: Tasks currently running:
E (32706) task_wdt: CPU 0: IDLE0
E (32706) task_wdt: CPU 1: async_tcp
E (32706) task_wdt: Aborting.
abort() was called at PC 0x400e1cc3 on core 0
Backtrace: 0x4008c434:0x3ffbe170 0x4008c665:0x3ffbe190 0x400e1cc3:0x3ffbe1b0 0x40084771:0x3ffbe1d0 0x4016aaf3:0x3ffbc0d0 0x400e307a:0x3ffbc0f0 0x4008a361:0x3ffbc110 0x40088b7d:0x3ffbc130

My Code Snippet is given below,

Master

HttpClient http(gsmClient, "www.mydummyserver.com", 80);
AsyncWebServer server(80);

const char* ssid = "ESP32-Access-Point";
const char* password = "123456789";
IPAddress ip(192,168,5,2);
IPAddress gateway(192,168,5,1);
IPAddress subnet(255,255,255,0);
const char* PARAM_MESSAGE = "message";

void notFound(AsyncWebServerRequest *request) {
    request->send(404, "text/plain", "Not found");
}

void setup() {

  Serial.begin(115200);
  WiFi.mode(WIFI_AP);
  delay(100);
  WiFi.softAPConfig(ip, gateway, subnet); 
  WiFi.softAP(ssid, password);

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
        request->send(200, "text/plain", "Hello, world");
    });

 server.on("/myApi", HTTP_POST, [] (AsyncWebServerRequest *request) {

        String message;
        if (request->hasParam(PARAM_MESSAGE)) {
            message = request->getParam(PARAM_MESSAGE)->value();
        } else {
            message = "No message sent";
        }

        http.setTimeout(20000);
        String postData = "Param1=abcd&Param2=pqrs";
        http.beginRequest();
        http.post("http://www.mydummyserver.com/testapi.php");
        http.sendHeader("Content-Type", "application/x-www-form-urlencoded");
        http.sendHeader("Content-Length", postData.length());
        http.beginBody();
        http.print(postData);
        http.endRequest();
        int statusCode = http.responseStatusCode();
        String response = http.responseBody();

        Serial.print("Status code: ");
        Serial.println(statusCode);
        Serial.print("Response: ");
        Serial.println(response);
        request->send(200, "text/plain", "Response: " + message);

    });

 server.onNotFound(notFound);

    server.begin();

}

void loop() {

}

Node

HTTPClient httpClient;

void setup() {

  Serial.begin(115200);

  WiFi.begin("ESP32-Access-Point", "123456789");

  Serial.print("WiFi ");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(10);

  }

  Serial.println("connected:" + WiFi.SSID());

}

void executeApi() {

  Serial.println("Executing Api...");

  httpClient.setTimeout(20000);
  httpClient.begin("http://192.168.5.2/myApi");
  httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  String contentStr = "Param1=abcd&Param2=pqrs";

  int httpResponseCode = httpClient.POST(contentStr);

  Serial.print("HTTP Response code: ");
  Serial.println(httpResponseCode);

  httpClient.end();

}

String cmd = "";
void loop() {

  if (Serial.available() > 0) {

    cmd = (Serial.readStringUntil('\n'));

    if (cmd.length() > 0) {

      if (cmd == "api") {
        executeApi();

      }

    }

  }

}

For sulutions i have tried following things,

a. I tried to write this into the loop() function.

 while(true){ 
   delay(1); 
 }

b. Tried disabling the wdt

disableCore0WDT()
disableCore1WDT()

but none of these giving good results.

java bee
  • 81
  • 2
  • 12

2 Answers2

1

The following recommendations, as you aren't providing enough relevant info:

  • Install ESP-exception decoder and analyse your backtrace - Post it here so we can see in detail what caused the wdt
  • AsyncWebServer tends to cause this behavior if a response takes too long; a well placed yield() sometimes helps to prevent this behavior - to analyse you need ESP-exeption decoder
  • As loop() is running on core1 some/no process (idle) on core0 can cause the behavior. You can look up "task assignment to cores" on how to manually distribute the tasks (or send a yield() timed to core0 to reset the wdt)
  • Placing time-outs in combination with sleep/deep sleep (although you don't seem to use them) are also causes for wdt resets
  • A hackey trick is to set CONFIG_ASYNC_TCP_USE_WDT 0, but it is OK for testing

If you look into async_tcp for the possible culprit you see one line xTaskCreateUniversal(_async_service_task, "async_tcp", 8192 * 2, NULL, 3, &_async_service_task_handle, CONFIG_ASYNC_TCP_RUNNING_CORE);

Here is an example of how I use it to prevent wdt resets. Place in setup():

disableCore0WDT();
disableCore1WDT();
disableLoopWDT(); // You forgot this one !

and because of this routine in the async_tcp lib:

static void _async_service_task(void *pvParameters) {
  lwip_event_packet_t * packet = NULL;
  for (;;) {
    if (_get_async_event(&packet)) {
      #if CONFIG_ASYNC_TCP_USE_WDT
      if (esp_task_wdt_add(NULL) != ESP_OK) {
        log_e("Failed to add async task to WDT");
      }           
      #endif
      _handle_async_event(packet);
      #if CONFIG_ASYNC_TCP_USE_WDT
      if (esp_task_wdt_delete(NULL) != ESP_OK) {
        log_e("Failed to remove loop task from WDT");
      }
      #endif
    }
  }
  vTaskDelete(NULL);
  _async_service_task_handle = NULL;
}

I set the CONFIG_ASYNC_TCP_USE_WDT 0 and it works so far.

ocrdu
  • 1,795
  • 3
  • 12
  • 24
Codebreaker007
  • 1,331
  • 1
  • 7
  • 14
0

Short answer

increase the size of the _async_queue in AsyncTCP.cpp

_async_queue = xQueueCreate(32, sizeof(lwip_event_packet_t *));

from 32 to 64 or even higher if you can spare the space

Detailed (but possibly incorrect) explanation:

After looking at the OS calls, I found that in AsyncTCP.cpp

The event queue is created as:

_async_queue = xQueueCreate(32, sizeof(lwip_event_packet_t *));

Later the items are added with a Timeout of portMAX_DELAY, for example:

static inline bool _send_async_event(lwip_event_packet_t ** e){
    return _async_queue && xQueueSend(_async_queue, e, portMAX_DELAY) == pdPASS;
}

Since (at least when using the AsyncWebserver), both the queueing and the dequeuing are done on the same task (async_tcp), as soon as there are 32 events in the queue the moment the next event is to be added, the task will block indefinitely, causing the TWDT to be triggered.

So the solution might be to have the event queueing/dequeueing done in different tasks (maybe intended, since it already is an os queue) or increase the queue size to a value that will not be reached. Since the queue items are not that large, I just increased it to 256 to be on the safe side.

I am not sure if this explanation is correct, since I can't find the code where async_tcp feeds or kicks the watchdog during normal operation. So if someone knows how it works and could tell me, it would be greatly appreciated.