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?
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.
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.