passing a parameter in server.on("/string", function(parameter)) ?

Hello,

Does anyone know how one can pass a parameter when using the WebServer.h's function "server.on()" so it could do something like this:

server.on("/string", function(parameter))

?

Thank you,

You can't. The callback is of type void (*)(void).
What parameter do you want to pass?

Thank you @LightuC!

Actually, I figured it out in the mean time and, yes you can! :wink: You can even use a for loop at setup to set up a range of values that will trigger either some function or pass a new value into a global.

for (1; i < s; i++) {
// Serial.println(i);
String iString = String(i);
String str1 = "/speedRange";
String str2 = str1 + "=" + iString;
server.on(str2, () {

  • //Serial.print("GETTING NEW SPEED: "); Serial.println(i);*

  • HttpReq = true; // tell mvt function that we have a new value requested from the user for next cmd*

  • // set the global variable*

  • SPEED = i;*

  • // now store this value so next fwd or rvs request will apply it*

  • rqSp = SPEED;*

  • //delay(100);*

  • });*

elfege:
Thank you @LightuC!

Actually, I figured it out in the mean time and, yes you can! :wink: You can even use a for loop at setup to set up a range of values that will trigger either some function or pass a new value into a global.

You are right, capturing local variables within lambda functions is possible. I forgot about that one, because std::function is usually not part of a standard arduino distribution.

Does your code compile? Shouldn't it be

server.on(str2, [=]() {
    //...

Juraj:
it is not lambda. only an url to match

It isn't? I've never heard of a construct like that and

server.on(str2, () {

});

Will fail to compile for me with:

expected primary-expression before ')' token

How is this called then?

elfege:
Actually, I figured it out in the mean time and, yes you can! :wink: You can even use a for loop at setup to set up a range of values that will trigger either some function or pass a new value into a global.

for (1; i < s; i++) {
// Serial.println(i);
String iString = String(i);
String str1 = "/speedRange";
String str2 = str1 + "=" + iString;
server.on(str2, () {

  • //Serial.print("GETTING NEW SPEED: "); Serial.println(i);*

  • HttpReq = true; // tell mvt function that we have a new value requested from the user for next cmd*

  • // set the global variable*

  • SPEED = i;*

  • // now store this value so next fwd or rvs request will apply it*

  • rqSp = SPEED;*

  • //delay(100);*

  • });*
    [/quote]
    better then match many url like /speedRange=3, use /speedRange?speed=3 and server.arg("speed")

```
*  server.on("/speedRange", {
    int  i = atoi(server.arg("speed"));
      //Serial.print("GETTING NEW SPEED: "); Serial.println(i);

HttpReq = true; // tell mvt function that we have a new value requested from the user for next cmd
      // set the global variable
      SPEED = i;
      // now store this value so next fwd or rvs request will apply it
      rqSp = SPEED;
   
      //delay(100);
    });*

```

Juraj:

  server.on("/speedRange", []() {

And here begins the lamda function :wink:

Juraj:
int  i = atoi(server.arg("speed"));

Well that wasn't part of his/her code. The OP used the variable 'i' from outside the lambda function and that is called capturing local variables and has to be done like

[i]() {}
[i&]() {}
[=]() {}
[&]() {}

However, since these are callback functions I'm not sure if this is possible/if this makes sense?

LightuC:
And here begins the lamda function :wink:
Well that wasn't part of his/her code. The OP used the variable 'i' from outside the lambda function and that is called capturing local variables and has to be done like

[i]() {}

i& {}
= {}
& {}

yes, I missed the i int this strange construct. normal is to use url arguments

Juraj:
yes, I missed the i int this strange construct. normal is to use url arguments

But can you do this for callbacks/does it make sense for callbacks?

Juraj:
better then match many url like /speedRange=3, use /speedRange?speed=3 and server.arg("speed")

This is the way to go, I guess.

LightuC:
But can you do this for callbacks?

the server matches the url and calls the callback function. in the callback function you can access the global server object and get the url arguments of the current request with server.arg(argname)