Post topics, source code that relate to the Arduino Platform

User avatar
By codeJake
#73173
jankop wrote:My solution goes against all the recommendations, but it works. I just did not use SPIFFS. It's clearer and it works.

http://esp8266.fancon.cz/esp8266-web-barometer-arduino/esp8266-bmp280-barometer-thermometer.html


Very nice looking webpage. I'll take a look at the code and see how you're going about it.
User avatar
By Pablo2048
#73174
codeJake wrote:Pablo, I'm using the standard web server since there will only ever be one person accessing it. The lib name is ESP8266WebServer.h. Didn't know it was a bad idea to use the += combination as there are many examples on the web using this combination.

Is it because it fragments the heap memory? Will concat keep memory in an orderly stack? Yes the chunked method is what I was asking about. I haven't been able to find a working example that's more then a couple of characters long. Like I said the file can be rather large before it is transfered to another machine.

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...
User avatar
By codeJake
#73188
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!
User avatar
By NickLD
#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:
Code: Select all#import "index.h"

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.