How to pass string into a function

I'm trying to pass the msg string into the html but it doesn't display anything.

Erro 'msg' is not captured

what must i be doing wrong?

         String msg = "Liberado";
 
server.on("/serverIndex", HTTP_GET, []() {
      String serverIndex =
    "<h2>Atualizar Firmware</h2>"
    "<h3>"+ String(msg) +"<h3>";  // não imprimi nada
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
});

Why did you start a topic in the Uncategorised category of the forum when its description is

:warning: DO NOT CREATE TOPICS IN THIS CATEGORY :warning:

Your topic has been moved to the Programming category

Not posting code.
Not posting actual error messages.

You're welcome

Try "<h3>" + msg + "</h3>"
[edited from \<h3>]

The problem is that the things on either side of the String are not Strings. They are represented as character arrays and you can't use the + operator to concatenate them. You can either make the whole thing with Strings or build the whole thing with char arrays.

If you start with a String then every time you add to it you get a String object.

Be careful though, adding Strings this way can be bad for memory management.

already tried

Try static String msg = "Liberado";

it worked more when I put it inside a condition it doesn't show anything

exemplo

 if(libera_update == 0){
         static   String msg = "Não Liberado";
        }else{
        static   String msg = "Liberado";
        }

The variable is declared inside the if so it's not accessible outside. If you can't make msg static for some reasons, you will have to capture it in the lambda function (actually, it's a better solution than using static) :

server.on("/serverIndex", HTTP_GET, [ msg ]() {

I don't use Strings, but you can use addition + in a variety of ways.

See

in particular,

// adding a constant string to a string:
 String stringThree =  stringOne +  "abc";

I assume the entire expression

    "<h2>Atualizar Firmware</h2>" "<h3>"+ String(msg) +"<h3>"

Uses adjacent string literals are combined into a single one, which looks odd but got snuck in there when I didn't look.

a7

didn't work either

Best to capture it by reference and avoid making a copy:

server.on("/serverIndex", HTTP_GET, [ &msg ]() {
1 Like

This could take some time.

I'd flunk a C or C++ class but I mostly use global variables (and local variables) in my Arduino projects... Saves me from having to pass-around values, pointers, & references.

Yup.
I agree.

didn't work either

Yup, really quite a long time.

Given that you don't seem willing to put in any effort here, I am not optimistic about the outcome.

2 Likes

Try initializing the Strings:

  String msg1 = "<h2>Atualizar Firmware</h2><h3>";
  String msg2 = "Liberado";
  String msg3 = "</h3>";

  server.on("/serverIndex", HTTP_GET, []() {
    String serverIndex = String(msg1) + String(msg2) + String(msg3);  // não imprimi nada
    server.sendHeader("Connection", "close");
    server.send(200, "text/html", serverIndex);
  }); // server.on()

The problem is not about String, it's about the lambda function using a temporary variable. At the time this function will be called, the local variable is gone since a long time, so it needs to be global (or static), or be "captured" by the lambda function.

1 Like