Chat freely about anything...

User avatar
By Sirquil
#80371 Blank text files downloaded using the readFile function, were caused by using "WiFiClient client = server.available" function; changing it to "client = server.available" function solved the issue.

Code: Select allvoid listen()   // Listen for client connection
{

     // Check if a client has connected
     [b]client = server.available();[/b]   

    if (client)
    {

      // Process this request until it completes or times out.
      // Note that this is explicitly limited to handling one request at a time!


Observing the behavior of both; I found better performance and reliability resulted from using "WiFiClient client = server.available" function. Function "client = server.avaiable" introduces wdt resets; where as, "WiFiClient client= server.available" function does not cause wdt resets.

Is there a way to use "WiFiClient client = server.available" function or a way to use "client = server.avaiable" just in the "reaFile" function?

Code: Select allvoid readFile()
{

    digitalWrite(online, HIGH);   //turn-on online LED indicator

     String filename = (const char *)&MyBuffer;

     Serial.print("File:  ");
     Serial.println(filename);

     File webFile = SPIFFS.open(filename, "r");

     if (!webFile)
     {
          Serial.println("File failed to open");
          Serial.println("\n");
     }
     else
     {
          char buf[1024];
          int siz = webFile.size();
          //.setContentLength(str.length() + siz);
          //webserver.send(200, "text/plain", str);
          while (siz > 0)
          {
               size_t len = std::min((int)(sizeof(buf) - 1), siz);
               webFile.read((uint8_t *)buf, len);
               client.write((const char*)buf, len);
               //client.write(webFile);
               siz -= len;
          }
          webFile.close();
     }

     error = 0;

     delayTime = 1000;

     MyBuffer[0] = '\0';

//     digitalWrite(online, LOW);   //turn-off online LED indicator

     listen();

}


William