Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serveStatic() triggers the watchdog and restarts ESP32. #544

Closed
GeorgeFlorian opened this issue Jul 7, 2019 · 11 comments
Closed

serveStatic() triggers the watchdog and restarts ESP32. #544

GeorgeFlorian opened this issue Jul 7, 2019 · 11 comments
Labels

Comments

@GeorgeFlorian
Copy link

GeorgeFlorian commented Jul 7, 2019

Hello !

I am trying to clean up my code and as such I am trying to replace all the GET handlers with

server.serveStatic("/", SPIFFS, "/").setDefaultFile("indexAP.html").setTemplateProcessor(processor);

Buuut, I'm having no luck. The only page that gets served is the indexAP.html and not even this works properly. Only a small part of the CSS works, the image/png doesn't gets served. The ESP32 also restarts after a few seconds on this page with the following errors:

E (8187) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (8187) task_wdt:  - async_tcp (CPU 0/1)
E (8187) task_wdt: Tasks currently running:
E (8187) task_wdt: CPU 0: IDLE0
E (8187) task_wdt: CPU 1: async_tcp
E (8187) task_wdt: Aborting.
abort() was called at PC 0x400e2d0b on core 0

Here is the full code:

Rewrites:

  server.rewrite("/events.html","/logs");
  server.rewrite("/AP_configPage.html","/IP-Config");
  server.rewrite("/dhcpIP.html","/dhcpIP");
  server.rewrite("/staticIP.html","/staticIP");

serveStatic:

server.serveStatic("/", SPIFFS, "/").setDefaultFile("indexAP.html").setTemplateProcessor(processor);

POST handler:

  server.on("/", HTTP_POST,[](AsyncWebServerRequest *request) {
    if(request->hasArg("register")){
      int params = request->params();
      String values_user[2];
      for(int i=0;i<params;i++){
        AsyncWebParameter* p = request->getParam(i);
        if(p->isPost()){
            logOutput((String)"POST[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");            
            values_user[i] = p->value();            
          } else {
              logOutput((String)"GET[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");
            }
      } // for(int i=0;i<params;i++)
      
      if(values_user[0] != NULL && values_user[0].length() > 4 &&
        values_user[1] != NULL && values_user[1].length() > 7) {
            File userWrite = SPIFFS.open("/user.txt", "w");
            if(!userWrite) logOutput((String)"ERROR_INSIDE_POST ! Couldn't open file to write USER credentials !");
            userWrite.println(values_user[0]);  // Username
            userWrite.println(values_user[1]);  // Password
            userWrite.close();
            logOutput("Username and password saved !");          
            request->redirect("/IP-Config");
            // digitalWrite(LED, LOW);
      } else request->redirect("/register");  
    } else if (request->hasArg("skip")) {
      request->redirect("/IP-Config");
    } else if(request->hasArg("import")) {
      request->redirect("/files");
    } else if(request->hasArg("dhcp")) {
      request->redirect("/dhcpIP");
    } else if(request->hasArg("static")) {
      request->redirect("/staticIP");
    } else if(request->hasArg("saveDHCP")) {
        int params = request->params();
        String values_dhcp[2];
        for(int i=0;i<params;i++){
          AsyncWebParameter* p = request->getParam(i);
          if(p->isPost()){
            logOutput((String)"POST[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");            
            values_dhcp[i] = p->value();            
          }
        } //for
        if(values_dhcp[0] != NULL && values_dhcp[0].length() != 0 &&
        values_dhcp[1] != NULL && values_dhcp[1].length() != 0) {
        File inputsWrite = SPIFFS.open("/network.txt", "w");
        if(!inputsWrite) logOutput((String)"ERROR_INSIDE_POST ! Couldn't open file to write DHCP IP credentials !");
          inputsWrite.println(values_dhcp[0]);  // SSID
          inputsWrite.println(values_dhcp[1]);  // Password
          inputsWrite.close();
          logOutput("Configuration saved !");
          request->redirect("/logs");
          shouldReboot = true;
        } else request->redirect("/dhcpIP");
    } else if(request->hasArg("saveStatic")) {
        int params = request->params();
        String values_static[6];
        for(int i=0;i<params;i++){
          AsyncWebParameter* p = request->getParam(i);
          if(p->isPost()){
              logOutput((String)"POST[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");
              values_static[i] = p->value();            
            } else {
                logOutput((String)"GET[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");
              }
        } // for(int i=0;i<params;i++)
        
        if(values_static[0] != NULL &&
          values_static[0].length() != 0 &&
          values_static[1] != NULL &&
          values_static[1].length() != 0 &&
          values_static[2] != NULL &&
          values_static[2].length() != 0 &&
          values_static[3] != NULL &&
          values_static[3].length() != 0 &&
          values_static[4] != NULL &&
          values_static[4].length() != 0 &&
          values_static[5] != NULL &&
          values_static[5].length() != 0) {
              File inputsWrite = SPIFFS.open("/network.txt", "w");
              if(!inputsWrite) logOutput((String)"ERROR_INSIDE_POST ! Couldn't open file to write Static IP credentials !"); 
              inputsWrite.println(values_static[0]);   // SSID
              inputsWrite.println(values_static[1]);   // Password
              inputsWrite.println(values_static[2]);   // Local IP
              inputsWrite.println(values_static[3]);   // Gateway
              inputsWrite.println(values_static[4]);   // Subnet
              inputsWrite.println(values_static[5]);   // DNS
              inputsWrite.close();
              logOutput("Configuration saved !");
              request->redirect("/logs");
              // digitalWrite(LED, LOW);
              shouldReboot = true;
        } else request->redirect("/staticIP");
    }
  });

special handler for /files page

  server.on("/files", HTTP_ANY, [](AsyncWebServerRequest *request){
    Serial.print("handle_config, request: ");
    Serial.println(request->methodToString());    

    if (request->hasParam("filename", true)) { // Download
      if (request->hasArg("download")) { // file download
        Serial.println("Download Filename: " + request->arg("filename"));
        AsyncWebServerResponse *response = request->beginResponse(SPIFFS, request->arg("filename"), String(), true);
        response->addHeader("Server", "ESP Async Web Server");
        request->send(response);
        return;
      }
    } else if(request->hasArg("goBack")) {
      request->redirect("register");
    }

    String HTML; // HTML code 
    String filename = request->url() + ".html";
    File pageFile = SPIFFS.open(filename, "r");
    if (pageFile) {
      HTML = readString(pageFile);
      pageFile.close();
      HTML = addDirList(HTML);
      request->send(200, "text/html", HTML);
    }
  });

I've been trying to make it work for the past 3 days. At this point I either get some help or I will let the code like it currently is.

Currently used handlers:

  server.on("/register", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/indexAP.html", "text/html", false, processor);
  });
  
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->redirect("/register");
  });

  server.on("/register", HTTP_POST, [](AsyncWebServerRequest * request){
    if(request->hasArg("register")){
      int params = request->params();
      String values_user[2];
      for(int i=0;i<params;i++){
        AsyncWebParameter* p = request->getParam(i);
        if(p->isPost()){
            logOutput((String)"POST[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");            
            values_user[i] = p->value();            
          } else {
              logOutput((String)"GET[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");
            }
      } // for(int i=0;i<params;i++)
      
      if(values_user[0] != NULL && values_user[0].length() > 4 &&
        values_user[1] != NULL && values_user[1].length() > 7) {
            File userWrite = SPIFFS.open("/user.txt", "w");
            if(!userWrite) logOutput((String)"ERROR_INSIDE_POST ! Couldn't open file to write USER credentials !");
            userWrite.println(values_user[0]);  // Username
            userWrite.println(values_user[1]);  // Password
            userWrite.close();
            logOutput("Username and password saved !");          
            request->redirect("/IP-Config");
            // digitalWrite(LED, LOW);
      } else request->redirect("/register");  
    } else if (request->hasArg("skip")) {
      request->redirect("/IP-Config");
    } else if(request->hasArg("import")) {
      request->redirect("/files");
    }
  }); // server.on("/register", HTTP_POST, [](AsyncWebServerRequest * request)

  // server.rewrite("/files.html", "/files");

  server.on("/files", HTTP_ANY, [](AsyncWebServerRequest *request){
    Serial.print("/files, request: ");
    Serial.println(request->methodToString());    

    if (request->hasParam("filename", true)) { // Download
      if (request->hasArg("download")) { // file download
        Serial.println("Download Filename: " + request->arg("filename"));
        AsyncWebServerResponse *response = request->beginResponse(SPIFFS, request->arg("filename"), String(), true);
        response->addHeader("Server", "ESP Async Web Server");
        request->send(response);
        return;
      }
    } else if(request->hasArg("goBack")) {
      request->redirect("register");
    }

    String HTML; // HTML code 
    String filename = request->url() + ".html";
    File pageFile = SPIFFS.open(filename, "r");
    if (pageFile) {
      HTML = readString(pageFile);
      pageFile.close();
      HTML = addDirList(HTML);
      // request->send(200, "text/html", HTML);
      AsyncWebServerResponse *response = 
      request->beginChunkedResponse("text/html", [&](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
        String chunk = HTML.substring(index, maxLen);
        strcpy((char*)buffer, chunk.c_str());
        return chunk.length();
      }, processor);
      // response->addHeader("Server","ESP Async Web Server");
      request->send(response);
    }
  });

  server.on("/IP-Config", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/AP_configPage.html", "text/html", false, processor);
  });
  server.on("/networks_placeholders.html", HTTP_GET, [](AsyncWebServerRequest *request) {
    networkScan(networks);
    // Serial.println((String)"networkS = " + networks);
    request->send(SPIFFS, "/networks_placeholders.html", "text/html", false, processor);
  });
  server.on("/logs", HTTP_GET, [](AsyncWebServerRequest* request){
    request->send(SPIFFS, "/events.html", "text/html", false, processor);
  });
  server.on("/events_placeholder.html", HTTP_GET, [](AsyncWebServerRequest* request){
    request->send(SPIFFS, "/events_placeholder.html", "text/html", false, processor);
  });	    
  server.on("/dhcpIP", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/dhcpIP.html", "text/html", false, processor);
  });
  server.on("/staticIP", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/staticIP.html", "text/html", false, processor);
  });     
  server.on("/newMaster.css", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/newMaster.css", "text/css");
  });
  server.on("/jquery-1.12.4.min.js", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/jquery-1.12.4.min.js", "text/javascript");
  });
  server.on("/logo.png", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/logo.png", "image/png");
  });

  // server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request) {
  // }, handleUpload);

  server.on("/staticIP", HTTP_POST, [](AsyncWebServerRequest * request){
    int params = request->params();
    String values_static[6];
    for(int i=0;i<params;i++){
      AsyncWebParameter* p = request->getParam(i);
      if(p->isPost()){
          logOutput((String)"POST[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");
          values_static[i] = p->value();            
        } else {
            logOutput((String)"GET[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");
          }
    } // for(int i=0;i<params;i++)
    
    if(values_static[0] != NULL &&
      values_static[0].length() != 0 &&
      values_static[1] != NULL &&
      values_static[1].length() != 0 &&
      values_static[2] != NULL &&
      values_static[2].length() != 0 &&
      values_static[3] != NULL &&
      values_static[3].length() != 0 &&
      values_static[4] != NULL &&
      values_static[4].length() != 0 &&
      values_static[5] != NULL &&
      values_static[5].length() != 0) {
          File inputsWrite = SPIFFS.open("/network.txt", "w");
          if(!inputsWrite) logOutput((String)"ERROR_INSIDE_POST ! Couldn't open file to write Static IP credentials !"); 
          inputsWrite.println(values_static[0]);   // SSID
          inputsWrite.println(values_static[1]);   // Password
          inputsWrite.println(values_static[2]);   // Local IP
          inputsWrite.println(values_static[3]);   // Gateway
          inputsWrite.println(values_static[4]);   // Subnet
          inputsWrite.println(values_static[5]);   // DNS
          inputsWrite.close();
          logOutput("Configuration saved !");
          request->redirect("/logs");
          // digitalWrite(LED, LOW);
          shouldReboot = true;
    } else request->redirect("/staticIP");
    
    }); // server.on("/staticLogin", HTTP_POST, [](AsyncWebServerRequest * request)

  server.on("/dhcpIP", HTTP_POST, [](AsyncWebServerRequest * request){    
    int params = request->params();
    String values_dhcp[2];
    for(int i=0;i<params;i++){
      AsyncWebParameter* p = request->getParam(i);
      if(p->isPost()){
          logOutput((String)"POST[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");            
          values_dhcp[i] = p->value();            
        } else {
            logOutput((String)"GET[" + p->name().c_str() + "]: " + p->value().c_str() + "\n");
          }
    }
    if(values_dhcp[0] != NULL && values_dhcp[0].length() != 0 &&
      values_dhcp[1] != NULL && values_dhcp[1].length() != 0) {
      File inputsWrite = SPIFFS.open("/network.txt", "w");
      if(!inputsWrite) logOutput((String)"ERROR_INSIDE_POST ! Couldn't open file to write DHCP IP credentials !");
      inputsWrite.println(values_dhcp[0]);  // SSID
      inputsWrite.println(values_dhcp[1]);  // Password
      inputsWrite.close();
      logOutput("Configuration saved !");
      request->redirect("/logs");
      shouldReboot = true;
    } else request->redirect("/dhcpIP");
  
  }); // server.on("/dhcpLogin", HTTP_POST, [](AsyncWebServerRequest * request)
@atanisoft
Copy link
Contributor

@GeorgeFlorian I think you have a typo...

server.rewrite("/AP_configPage.html","//IP-Config");

Shouldn't there only be one / in the target?

Also please capture the console output at the VERBOSE log settings so @me-no-dev can help investigate why async_tcp is triggering the WDT.

@GeorgeFlorian
Copy link
Author

GeorgeFlorian commented Jul 7, 2019

@GeorgeFlorian I think you have a typo...

server.rewrite("/AP_configPage.html","//IP-Config");

Shouldn't there only be one / in the target?

Also please capture the console output at the VERBOSE log settings so @me-no-dev can help investigate why async_tcp is triggering the WDT.

Which one is correct:
server.rewrite("/IP-Config","/AP_configPage.html"); or server.rewrite("/AP_configPage.html","/IP-Config"); ?
I am thinking that I want to send from .html to the uri so I'll chose: server.rewrite("/AP_configPage.html","/IP-Config");

The typo is only in this thread. After I've pasted the code here I removed one of the back slash.

That was the VERBOSE log:

E (21547) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (21547) task_wdt:  - async_tcp (CPU 0/1)
E (21547) task_wdt: Tasks currently running:
E (21547) task_wdt: CPU 0: IDLE0
E (21547) task_wdt: CPU 1: async_tcp
E (21547) task_wdt: Aborting.
abort() was called at PC 0x400e2ec3 on core 0

Backtrace: 0x4008c7e0:0x3ffbe160 0x4008ca11:0x3ffbe180 0x400e2ec3:0x3ffbe1a0 0x40081789:0x3ffbe1c0 0x4014f627:0x3ffbc320 0x400e1523:0x3ffbc340 0x4008a67d:0x3ffbc360 0x4008877d:0x3ffbc380

Also, to make the CSS to work I returned to using a handler for it:

  server.on("/newMaster.css", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/newMaster.css", "text/css");
  });

Also, each page from the rewrites triggers the watchdog resulting in the same error.
For example by going to http://IP/files

Also, not one single HTTP_POST works. Not even the simple redirect ones.

@me-no-dev
Copy link
Owner

you have cases where you send two responses/redirects or not at all. Any time you get WDT, means it's your code that is bad :)

@velbn
Copy link

velbn commented Jul 8, 2019

Hi
I have a too similar issue. The issue started when I updated ESPAsyncWebServer
image
is there anyway to backtrace this to corresponding request?

@velbn
Copy link

velbn commented Jul 8, 2019

I think it has something to do with the partition size of the SPIFFS.
When I make the partition size smaller. It goes first of all much faster but also more stable

@vinot
Copy link

vinot commented Jul 15, 2019

Hi,

try add:
#include "esp_wifi.h"
esp_wifi_set_ps (WIFI_PS_NONE);

and test, it solved my problem

@GeorgeFlorian
Copy link
Author

Hi,

try add:
#include "esp_wifi.h"
esp_wifi_set_ps (WIFI_PS_NONE);

and test, it solved my problem

Will try.

@stale
Copy link

stale bot commented Sep 21, 2019

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 21, 2019
@stale
Copy link

stale bot commented Oct 5, 2019

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Oct 5, 2019
@d0ct0rd-dalimo
Copy link

It didn't help me, I have the same problem, but with AsyncWebServer

@GeorgeFlorian
Copy link
Author

It didn't help me, I have the same problem, but with AsyncWebServer

What problems ? I advise you to open a separate issue.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants