I have an Arduino C++ program for ESP8266 that uses ESPAsyncWebServer to serve dynamic HTML pages.
The static parts of HTML, CSS and JavaScript are stored in PROGMEM as string literals.
The dynamic parts are supplied programmatically at runtime using the built in template engine.
For example:
server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", index_html, processor);
});
String processor(const String &var) {
if (var == "PLACE_HOLDER") {
return String("this is the actual value for your place holder");
}
return String();
}
I understand the template engine used by ESPAsyncWebServer moves the content from PROGMEM to a String object in memory and performs String replacement operations. Each web page has several dynamic strings of length 100 to 200 bytes, whose value is known only at runtime. (For example, it scans for available WiFi networks and shows them on a web page. PROGMEM has only a place holder where the full list is inserted dynamically)
I am concerned that this will need frequent reallocation of memory to grow the string. Will this run the risk of heap fragmentation? If so, is there a work around?