-1

I am trying to convert this Arduino sketch into a C++ class. The header file, "WifiManager.h" is defined as

class WifiManager
{
  public:
    // TODO: Use a singleton pattern https://stackoverflow.com/a/1008289
    WifiManager();
    void run();
    bool isConnected();
    char *getIpAddress();
    virtual ~WifiManager();

  private:

};

and is implemented in a WifiManager.cpp, whose full code implementation is accessible here. In nutshell, the .cppfile implements the .h's API but also handles the WIFI callback functions. As for testing, I am assuming that the callback functions will be called automatically if I run the program as follows:

#include <Arduino.h>
#include "WifiManager.h"
WifiManager *wifi;
void setup()
{
    Serial.begin(115200);
    wifi = new WifiManager();
}

void loop()
{
    delay(1000);
}

This, however, does not work. It seems that all WIFI related callbacks do not run at all. However, I do not know how to fix this as I a C++ beginner. Thus, I would like to know what is the best way to fix this.

Liam deBoeuf
  • 131
  • 3
  • 9
  • 1
    Also asked at: http://forum.arduino.cc/index.php?topic=553734 If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. – per1234 Jun 18 '18 at 08:47
  • All relevant info needs to be inside the post and not behind a link. – gre_gor Jun 18 '18 at 14:16

1 Answers1

1

I think you need to call WiFi.begin. You called it in onWhenWifiOnDisconnects(), but even if a disconnect event is fired, you registered the event handler in the constructor only after the call to WiFi.disconnect(true).

So to clear this up, what your code does right now:

  1. In the constructor you call WiFi.disconnect(true)

  2. Disconnect event is fired, but nobody handles it

  3. You register your event handler and then nothing happens

You should call WiFi.begin in the constructor or in a separate member function that you explicitly call and not (only) in onWhenWifiOnDisconnects().

krisz
  • 2,686
  • 2
  • 11
  • 18
  • Thanks for the reply. In my code, I already call the `WiFi.begin(APP_WIFI_STA_SSID, APP_WIFI_STA_PASS)` when the wifi is disconnected. Also, would you please explain more what you mean by registering the event handler in the constructor? Do you mean adding `WiFi.onEvent(WiFiEvent);` in the constructor? I just tried, and it seems not to work either. – Liam deBoeuf Jun 18 '18 at 08:48
  • I meant to describe facts that you already did. I will rephrase into past tense. – krisz Jun 18 '18 at 08:54
  • Thanks very much! I will try your suggestions as soon as I get back to my board. – Liam deBoeuf Jun 18 '18 at 10:58