How to avoid using large Strings vor the ESP8266 Server ?

Hey Falks,

I am running the ESP8266 as a local server. I have placed a large String to show my homepage. And this is the String for the index-file:

String index_html = R"rawliteral(
  <!DOCTYPE HTML>
  <html>
   <head>
    <meta name=viewport content="width=device-width, initial-scale=1">
    <title>My Smart Home</title>
   </head>
   <body>
    <b><u>SmartHome Controlcenter</b></u><br><br>

    This is not the full code, but believe me, it is at least 5 times like you see here :D

   </body>
  </html>
 )rawliteral";

Are the other way to put out the html-code ? Something like print or else ? (I don't know)

[EDIT] Forget to mention the setup() method which outputs the index_html string if requested:

String outBuffer = index_html;
server.send(200,"text/html",outBuffer);

If it is the use of Strings that is worrying you, the HTML code can be in an array of chars instead. Doing it that way makes it easy to include the value of variables using sprintf() to build the buffer before sending it

If it is not the use of Strings that you are worried about then what problem are you trying to solve ?

Incidentally, why copy index_html to a second String before sending it ?

1 Like

Don't know what this sprintf() is, but I wall try it out. wait, will look now. Okay, I have googled it. As I understand: it makes an Serial Output. I thought, Serial output are only for the RX TX Pins. To be clear: I am talking about a server which makes an output to a chrome browser after a server request.

Because I am replacing certain letters with Strings before an server request.

It is the long String, yes. I am worrying about the RAM and perfomance of my ESP8266. But I have not note here: The String does not getting bigger if I change some letters in the String itself. It is getting rather smaller. Am I worrying for no reason?

Complete resources like a full page, your CSS , javascript, pictures etc can be stored on the filesystem - no need to put them in program memory.

Your use case is a good match for the use of sprintf(). The function allows you to combine text and the value of variables in an array of chars that you subsequently send to the server. If you use Strings for the same purpose then every time that you replace part of a String then a new String is created. Whilst the memory used by the old String is released this can cause memory fragmentation as the gaps left in memory may not be large enough for later, longer Strings to be fitted in. Using an array of chars of a fixed size eliminates this problem.

Take a look at this function that is taken from something that I am experimenting with

void handleRoot()
{
    char buffer[2000];  //using RAW text
    sprintf(buffer, R"===(<!DOCTYPE html>
<html>
<head>

<meta http-equiv="refresh" content="10">

<style>
.colourButton 
{
    width: 960px;
    height: 200px;
    font-size: 50px;
    color: WHITE;
    font-weight:bold;
}
</style>
</head>

    <form> <input type="submit" class="colourButton" style="background-color:BLUE; height: 100px;" value="%s"> </form>
    <form> <input type="submit" class="colourButton" style="background-color:BLUE; height: 100px;" value="Current state : %s"> </form>

    <form action="/ledsOff" method="POST"> <input type="submit" class="colourButton" style="background-color:RED;" value="Turn LEDs off"> </form>
    <form action="/ledsOn" method="POST"> <input type="submit" class="colourButton" style="background-color:RED;" value="Turn LEDs on"> </form>
    <form action="/flashFastTrigger" method="POST"> <input type="submit" class="colourButton" style="background-color:RED;" value="Flash LEDs fast"> </form>
    <form action="/flashSlowTrigger" method="POST"> <input type="submit" class="colourButton" style="background-color:RED;" value="Flash LEDs slow"> </form>
    <form action="/randomTrigger" method="POST"> <input type="submit" class="colourButton" style="background-color:RED;" value="Random LED periods"> </form> 

  </html>)===",
            systemName, ledStateText);

    server.send(200, "text/html", buffer);
}

Note the use of sprintf() to insert the value of systemName and ledStateText into the buffer

Please accept my apologies for the crude nature of the HTML code !

I use Strings for HTML pages. I use the .reserve() String function to reserve the maximum size that I expect for the page. This will minimize the reallocation and memory fragmentation problems as I build up the page.

you helped me out many times sir, so thank you. Will try it out.

Okay, I will google an exapmle. But: My Large String will never get larger. It will rather go smaller (for example: if I replace certain words, than the replaced words will allway be smaller than the original word). So, would this be okay too ?

I don't know what the String class does when you edit a string and it shrinks or stays the same. I build the String in pieces so I am always adding to the end. I do the .reserve() to cover how big the String will get so that all the space is allocated at once.

Whether or not use of the String class causes problems I think that using C style strings is a neater way to to handle text althiugh it can seem to be more complicated at first glance, but everyone to his/her own

Declaring the array at its maximum size and using snprintf() to build the string ensures that it never gets any bigger and that the programmer is in control of the memory used

1 Like

@UKHeliBob hey man :smiley: thanks.

I guess, I have a problem with variabels (Strings Chars etc.). I am a prof in PHP, but C++ and Arduino, are on another level. Do you know a good YoutubeVideo, where they explain it very well ?

By "very well", I mean the background of variables. How the are stored, and how the effect the RAM etc.

Of course, an integer is a variable for numbers, but how are they being stored ? And how they have an effect on the perfomance of the IC ? Also Strings and Chars: How are these being stored ? And are they doing with the IC's perfomance and its Ram ?

I think, these are very very important questions. If unserstanding these questions, than coding would be much easier for us beginner :smiley:

You could start by looking at the Data Type section of the Arduino Reference

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