Hi. I'm making a device configurable through a web. I usually made this without problem by concatenating strings (using "+" operator) when using server.send(). The static bunch of the website was stored on PROGMEM, while the smallest chunks were intercalated with the variables. I do this because I want the web to show the stored values of the variables before modifying them.
This always worked well.
The problem is that I was usually storing the favicon files on SPIFFS, now I want to include them inline as an URI (my device is updatable via firmware uploading, so I would like to be able to update also the favicon through this method). When I implement this and try to load the webpage on a browser, loading stops before the last big chunk of HTML (the string after the last "+" ). I tried concatenating the strings via multiple use of "server.sendContent", but it was worse, the webpage stopped loading halfway.
It seems to be a problem with the time it takes to send the full website, maybe being interrupted by something else. I share part of the code with you, what is a better approach to resolve this?
server.send(200,"text/html",String((__FlashStringHelper *)FAVICON01) +
String((__FlashStringHelper *)FAVICON02) +
String((__FlashStringHelper *)WEBSITE)+
ssid +
R"=====("><h2>ContraseƱa</h2><input type="password" class="pass" name="pass" placeholder="ContraseƱa" value=")=====" +
pass +
//here I continue to add String variables
//intercalated with little chunks of HTML
R"=====("><input id="apply" type="submit" value="Aplicar"></form><form method='POST' action='/update' enctype='multipart/form-data'><h1>Actualización</h1><p class="info">Versión actual: v)=====" +
version +
String((__FlashStringHelper *)WEBSITE_END));
server.sendHeader("Connection", "close");
yield();
(The chunk that doesn't load is the last string, WEBSITE_END)
Yes chunked content is the way to go.
try it like this
String s;
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
s += "all sorts of stuff";
server.send(200, "text/html", s);
s = "";
s += "more stuff";
server.sendContent(s);
s = "";
s += "even more stuff";
server.sendContent(s);
server.sendContent(""); // this signifies the end of chunked content transmission;
I think the webserver can not concatenate progmem data without loading it into RAM anyway, so it will need to buffer it regardless, so in the end if you page is bigger than the available heap, the last part will simply not be added to the buffere and can not be transmitted.
Ok, I tried sending two chunks, and it works: the whole thing in one part, and WEBSITE_END on a second part.
The problem I have now is that I would like to send the middle part (variables mixed with const) in one chunk, but I am having problems combining them as I was doing in the code shown above.
server.sendContent() doesn't accept char combined with const char via the "+" operator as server.send does.
How could I combine the following inside a variable to send through server.sendContent()
Well, I divided the HTML in 3 chunks: one for the headers (containing the URIs for the favicon and the apple-touch-icon), one for the variables and little pieces of HTML intercalated and one for the rest of the webpage (which also contains the javascript).