Post topics, source code that relate to the Arduino Platform

User avatar
By codeJake
#73158 New to the nodemcu and arduino. I'm having trouble with part of my code to send a string that is very long. I believe I'm runnng out of memory to send this web page. The webserver works great until the file size reachs 5.22kb and then it stops working. I know I need to buffer this date to get the full page sent but I haven't found an example that is easily explained. I know I have to break the page down into chunks and send each chunk but how. I am thinking the size of the chunks should fill one tcp packet at a time
Here's the page I'm trying to send.
Code: Select allString page = "<!DOCTYPE html><link rel=\"icon\" type=\"image/ico\" href=\"/favicon\"><html>";
    page += "<head><meta http-equiv=\"content-type\" content=\"text/html\" charset=\"UTF-8\">";
    page += "<title>Fuel Acquisition System</title></head><body text=\"#000000\" vlink=\"#551A8B\" ";
    page += "bgcolor=\"#99ffff\" alink=\"#EE0000\" link=\"#0000EE\"><div align=\"center\">";
    page += "<font color=\"#000099\" size=\"+2\"><font size=\"+3\">Fuel Acquisition</font><br></font>";
    page += "<div align=\"left\"><br></div><div align=\"left\"><p><p align =\"left\" style=\"color: ";
    page += "darkblue\"><font size=\"5\"><u>System Information</u></font></p>";
    page += "<div align=\"left\"><ul><ul><ul><li><div align=\"left\"><div align=\"left\"><pre>";
    page += "<font color=\"#000099\" size=\"5\">Runtime:";
    page += RunTime;
    page += "     Free Space: ";
    page += T_Size;
    page += "         </font></pre></div></div></li></ul></ul></ul></div>";
    page += "<hr color=\"#000099\" size=\"4\" width=\"100%\"><center><table border=\"3\" ";
    page += "cellspacing=\"2\" cellpadding=\"2\" bgcolor= \"#ffff99\" width=\"80%\">";
    page += "<br><caption><font color=\"#000099\" size=\"+1\"> ";
    page += "Fuel Usage History<br></font></caption><tbody>";
    ///==Build dynamic grid table to be filled with historic data(FUELHISTORY)==///
    page += "<tr><th>Date</th><th>Time</th><th>Gallons</th><th>Temperature</th></tr>";

This above would be the top of the page only. The code I have to fill the table in looks like the following
Code: Select allif (spiffsActive)
      {
        if (SPIFFS.exists(FUELHISTORY))
          {
            File fuel = SPIFFS.open(FUELHISTORY, "r");
            if (!fuel)
              {
                Serial.print("Unable To Open '");
              }
            else
              {
                String s;
                Serial.println("Fuel Usage Report Viewed!");
                yield();
                while (fuel.position()<fuel.size())
                  {
                    s=fuel.readStringUntil('\n');
                    s.trim();
                    page += "<tr><td valign=\"Top\">";
                    page += s.substring(0,11);
                    page += "<br></td><td valign=\"Top\">";
                    page += s.substring(11,19);
                    page += "<br></td><td valign=\"Top\">";
                    page += s.substring(20,26);
                    page += "<br></td><td valign=\"Top\">";
                    page += s.substring(27,33);
                    page += "<br></td></tr>";
                   
                    //PageLength = page.length();
                    //Serial.println(PageLength);
                  }
                Serial.println("  ");
                fuel.close();
                page += "</tbody></table><br><div align=\"center\">";
                page += "<form id=\"/\" action=\"/\" method=\"post\">";
                page += "<input type=\"submit\" name = \"\" value=\"Back\"></form><br></div></div>";
                page += "</div></body></html>";


Any help would be greatly appreciated. Oh and the file size given is what is reported by SPIFFS
Thanks in advance.
Last edited by codeJake on Tue Jan 16, 2018 4:06 pm, edited 1 time in total.
User avatar
By Pablo2048
#73164 My suggestions:
1. use F( macros for storing such huge string into PROGMEM, and/or use R" macro and place string into PROGMEM
2. try to use page.reserve() to avoid reallocation
3. NEVER use string concatenation like page += , use page.concat() instead (play nice with your heap/stack :-) )
4. try to use chunked transfer (which webserver do you use? async, standard lib,...)
User avatar
By codeJake
#73172
Pablo2048 wrote:My suggestions:
1. use F( macros for storing such huge string into PROGMEM, and/or use R" macro and place string into PROGMEM
2. try to use page.reserve() to avoid reallocation
3. NEVER use string concatenation like page += , use page.concat() instead (play nice with your heap/stack :-) )
4. try to use chunked transfer (which webserver do you use? async, standard lib,...)



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.