I really really need your help. I am running a webserver. My problem ist, that the index_html String is static. How can I replace certain letters with variables (integer) ?
For example: replace the letters 'aa' in the index_html, with a Integer.
Here is my code:
This is the same as asking "how can I change a string inside another string?". You really don't know how to do it?
If you have a String variable containing the page template, you can replace any occurrence of a specific string with the String replace() function inside another variable you use to send the result/reply.
...
String index_html = R"rawliteral(<html>
<body>
<p>aa</p></body></html>)rawliteral";
...
int value = 25;
...
int value = 25;
String outBuffer = index_html;
outBuffer.replace("aa", String(value));
// Now send the "outBuffer" content as response
Please note I suggest you not to use short sequences and/or potentially common ones. Better use something more specific, like a word enclosed between a special mark, for example "$VALUE$". The second thing you need to keep in mind is you can't replace a string with a longer string (e.g. replace "aa" with "1320"), so your marker should always be longer than the longest value you need to replace it with.
See this:
...
String index_html = R"rawliteral(<html>
<body>
<p>$VALUE$</p></body></html>)rawliteral";
...
int value = 25;
...
int value = 25;
String outBuffer = index_html;
outBuffer.replace("$VALUE$", String(value));
// Now send the "outBuffer" content as response
@docdoc Damn man you absolutely have saved my day... damn. If my project will work finaly, I will send you some bucks man... you really really saved my day I mean, really really really saved my day. Damn. Can't believe it. I've been working on this issue since last Thursday. Damn. You saved my day man.
Yeah, that page looks to be rubbish, it even shows a syntax different from what I remember for the .replace() function...
And you demostrated me I surely remember something that's quite old and obsolete, but it's because if possible I never use Strings, even on ESPs...
Yeah, but even if it has extra memory, if there isn't any garbage collector under the hood I don't feel comfortable. And it's like hard drives: you can have a very large one, but if you keep using it someday it'll fill up!
What I can say is, that the ESP-12E (I am using this for my Code above) has an external flash memory soldered on the board. I know, that you guys avoid using Strings because of the memory garbage. Is there maybe a way to clean up the garbage on the flash memory, in order to keep it clean ? Maybe, randonly once a day, a cleaning command ?
Generally speaking, despite the name it's not really a matter of "garbage", but "memory fragmentation". Read "garbage collector" as a process for some kind of "memory optimizer and defragmentation". High level languages and operating systems always have background garbage collecting processes, so you don't have to deal with it, and you can even ignore it.
Arduino UNO for example, has a way less memory available, so using String variables easily results in the end of the available UNO memory.
But getting back to Arduino world, as this depends on the number of variables, the content length, and the number of changes made to their content, with larger memories the problem becomes way less dangerous even without any kind of garbage collector (or memory allocation management and optimization).
In the end, if you have a large available memory and most of the Strings are constant (like web pages), you can forget about this potential problem.