Passing WiFi object to a class

Hi,
I'm using Arduino for some time now, but always for very small projects, where I did not use any classes or other kind of "better readability" things.
Now that my last project is a bit longer, I would like to split it into classes. Also because of reuse.
Now my question : (It's very fundemental for a real coder, but I'm a noob in Arduino and still learning a lot).
I use an ESP32 as MCU and create a WiFi object.
In my main code :
#include <WiFi.h>
#include <WiFiClientSecure.h>

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

if (!client.connect(192.168.178.1, 80)) ..........

All no problem, runs fine. But now if I want to split this functionality into a class, I will have to pass a reference to the "client" (WiFiClient) object. How can this be done. Of course I need to include arduino.h and wificlientsecure.h in the class and create an object ? then I overwrite the object with the one that I pass from the main.ino ?

Hope It's understandable what I want to do and someone can help me. It would be great to begin writing more readable and reuseable code :slight_smile:

Did a custom server and lib to connect..
Take a peek at the lib, maybe it gives you some ideas..
Lib here..

good luck.. ~q

Thank you very much. I think i got the picture. So i can access a Wifi instance from within the class without passing it from my main program. Just define a new client and its ready to be used, if there is a configured wifi connection available.
I thought, I need to pass this connection somehow (The Wifi connection) , but it seems to be global ? A new instance of the wificlient is required, of course. I will try it later, thank you.

Acutally, you're not creating it. You're just using it. The lib created a global variable for you: arduino-esp32/libraries/WiFi/src/WiFi.h at master · espressif/arduino-esp32 · GitHub

I will argue that passing a reference would be better: a pointer can be NULL, a reference cannot. So, in theory, a reference is a "stronger promise" that you will get what you really want :wink:

As I said before, you can just use the global var. It can be accessed anywhere you #include <WiFi.h>.

BUT, I REALLY don't like the idea of globals. Hence, I'm doing what you want to do -> injecting the global instance into my objects explicitly.

Take a look at this: DnWiFiDoorLock/lib/DnAppEsp/src/DnApp/Esp/Esp8266/WiFiSetupAndLoopAware.h at master · MacDada/DnWiFiDoorLock · GitHub

Thank you all for the help. I first used the global kind of access. Just to make it easy in the beginning. But yes, this is not as I intended it and it would also not teach me anything, if in future I use something that is not global.
So after writing the class with a global access to Wifi and being happy to see it running, I have rewritten anything with a pointer to the wifi I use in main. This also runs without problems and was not so much more complex.
Thank you guys, now I will begin creating classes, so I don't write the same routines over and over again in next programs.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.