WiFiClass constructor and destructor

Hi,

I am developing a project where I need to switch between connecting to an existing network and opening Arduino's Access Point for incoming connections. That is, I need to switch between WiFiClass:begin(...) and WiFiClass:beginAP(...).

The problem is that some libraries (e.g. WiFiClient for TCP communication) do not work properly on my Arduino GIGA R1 wifi, unless I invoke the following:

WiFi = WiFiClass(WiFiInterface::get_default_instance());

Then things work, at least for a while. However, I am concerned about the safety of the previous command because the class has pointers inside and it lacks of a deconstructor.

Is it safe or shall I adjust anything?

Thank you for your help

Not an answer to your question but wanted to share (in case you can switch processor) that the ESP32 board can act as Wi-Fi Station, Access Point or both.

To set the Wi-Fi mode, use WiFi.mode() and set the desired mode as argument:

code description
WiFi.mode(WIFI_STA); station mode: the ESP32 connects to an access point
WiFi.mode(WIFI_AP); access point mode: stations can connect to the ESP32
WiFi.mode(WIFI_AP_STA;) access point and a station connected to another access point

you reuse a WiFiClient object for STA and AP? do you really need a global WiFiClient object?

Thank you Jackson for your suggestion

Actually, I have two global variables for REST communication in STA mode:

#include <ArduinoHttpClient.h>
WiFiClient _Wfc;
HttpClient _DBClient = HttpClient(_Wfc, DB_SERVER_ADDRESS, DB_PORT);

and one static variable in AP mode:

WiFiServer _ap_server(80);
// ...
void function()
{  
    static WiFiClient ap_client;
    //...
    ap_client = _ap_server.available();
    //...
}

The last error I remember is that HttpClient::post(...) function failed when returning to STA mode after having been in AP mode. But then, if I do

WiFi = WiFiClass(WiFiInterface::get_default_instance());

when switching between AP and STA, everything looks fine. The problem is that I don't really know what I am doing and if it is safe or not.

I think neither of them needs to be global/static

Not really an answer to your questions, just a note - the C++ method to destroy the object usually called "destructor" rather than "deconstructor"

1 Like

I don't know if it is convenient to make the global variables local, maybe I'll try.

However, the static one must be so due to the use I make of the function. Essentially, that function does a rather long job and I have a blinking LED that I want to keep blinking smoothly (plus checking buttons and other issues). But never mind all that: my question then is:

Why should those variables be local rather than global?

Is there any good documentation where I can learn and understand the best practice with those libraries? I couldn't find any...

client = server.available() replaces the content of the client object so there is no point to have it static.

so they are always created for the current network interface

1 Like

Ok, as I imagined.
Thank you very much for your help

Notwithstanding, making WiFiClient variables local did not solve the problem. It is still necessary to clean the WiFi variable to get things work.
Also a local WiFiUDP variable fails if I don't do so.

Then I'll hope WiFi = WiFiClass(WiFiInterface::get_default_instance()) is a safe instruction.