adds new callback and moved webserver to public
This commit is contained in:
tablatronix
2018-10-12 15:57:35 -05:00
parent 3c280b12a8
commit c3fd7d9622
3 changed files with 38 additions and 8 deletions
+22 -5
View File
@@ -453,6 +453,12 @@ void WiFiManager::setupConfigPortal() {
DEBUG_WM(DEBUG_DEV,"dns server started with ip: ",WiFi.softAPIP());
dnsServer->start(DNS_PORT, F("*"), WiFi.softAPIP());
// @todo new callback, webserver started, callback cannot override handlers, but can grab them first
if ( _webservercallback != NULL) {
_webservercallback();
}
/* Setup httpd callbacks, web pages: root, wifi config pages, SO captive portal detectors and not found. */
server->on(String(FPSTR(R_root)).c_str(), std::bind(&WiFiManager::handleRoot, this));
server->on(String(FPSTR(R_wifi)).c_str(), std::bind(&WiFiManager::handleWifi, this, true));
@@ -521,15 +527,15 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
startAP();
WiFiSetCountry();
// init configportal
DEBUG_WM(DEBUG_DEV,F("setupConfigPortal"));
setupConfigPortal();
// do AP callback if set
if ( _apcallback != NULL) {
_apcallback(this);
}
// init configportal
DEBUG_WM(DEBUG_DEV,F("setupConfigPortal"));
setupConfigPortal();
if(!_configPortalIsBlocking){
DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, non blocking/processing"));
return result;
@@ -2012,6 +2018,17 @@ void WiFiManager::setAPCallback( std::function<void(WiFiManager*)> func ) {
_apcallback = func;
}
/**
* setWebServerCallback, set a callback after webserver is reset, and before routes are setup
* if we set webserver handlers before wm, they are used and wm is not by esp webserver
* on events cannot be overrided once set, and are not mutiples
* @access public
* @param {[type]} void (*func)(void)
*/
void WiFiManager::setWebServerCallback( std::function<void()> func ) {
_webservercallback = func;
}
/**
* setSaveConfigCallback, set a save config callback after closing configportal
* @note calls only if wifi is saved or changed, or setBreakAfterConfig(true)
+5 -2
View File
@@ -184,8 +184,10 @@ class WiFiManager
// SET CALLBACKS
//called when AP mode and config portal is started
//called after AP mode and config portal has started
void setAPCallback( std::function<void(WiFiManager*)> func );
//called after webserver has started
void setWebServerCallback( std::function<void()> func );
//called when settings reset have been triggered
void setConfigResetCallback( std::function<void()> func );
//called when wifi settings have been changed and connection was successful ( or setBreakAfterConfig(true) )
@@ -273,7 +275,6 @@ class WiFiManager
// set body class (invert)
void setClass(String str);
private:
std::unique_ptr<DNSServer> dnsServer;
#if defined(ESP32) && defined(WM_WEBSERVERSHIM)
@@ -284,6 +285,7 @@ class WiFiManager
std::unique_ptr<WM_WebServer> server;
private:
std::vector<uint8_t> _menuIds;
std::vector<const char *> _menuIdsDefault = {"wifi","info","exit"};
@@ -484,6 +486,7 @@ class WiFiManager
// callbacks
// @todo use cb list (vector) maybe event ids, allow no return value
std::function<void(WiFiManager*)> _apcallback;
std::function<void()> _webservercallback;
std::function<void()> _savewificallback;
std::function<void()> _presavecallback;
std::function<void()> _saveparamscallback;
@@ -39,7 +39,6 @@ void saveWifiCallback(){
Serial.println("[CALLBACK] saveCallback fired");
}
//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("[CALLBACK] configModeCallback fired");
@@ -53,6 +52,16 @@ void saveParamCallback(){
Serial.println("[CALLBACK] saveParamCallback fired");
}
void bindServerCallback(){
wm.server->on("/custom",handleRoute);
// wm.server->on("/info",handleRoute); // can override wm!
}
void handleRoute(){
Serial.println("[HTTP] handle route");
wm.server->send(200, "text/plain", "hello from user code");
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
@@ -90,6 +99,7 @@ void setup() {
// callbacks
wm.setAPCallback(configModeCallback);
wm.setWebServerCallback(bindServerCallback);
wm.setSaveConfigCallback(saveWifiCallback);
wm.setSaveParamsCallback(saveParamCallback);