ESP8266 WebServer -> Replace certain letters with variables

Hi Falks,

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:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>


char * ssid_ap = "testing";
char * password_ap = "11111111";


IPAddress ip(192,168,11,4); // arbitrary IP address (doesn't conflict w/ local network)
IPAddress gateway(192,168,11,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server;


String index_html = R"rawliteral(<html>
  <body>
   <p>aa</p></body></html>)rawliteral";



String PageNotFound = "<center><p style=\"font-size:30px;\">Page not found!</p>";
String UnknownCommand = "<center><p style=\"font-size:30px;\">Unknown command</p>";
String InputReaktion1 = "<center><p style=\"font-size:30px;\">yeaaaaah.</p>";
String InputReaktion2= "<center><p style=\"font-size:30px;\">noooooo.</p>";


void notFound() {
  server.send(200,"text/html",String(PageNotFound));
  }


void handleForm() {
 String CheckField = server.arg("input"); 
 if(CheckField  == "1") {
  server.send(200, "text/html", InputReaktion1);
  }
  if(CheckField  == "2") {
  server.send(200, "text/html", InputReaktion2);
  }
 }



void setup() {
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(ip,gateway,subnet);
  WiFi.softAP(ssid_ap,password_ap);
  Serial.begin(115200);
  
  server.begin();
  server.on("/", HTTP_GET, []{server.send(200,"text/html",String(index_html));});
  server.on("/action_page", handleForm);
  server.onNotFound(notFound);
}




void loop() {

String aa="aa";
String bb="bb";
index_html.replace(aa, bb);
server.handleClient();

}

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
1 Like

Indeed.

If that becomes a problem, maybe consider having separate strings and concatenating them with the variable information...

1 Like

Okay man, I will try it now, thanks :smiley:

@docdoc Damn man :frowning: 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 :smiley: 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.

I'm glad I helped you. Good coding!

1 Like

So why does this work ?

String text = "0123456789";

void setup()
{
    Serial.begin(115200);
    Serial.println(text);
    text.replace("1", "qwerty");
    Serial.println(text);
}

void loop()
{
}

Output :

0123456789
0qwerty23456789
1 Like

Probably the limitation I remembered and reported is for Arduino boards only, and not for ESPs, so in this case you're quite right. :+1:

1 Like

The sketch was compiled and run on a legacy Nano

I suspect that the confusion arises from this page or one like it https://docs.arduino.cc/built-in-examples/strings/StringReplace which contains a nonsense example

1 Like

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...:wink:

1 Like

I am with you on not using Strings, but it seems to be accepted practice on the ESP32 due to the extra memory available

1 Like

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! :sunglasses:

1 Like

Oh man, you really saved my week. Thanks again.

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.

1 Like

Okay my mate! Thanks.

What do you do if you're using some third party code which simply requires input of type String, as in this example?

Perhaps this is what you need?

GitHub - winder/ESPTemplateProcessor: Library for the ESP8266 Arduino to process templates and stream them with the ESP8266WebServer.

This capability is built into the AsyncWebServer...

If that code should run on a UNO, I'd think that third party have a bad programming habit and cease to use it anymore.

Anyway be more specific: why a "third party" code should require a "String" variable?

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