-->
Page 1 of 2

Use HTTP server, from within a lib...

PostPosted: Mon Oct 05, 2015 12:42 pm
by andrew melvin
I'm trying to write a lib that will need to have a function that handles all onnotfound requests made to the web server.

When the class is initialised the user passes in a pointer to the HTTP server they are running.
Code: Select all     
HueBridge(ESP8266WebServer * HTTP, uint8_t lights, uint8_t groups);

 


I am then trying to use it in the following way..
Code: Select all       _HTTP->onNotFound(HandleHue);


where HandleWebRequest is a function inside my lib.

I've tried all sorts but, ultimately failed. can someone point me in the right direction!

This is the error:

Code: Select all
HueBridge.cpp: In constructor 'HueBridge::HueBridge(ESP8266WebServer*, uint8_t, uint8_t)':
HueBridge.cpp:13: error: no matching function for call to 'ESP8266WebServer::onNotFound(<unresolved overloaded function type>)'
    _HTTP->onNotFound(HandleHue);
                               ^
HueBridge.cpp:13:31: note: candidate is:
In file included from HueBridge.h:19:0,
                 from HueBridge.cpp:2:
/Users/amelvin/git/Arduino/build/Arduino-latest.app/Contents/Java/portable/packages/esp8266/hardware/esp8266/1.6.5-1160-gef26c5f/libraries/ESP8266WebServer/src/ESP8266WebServer.h:69:8: note: void ESP8266WebServer::onNotFound(ESP8266WebServer::THandlerFunction)
   void onNotFound(THandlerFunction fn);  //called when handler is not assigned
        ^
/Users/amelvin/git/Arduino/build/Arduino-latest.app/Contents/Java/portable/packages/esp8266/hardware/esp8266/1.6.5-1160-gef26c5f/libraries/ESP8266WebServer/src/ESP8266WebServer.h:69:8: note:   no known conversion for argument 1 from '<unresolved overloaded function type>' to 'ESP8266WebServer::THandlerFunction {aka std::function<void()>}'
no matching function for call to 'ESP8266WebServer::onNotFound(<unresolved overloaded function type>)'


Re: Use HTTP server, from within a lib...

PostPosted: Tue Oct 06, 2015 3:39 am
by igrr
If HandleHue is a non-static member function, it has to be bound to an object pointer (this).

Code: Select all_HTTP->onNotFound(std::bind(&HueBridge::HandleHue, this));

Re: Use HTTP server, from within a lib...

PostPosted: Tue Oct 06, 2015 12:42 pm
by andrew melvin
IGRR... you are officially a legend!!!

This solved this issue, plus another one i was having with callbacks to my own libs...

I had tried various methods of casting to (this)... as i knew what i wanted, just no idea how to do it...

Many many thanks

Re: Use HTTP server, from within a lib...

PostPosted: Tue Oct 06, 2015 5:07 pm
by igrr
You're welcome!
Since you are writing a library which uses ESP8266WebServer, you might be interested in RequestHandler API i added today. It's a bit more powerful than normal WebServer callbacks.
Here's a sample: https://gist.github.com/igrr/0da0c4adc7588d9bd911