Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By beic
#76553
Cosmic Mac wrote:And you can take advantage of C++ default parameters to simplify the code:
Code: Select allserver.on("/", handleRoot);
serverExt.on("/", handleRootExt);

void handleRootExt() {
   handleRoot(true)
}

void handleRoot(boolean blIsExtAccess = false) {
   String stData = "Hello there";
   if (blIsExtAccess == false) {
      server.send(200, "text/html", stData);
   } else {
      serverExt.send(200, "text/html", stData);
   }
}


Another way would be to redirect Ext requests to the main server:
Code: Select allserverExt.on("/", []() {
   serverExt.sendHeader("Location", String("http://") + toStringIp(serverExt.client().localIP()), true);
   serverExt.send(302, "text/plain", "");
   serverExt.client().stop();
});

server.on("/", []() {
   String stData = "Hello there";
   server.send(200, "text/html", stData);
});


Untested... ;)


Thank you for your approach, I would use your 1st one, but I got error "'handleRoot' was not declared in this scope".

Just a note that my handleRoot function is in another Tab/Sketch.

Any further ideas?
User avatar
By btidey
#76562 Not quite sure what you mean by 'handleRoot function is in another Tab/Sketch', but I assume that means you have the function in a different cpp file to the main sketch.

To make the handleRoot function available in your main sketch you should include a function declaration in your main sketch.

extern void handleRoot(boolean blIsExtAccess = false);

The extern keyword is optional but I like to make it explicit so I know it is defined elsewhere.
User avatar
By beic
#76566
btidey wrote:Not quite sure what you mean by 'handleRoot function is in another Tab/Sketch', but I assume that means you have the function in a different cpp file to the main sketch.

To make the handleRoot function available in your main sketch you should include a function declaration in your main sketch.

extern void handleRoot(boolean blIsExtAccess = false);

The extern keyword is optional but I like to make it explicit so I know it is defined elsewhere.


Yes, the "handleRoot" function is in a different Sketch.

But, I got filtered out the issue with that, but I don't know what is causing that.

If the function declaration is like this:

Code: Select allvoid handleRoot(boolean blIsExtAccess = false)
{
[


I got an error mentioned before "'handleRoot' was not declared in this scope".

But, if I remove the function argument initial/optional value "= false"

Code: Select allvoid handleRoot(boolean blIsExtAccess)
{
[


In this case it will work, but need to define both, true or false when calling it.

Any suggestions?
User avatar
By Cosmic Mac
#76567 Try to put the default value in the function declaration, not in the definition.

Declaration in foobar.h file:
Code: Select allvoid handleRoot(boolean blIsExtAccess = false);

Definition in foobar.cpp file:
Code: Select all#include "foobar.h"

void handleRoot(boolean blIsExtAccess) {
    // your code
}