Using delay with WebServer handleClient

Emygma
Posts: 4
Joined: Thu Apr 18, 2019 12:30 pm

Using delay with WebServer handleClient

Postby Emygma » Sun Jun 30, 2019 5:32 pm

Hello,

I am using this library to run a simple webserver, with the ESP32 as AP
https://github.com/espressif/arduino-es ... Server.ino

Everything works fine, if there's only server.handleClient(); in the loop and nothing else.

But the problem is that i need to run another function in the loop that takes a about 150ms to complete.
So something like this:

Code: Select all

loop()
{
    server.handleClient();
   test();
}

void test()
{
   digitalWrite(13, HIGH);
   delay(5);
   digitalWrite(13, LOW);
   delay(150);
   digitalWrite(13, HIGH);
}

And that causes a problem where when I POST an html form, i get an error page in my browser sometimes with a closed connection message.
Basically, sometimes the page loads and sometimes it does not.

So how can I make the server.handleClient() work properly with that delay?

pedros89
Posts: 4
Joined: Tue Aug 03, 2021 9:28 am

Re: Using delay with WebServer handleClient

Postby pedros89 » Fri Oct 15, 2021 12:49 pm

Hello Emygma,
I have the same problem. I sometimes have in response from the server a blank white page and I think this is the reason.
I am sending to the client a very long HTML page (51KBytes) and I in addition I steam images from SPIFFS with the code below.

Code: Select all

if (SPIFFS.exists(path)){
      File dataFile = SPIFFS.open(path, "r");
      if (!dataFile) {
          handleNotFound();
      }
      if (server.streamFile(dataFile, dataType) != dataFile.size()) {   //this is the actual command that is doing the file streaming 
        //Serial.println("Sent less data than expected!");
        handleNotFound();
      }else{
          Serial.println("Page served!");
      }
      dataFile.close();
  }else{
      handleNotFound();
  }
At the same time in the loop I am listening for incoming bytes on the Serial2 from another microcontroller.
May I ask if you have been able to sort out the problem?

weldsstick
Posts: 1
Joined: Tue Nov 02, 2021 3:21 am

Re: Using delay with WebServer handleClient

Postby weldsstick » Tue Nov 02, 2021 4:25 am

It seems to me that the test() function needs to be revised. The culprit is use of delay(). That function will rob processor cycles from your main loop. I would recommend a non blocking delay method. Refer to https://fvdm.com/come/blocking-vs-non-b ... d-particle for one method, or do a Google search for 'non blocking timer arduino' for other ideas.

martinius96
Posts: 33
Joined: Thu Dec 13, 2018 1:39 am

Re: Using delay with WebServer handleClient

Postby martinius96 » Wed Nov 03, 2021 10:49 am

You can use millis() for timing actions instead of delay that will block your whole code.
Or you can use FreeRTOS and 2 tasks that will run separately.

Who is online

Users browsing this forum: No registered users and 57 guests