- Wed Jan 17, 2018 12:20 am
#73204
codeJake wrote:Pablo2048 wrote:Best reading about string concatenation in Arduino I've seen is here http://www.gammon.com.au/concat . Chunked method in standard library can be accomplished by this sequence:
Code: Select allwebServer.sendHeader("Cache-Control",` "no-cache, no-store, must-revalidate");
webServer.sendHeader("Pragma", "no-cache");
webServer.sendHeader("Expires", "-1");
webServer.setContentLength(CONTENT_LENGTH_UNKNOWN);
// here begin chunked transfer
webServer.send(200, "text/html", "");
webServer.sendContent(F("<html><head>")); // here can be for example your first big string
// ... here you send line-by-line html formatted line from SPIFFS file
webServer.sendContent(F("</body></html>")); // html closing
webServer.sendContent(F("")); // this tells web client that transfer is done
webServer.client().stop();
But I prefer Async web server - is way faster and supports concurrent connections...
Thankyou, you are awesome! I've been burning my brain trying to figure this out. I plugged in your suggestions and it almost worked. But after reading through the errors in the console I ended up removing the (F) part of the statements and it works perfectly. I thank you once again for your help.
CodeJake!
With Removing the "F()" from the code, you are keeping the Strings as literals which will use up more memory, and flash storage unnecessarily (At least that's how I believe it works).
This is my Favorite method of serving any webpage content from an ESP that's more complex than a few lines:
1. Create a new file in the Arduino IDE named index.h
2. Put this inside it:
Code: Select allconst char MAIN_page[] PROGMEM = R"=====(
/// Your entire webpage here. (Can be multi-line)
)=====";
3. Make sure you save it, and import it in your main sketch file:
4. Now to serve the web-page, simply do this:
Code: Select allhttpServer.send(200, "text/html", MAIN_page);
If you ask me, this is one of the best ways to do it, next to using SPIFFS. The main advantage this has over SPIFFS is that when you update the main firmware, you can also update the webpage contents, all at once

Also this approach stores the page ONLY in program memory (The SPI Flash chip), and not your precious heap. Whenever the page is fetched, it's simply streamed directly from the SPI flash out over the network. This method has worked flawlessly for me, and I have streamed
13+ KB pages through it, all loading extremely fast without a single problem or memory errors.