[GH-ISSUE #381] Error calling configModeCallback inside another class #320

Closed
opened 2026-02-28 01:24:43 +03:00 by kerem · 5 comments
Owner

Originally created by @jcmojj on GitHub (Jul 1, 2017).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/381

I made the class Internet to take out of the main code all the code from wifiManager and make it cleaner, but I have a problem with the configModeCallback that have an error that I don't know how to fix.

The internet class is:

Internet::Internet(bool resetWifi){
  Serial.begin(115200);
  wifiAutoConnect(resetWifi);
  pinMode(TRIGGER_PIN, INPUT);
}
void Internet::wifiAutoConnect(bool resetWifi){ 
    WiFiManager wifiManager;
    wifiManager.setBreakAfterConfig(true);
    wifiManager.setAPCallback(configModeCallback);
    if(resetWifi){wifiManager.resetSettings();}//reset saved settings

      wifiManager.setMinimumSignalQuality(20);
      wifiManager.setTimeout(120); 

    if (!wifiManager.autoConnect("MyNet","12345678")) {
      Serial.println("failed to connect and hit timeout");
      ESP.restart();
      delay(1000);
    }
    Serial.println("connected...yeey :)");
}
void Internet::wifiCheckToConnectOnDemand(bool resetWifi){ 
  if ( digitalRead(TRIGGER_PIN) == LOW ) {
      WiFiManager wifiManager;
      if(resetWifi){wifiManager.resetSettings();}//reset saved settings
      wifiManager.setMinimumSignalQuality(20);
      wifiManager.setTimeout(120); 
      wifiManager.setBreakAfterConfig(false);
      if (!wifiManager.startConfigPortal("MyNet","12345678")) {
        Serial.println("failed to connect and hit timeout");
        delay(3000);
        //reset and try again, or maybe put it to deep sleep
        ESP.restart();
        delay(5000);
      }
    Serial.println("connected...yeey :)");
  }
}
void Internet::configModeCallback(WiFiManager *myWiFiManager) {
  Serial.println("Entered config mode(callBack):");
  Serial.println(WiFi.softAPIP());
}

I call from setup: with internet.wifiAutoConnect(resetWifi);

I call from loop with internet.wifiCheckToConnectOnDemand(resetWifi);

I get this error message:

Arduino: 1.8.3 (Mac OS X), Placa:"ESP32 Dev Module, 80MHz, 921600, None"
Internet.ccp.ino: In member function 'void Internet::wifiAutoConnect(bool)':
Internet.ccp:21: error: invalid use of non-static member function
wifiManager.setAPCallback(configModeCallback);

I know that the code expected that the configModeCallback was inside of the main code and not inside another class. If someone can give me a tip to change something in my code or inside the lib, you are welcome. To make it easy the part of the lib that deals with this function is:

void WiFiManager::setAPCallback( void (*func)(WiFiManager* myWiFiManager) ) {
  _apcallback = func;
}
Originally created by @jcmojj on GitHub (Jul 1, 2017). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/381 I made the class Internet to take out of the main code all the code from wifiManager and make it cleaner, but I have a problem with the configModeCallback that have an error that I don't know how to fix. The internet class is: ``` Internet::Internet(bool resetWifi){ Serial.begin(115200); wifiAutoConnect(resetWifi); pinMode(TRIGGER_PIN, INPUT); } void Internet::wifiAutoConnect(bool resetWifi){ WiFiManager wifiManager; wifiManager.setBreakAfterConfig(true); wifiManager.setAPCallback(configModeCallback); if(resetWifi){wifiManager.resetSettings();}//reset saved settings wifiManager.setMinimumSignalQuality(20); wifiManager.setTimeout(120); if (!wifiManager.autoConnect("MyNet","12345678")) { Serial.println("failed to connect and hit timeout"); ESP.restart(); delay(1000); } Serial.println("connected...yeey :)"); } void Internet::wifiCheckToConnectOnDemand(bool resetWifi){ if ( digitalRead(TRIGGER_PIN) == LOW ) { WiFiManager wifiManager; if(resetWifi){wifiManager.resetSettings();}//reset saved settings wifiManager.setMinimumSignalQuality(20); wifiManager.setTimeout(120); wifiManager.setBreakAfterConfig(false); if (!wifiManager.startConfigPortal("MyNet","12345678")) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.restart(); delay(5000); } Serial.println("connected...yeey :)"); } } void Internet::configModeCallback(WiFiManager *myWiFiManager) { Serial.println("Entered config mode(callBack):"); Serial.println(WiFi.softAPIP()); } ``` I call from setup: with `internet.wifiAutoConnect(resetWifi);` I call from loop with `internet.wifiCheckToConnectOnDemand(resetWifi);` I get this error message: > Arduino: 1.8.3 (Mac OS X), Placa:"ESP32 Dev Module, 80MHz, 921600, None" > Internet.ccp.ino: In member function 'void Internet::wifiAutoConnect(bool)': > Internet.ccp:21: error: invalid use of non-static member function > wifiManager.setAPCallback(configModeCallback); I know that the code expected that the configModeCallback was inside of the main code and not inside another class. If someone can give me a tip to change something in my code or inside the lib, you are welcome. To make it easy the part of the lib that deals with this function is: ``` void WiFiManager::setAPCallback( void (*func)(WiFiManager* myWiFiManager) ) { _apcallback = func; } ```
kerem closed this issue 2026-02-28 01:24:43 +03:00
Author
Owner

@tablatronix commented on GitHub (Aug 28, 2017):

You probably need to put wifimanager globally across your class, you are calling it in each function.

<!-- gh-comment-id:325374201 --> @tablatronix commented on GitHub (Aug 28, 2017): You probably need to put wifimanager globally across your class, you are calling it in each function.
Author
Owner

@suculent commented on GitHub (Sep 6, 2017):

I have similar issue.

// .h
class MyClass {
   public:
       WiFiManager *manager;
      
     inline void saveConfigCallback() {
      should_save_config = true;
    }
 }

// .cpp
class MyClass {

    void some_initializer() {
          manager = new WiFiManager();
          manager->setSaveConfigCallback( saveConfigCallback );
    }    
}

And the error. It probably needs just some casting. One issue is that when dropping WiFiManager into a C++ class, you need to change dot notation from examples to "->" and even more:

src/MyClass.cpp:5:4: error: no matching function for call to 'WiFiMana
ger::setSaveConfigCallback(<unresolved overloaded function type>)'
manager->setSaveConfigCallback( saveConfigCallback );
^
src/MyClass.cpp:5:4: note: candidate is:
In file included from src/MyClass.h:8:0,
from src/MyClass.cpp:1:
lib/WiFiManager/WiFiManager.h:101:19: note: void WiFiManager::setSaveConfigCallback(void (*)())
void          setSaveConfigCallback( void (*func)(void) );
^
lib/WiFiManager/WiFiManager.h:101:19: note:   no known conversion for argument 1 from '<unresolved over
loaded function type>' to 'void (*)()'

<!-- gh-comment-id:327484649 --> @suculent commented on GitHub (Sep 6, 2017): I have similar issue. ``` language:cpp // .h class MyClass { public: WiFiManager *manager; inline void saveConfigCallback() { should_save_config = true; } } // .cpp class MyClass { void some_initializer() { manager = new WiFiManager(); manager->setSaveConfigCallback( saveConfigCallback ); } } ``` And the error. It probably needs just some casting. One issue is that when dropping WiFiManager into a C++ class, you need to change dot notation from examples to "->" and even more: ``` src/MyClass.cpp:5:4: error: no matching function for call to 'WiFiMana ger::setSaveConfigCallback(<unresolved overloaded function type>)' manager->setSaveConfigCallback( saveConfigCallback ); ^ src/MyClass.cpp:5:4: note: candidate is: In file included from src/MyClass.h:8:0, from src/MyClass.cpp:1: lib/WiFiManager/WiFiManager.h:101:19: note: void WiFiManager::setSaveConfigCallback(void (*)()) void setSaveConfigCallback( void (*func)(void) ); ^ lib/WiFiManager/WiFiManager.h:101:19: note: no known conversion for argument 1 from '<unresolved over loaded function type>' to 'void (*)()' ```
Author
Owner

@suculent commented on GitHub (Oct 30, 2017):

Thanks to this example: https://github.com/ajfisher/espixel/blob/master/firmware/esp8266_pixel/configurator.cpp

Solved by turning callback (and everything accessed from the callback) to static property. This resolved the <unresolved overloaded function type> error.

And no, I don't have WiFiManager global in order to deallocate it immediately after it gets used->useless.

See:
github.com/suculent/thinx-lib-esp8266-arduinoc@a2a2a6bfda/src/THiNXLib.cpp
github.com/suculent/thinx-lib-esp8266-arduinoc@a2a2a6bfda/src/THiNXLib.h

<!-- gh-comment-id:340508467 --> @suculent commented on GitHub (Oct 30, 2017): Thanks to this example: https://github.com/ajfisher/espixel/blob/master/firmware/esp8266_pixel/configurator.cpp Solved by turning callback (and everything accessed from the callback) to static property. This resolved the `<unresolved overloaded function type>` error. And no, I don't have WiFiManager global in order to deallocate it immediately after it gets used->useless. See: https://github.com/suculent/thinx-lib-esp8266-arduinoc/blob/a2a2a6bfdad94b806ed65909954f256e551e02ca/src/THiNXLib.cpp https://github.com/suculent/thinx-lib-esp8266-arduinoc/blob/a2a2a6bfdad94b806ed65909954f256e551e02ca/src/THiNXLib.h
Author
Owner

@tablatronix commented on GitHub (Oct 30, 2017):

oh ok, maybe post example code for others with the same issue.

<!-- gh-comment-id:340539163 --> @tablatronix commented on GitHub (Oct 30, 2017): oh ok, maybe post example code for others with the same issue.
Author
Owner

@keitetran commented on GitHub (Jul 4, 2018):

add static to var and function

Look https://github.com/ajfisher/espixel/blob/master/firmware/esp8266_pixel/configurator.h#L52

<!-- gh-comment-id:402341416 --> @keitetran commented on GitHub (Jul 4, 2018): add static to var and function Look https://github.com/ajfisher/espixel/blob/master/firmware/esp8266_pixel/configurator.h#L52
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#320
No description provided.