使用 WiFiClient.client.read() 在 ESP32 上下载文件失败 - “任务看门狗”错误

Wha*_*ion 1 c++ arduino esp32

我正在测试从服务器下载大文件(大约 1mb OTA 二进制文件)的代码

下载中途出现错误:

-> E (15787) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
-> E (15787) task_wdt:  - async_tcp (CPU 0/1)
-> E (15787) task_wdt: Tasks currently running:
-> E (15787) task_wdt: CPU 0: IDLE0
-> E (15787) task_wdt: CPU 1: IDLE1
-> E (15787) task_wdt: Aborting.
-> abort() was called at PC 0x400e16af on core 0
Run Code Online (Sandbox Code Playgroud)

我目前基于ESP32 github 链接的理解是,下载过程正在阻止 ESP 执行必要的后台功能。

while()在运行 aclient.read()以从服务器获取文件的循环期间发生故障(在下面的代码中) 。

我尝试进行测试delay()vTaskDelay()看看它们是否有帮助。不确定这是否会释放一些东西,或者只是进一步增加任何任务阻塞。都没有帮助。(而且我认为它们无论如何都是相同的功能,对吗?)

我也不能 100% 确定阻塞就是问题所在。使用 a 监控下载Serial.println(thisClient.available())显示剩余字节从大约 7k 到大约 5k,然后跳转到备份到 7k,并重复执行此操作。这表明服务器可能存在问题。但是在同一个服务器上,将相同的文件下载到 JS 编码的 ajax 请求就可以了。

下面的测试代码改编自Espressif OTA 示例。我还是 C++ 新手,所以请原谅使用 String 而不是 char 数组。使用这些字符时遇到问题。

#include <WiFi.h>
#include <Update.h>
#include "AsyncJson.h"
#include "ArduinoJson.h"

AsyncWebServer EspServer(80);

void setup(){
    Serial.begin(115200);

    const char* ClientSsid = "***";
    const char* ClientPwd = "***";
    Serial.print("Connecting to LAN ");
    WiFi.begin(ClientSsid, ClientPwd);
    int CreepConnect;
    CreepConnect=0;
    while (WiFi.status()!=WL_CONNECTED && CreepConnect<30){
        delay(250);
        Serial.print(".");
        CreepConnect++;
    }
    Serial.println(" on SSID " + WiFi.SSID() + " at " + String(WiFi.localIP()));

    EspServer.on("*", HTTP_POST, [](AsyncWebServerRequest * Req){
        AsyncWebParameter* keyVal=Req->getParam(0);
        String key=keyVal->name();
        String val=keyVal->value();
        if(key=="req" && val=="execOTA"){
            Serial.println("Updating...");
            Req->send(200, "text/plain", "Updating...");
            //EspServer.end(); //Tested disabling server
            execOTA();
        }else{
            Serial.println("Request ignored");
            Req->send(200, "text/plain", "Request ignored");
        }
    });
    EspServer.begin();
    //execOTA();  This does work.  It only fails under the callback above.
}

String execOTA(){
    WiFiClient thisClient;
    IPAddress thisHost(192, 168, 1, 10);
    char thisPath[]="/testBigFile.ino.esp32.bin"; //Big = about 800k OTA bin file
    String thisPart, theseHeaders, thisBody;
    if(!thisClient.connect(thisHost, 8465)){
        Serial.println("Connection Failed");
        return "{\"Error\":\"Connection Failed\"}";
    }
    Serial.println(thisPath);
    Serial.println(String(thisPath));
    Serial.println("Connection succeeded");
    Serial.print("thisClient.available(): ");
    Serial.println(thisClient.available());
    String thisReq=String("GET ") + String(thisPath) + " HTTP/1.1\r\n" +
        "Host: 192.168.1.10:8465\r\n" +
        "Cache-Control: no-cache\r\n" +
        "Connection: close\r\n\r\n";
    Serial.println("thisReq: " + thisReq);
    thisClient.print(thisReq);
    unsigned long timeout = millis();
    while(thisClient.available()==0) {
        if(millis()-timeout > 5000){
            Serial.println("Client timed out");
            thisClient.stop();
            Serial.println("Client timed out");
            return "{\"Error\":\"Client timed out\"}";

        }
    }
    Serial.println("Headers Begin");
    thisPart="Header";
    while(thisClient.available()){
        Serial.println(thisClient.available());
        if(thisPart=="Header"){
            String thisLine=thisClient.readStringUntil('\n');
            theseHeaders.concat(thisLine);
            thisLine.trim();
            if(!thisLine.length()){
                Serial.println("Headers Complete:\n" + theseHeaders + "\n------\n");
                thisPart="Body";
            }
        }else{ //*** Task Watchdog Error happens in this block, after about 50 successful character reads with delay ***
            char thisChar=thisClient.read();
            thisBody.concat(thisChar);
            //delay(10); //Tested at various durations to see if it adds to or frees up blocking.  It seems to add further blocking?
            //vTaskDelay(15); //Also tested, at various durations.
        }
    }
    Serial.println("Body Complete");
    thisClient.stop();
    return "{\"Headers\":\"" + theseHeaders + "\",\"Body\":\"" + thisBody + "\"}";
}

String getHeaderValue(String header, String headerName){
    return header.substring(strlen(headerName.c_str()));
}

Run Code Online (Sandbox Code Playgroud)

rom*_*key 5

您在 HTTP 回调中做了太多事情。当回调正在运行时,看门狗定时器无法重置。如果这种情况发生太长时间,您将收到您所看到的错误 - Task watchdog got triggered。最重要的线索是它发生在async_tcp任务中。

尝试重写代码,以便HTTP_POST处理程序设置一个全局变量来指示execOTA()需要调用它,而不是调用它本身。然后就得loop()做繁重的工作。

像这样的东西:

boolean exec_ota_flag = false;

void setup() {

...

    EspServer.on("*", HTTP_POST, [](AsyncWebServerRequest * Req){
        AsyncWebParameter* keyVal=Req->getParam(0);
        String key=keyVal->name();
        String val=keyVal->value();
        if(key=="req" && val=="execOTA"){
            exec_ota_flag = true;
            Req->send(200, "text/plain", "Updating...");
        }else{

...

void loop() {
  if(exec_ota_flag) {
      exec_ota_flag = false;
      execOTA();
  }
}
Run Code Online (Sandbox Code Playgroud)

whileexecOTA 中的循环也需要delay()调用。尝试这样的事情:

    while(thisClient.available()==0) {
        delay(1);
        if(millis()-timeout > 5000){
Run Code Online (Sandbox Code Playgroud)

当您调用时,delay()您给其他任务一个运行的机会,这将使看门狗计时器被重置。

  • 是的。在 Arduino 板上 - 至少是早期的板 - 没有底层操作系统。提供Arduino调用的软件是固件。在 ESP8266 和 ESP32 上,Arduino 软件是基于 Espressif - ESP32 中的 FreeRTOS 提供的软件构建的层。该软件还执行其他功能,例如运行网络堆栈。因此,在真正的 Arduino 板上,“delay()”除了等待延迟完成之外没有其他事情可做。在 Espressif 主板上,它让 CPU 执行非抢占式多任务处理,以便其他任务可以运行。 (2认同)
  • 您提到的对“vTaskDelay()”的调用是 FreeRTOS 调用而不是 Arduino 调用的示例。事实上,“delay()”只是调用“vTaskDelay()”。您可以在 https://github.com/espressif/arduino-esp32/blob/c2b3f2d6afc1911db1974a324782730dd642d524/cores/esp32/esp32-hal-misc.c#L144 查看 ESP32 `delay()` 的实现 (2认同)