ESP8266WebServer, big webpage do not load completely

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)

Thank you.

When the page is too big it’s time to consider Chunked transfer encoding - Wikipedia

1 Like

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.

1 Like

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()

ssid +
R"=====("><h2>ContraseƱa</h2><input type="password" class="pass" name="pass" placeholder="ContraseƱa" value=")=====" +
pass +
R"=====("><h2>DHCP</h2><label class="switch"><input type="hidden" name="dhcp" id="dhcp" value="yes"><input type="checkbox" id="checkbox" )=====" +
dhcpTest

I suppose this is a basic question, but I'm not used to work with strings and find it a little confusing.

how big is "big" in real numbers? In KiloByte?

What we see from your snippets is "nothing" and should work if the rest of the code is ok.

Please make a full compileable example we can put in our IDE and test.

I just use 'String'

s += ssid;
s += "><h2>ContraseƱa</h2><input type='password' class='pass' name='pass' placeholder='ContraseƱa' value=";
s += pass;
s += "><h2>DHCP</h2><label class='switch'><input type='hidden' name='dhcp' id='dhcp' value='yes'><input type='checkbox' id='checkbox";
s += dhcpTest;

Main thing is that you cannot have " within a String unless you specify it with like " but you can also use ' in html and it is treated the same way.

1 Like

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).

It works flawlessly, thank you very much!

1 Like

You are welcome, i had a similar issue. Lack of memory really.

Good have fun

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.