how to access ESP8266WebServer initialised object from main sketch in namespace

Hi ,

I have main sketch in which I have inititalzied

ESP8266WebServer server(80);

and I have a subroutine which I have put in header file control.h as shown below

namespace control
{

void toggleRelay() {
  char temp[400];

  int state = server.arg("state").toInt();
  if ((state == 1) or (state == 0)) {
    int relay = server.arg("relay").toInt();
    if ((relay == 5) or (relay == 4)) {
      digitalWrite (relay, state);
      snprintf ( temp, 400,
                 "<html>\
  <head>\
    <title>NodeMCU DHT11 Sensor and Relay Board</title>\
    <meta http-equiv='refresh' content='0; url=../'>\
    </head>\
  <body>\
  %d .</body>\
  </html>", 0);
      server.send ( 200, "text/html", temp );
    }

  }

}
}

now in the main sketch I have function that calls toggleRelay,

which so far I was calling like this:

server.on("/control", control::toggleRelay);

Either I have to pass server object to toggleRelay which I do not know how or I have to have server object initialised globally so its accessible in other header file. How do I achieve this?

Why do you have executable code in a header file? Definitions belong in a header file, NOT implementations.

Why are you adding a mostly useless namespace?

How do I achieve this?

I suggest you head over to http://snippets-r-us.com for help with your snippets.

PaulS:
Definitions belong in a header file, NOT implementations.

in that case i really want to know how do I put functions in a separate file I can include and call in main sketch rather than having them in a header file ?

I am not a c/c++ programmer. I am learning as I am doing.

PaulS:
Why do you have executable code in a header file? Definitions belong in a header file, NOT implementations.

See code for this here I would appreciate if you can show me the right way to achieve modularity for specific task in this case I am trying to do.

Put the function prototypes in the header file.
Put the function implementations in the source file (usually with a .cpp extension).

The header file can be included anywhere. The corresponding source file will be compiled, separately, and the linker will combine the sketch's object file and the other object files into a single hex file.