Arduino WiFi101 Library - Pass WiFi and WiFiClient to child classes

Hi,

I’m slowly getting to work on an IoT project involving a WiFi client and am trying to write my own libraries to make for a cleaner implementation. Full disclosure: I'm a beginner!

I am using Adafruit’s ItsyBitsy M4 and the ATWINC1500 breakout with the WiFi101 library and all is going great when everything is in a single file. However, I want to be able to modularise my code by its purpose into classes which for the most part has been successful. I can then pass pointers (*) of the connected hardware (i.e. NeoPixel and Uart for serial ports) to the classes "set" method. Though, I have been unable to do this with the WiFiClass and WiFiClient classes.

I’ve had a go at passing the WiFi and WiFiClient to my classes as a pointer (see code below), in the same way I have successfully done it with Uart Classe for my serial ports (though I have to use -> and not . for methods). The issue is, whilst I am able to determine (what I believe to be) the current client status, no data is ever returned in my loop(). I have getter and setter methods to get/set the WiFiClient pointer, yet there’s no data when I use them.

It also seems that WiFiClass (the WiFi object) is being automatically created when the sketch starts which makes me uncomfortable as I haven't declared it anywhere.

My questions:

  1. Is there any way to explicitly declare a WiFiClass and initialise a custom WiFi101 connection, as opposed to using the default WiFi class provided by the library? For clarity I like to see where things are initialised and not just accept that they have been.
  2. Is it actually possible for me to pass the WiFiClient class through to my own classes and have all classes use the same single instance back in main.cpp via pointers?

Below is what I have tried so far:

NetworkInterface class
(originally designed to hold the entire instantiated WiFi class, but currently just using WiFiClient):

    // network-interface.cpp:
    
    WiFiClient NetworkInterface::getNetworkClient() {
        return *_networkClient;
    }
    
    byte NetworkInterface::setNetworkInterface(WiFiClient *newNetworkInterface, interfacetype_t mode) {
        if (mode >= 0 && mode <= 3) {
            networkMode = mode;
        } else {
            networkMode = -1; // TODO: Use enum
            return -1;
        }
        _networkClient = newNetworkInterface;
        return mode;
    }
    
    
    // .header file:
    
    #ifndef NETWORKINTERFACE_H
    #define NETWORKINTERFACE_H_
    
    #include "Arduino.h"
    
    typedef enum {
      INTERFACETYPE_WIFI = 0,
      INTERFACETYPE_ETH = 1,
      INTERFACETYPE_BT = 2,
      INTERFACETYPE_UNSUPPORTED = -1
    } interfacetype_t;
    
    class NetworkInterface {
    
        public:
            bool connectToSSID(byte ssid[], byte password[]);
            bool getConnectionStatus();
            bool disconnectFromNetwork();
            interfacetype_t getNetworkMode();    
            WiFiClient getNetworkClient();
            byte setNetworkInterface(WiFiClient *newNetworkInterface, interfacetype_t mode);  // 26.01.2019 - LIMITED TO WiFICLIENT, UNSURE ABOUT WiFiClass
    
        private:
            interfacetype_t networkMode;
            WiFiClient *_networkClient;
    };
    
    #endif

main class:

    // main.cpp:
    
    #include <SPI.h>
    #include <WiFi101.h>
    #include "arduino_secrets.h"
    #include "network-interface.h"
    
    char ssid[] = SECRET_SSID;        // your network SSID (name)
    char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
    int keyIndex = 0;            // your network key Index number (needed only for WEP)
    int status = WL_IDLE_STATUS;
    char server[] = "www.google.com";    // name address for Google (using DNS)
    WiFiClient client;
    
    NetworkInterface networkInterface;
    
    
    void setup() {
        //Initialize serial and wait for port to open:
        Serial.begin(9600);
        while (!Serial);
    
        Serial.println("= = = Simple Client-Server Test = = = ");
    
        WiFi.setPins(A5, A4, A3, A2);
        if (WiFi.status() == WL_NO_SHIELD) {
            Serial.println("WiFi shield not present");
            while (true);
        }
    
        // attempt to connect to WiFi network:
        while (status != WL_CONNECTED) {
            Serial.print("Attempting to connect to SSID: ");
            Serial.println(ssid);
            // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
            status = WiFi.begin(ssid, pass);
    
            // wait for connection 
            delay(2000);
        }
        Serial.println("Connected to wifi");
        printWiFiStatus();
    
        Serial.println("\nStarting connection to server...");
    
        /*
          Pass the network client (and hopefully soon WiFi class with hardware connection data) to NetworkInterface class
          
          TODO: IMPLEMENT ENUM FOR INTERFACE TYPES
        */
        networkInterface.setNetworkInterface(&client, 0); 
        ...
    }
    
    void loop() {
        // if there are incoming bytes available
        // from the server, read them and print them:
        WiFiClient myClient = networkInterface.getNetworkClient();
    
        if (myClient.available()) {
            Serial.print("\n==================================\n");
            Serial.print("| REMOTE :: Server Has Responded: |");
            Serial.print("\n----------------------------------\n");
            while (myClient.available()) {
                char c = myClient.read();
                Serial.write(c);
            }
            Serial.print("\n==================================\n");
        }
    
        // if the server's disconnected, stop the myClient:
        if (!myClient.connected()) {
            Serial.println();
            Serial.println("CLIENT :: Disconnecting");
            myClient.stop();
    
            // do nothing forevermore:
            while (true);
        }
    }
    
    
    void makeTestCallToPusher() {
          char serverURLTest[] = "192.168.2.4";
    
          WiFiClient myClient = networkInterface.getNetworkClient();
    
        if (myClient.connect(serverURLTest, 80)) {
            Serial.print("CLIENT :: Connected - ");Serial.print(serverURLTest);Serial.print("\n");
            myClient.println("GET /test HTTP/1.1");
            myClient.println("Upgrade: WebSocket");
            myClient.println("Connection: Upgrade");
            myClient.println("Origin: ARDUINO_TEST");
            myClient.println("Host: eu");
            myClient.println("Connection: close");
            myClient.println();
        }
    }
    
    
    void printWiFiStatus() {
        // print the SSID of the network you're attached to:
        Serial.print("SSID: ");
        Serial.println(WiFi.SSID());
    
        // print your WiFi shield's IP address:
        IPAddress ip = WiFi.localIP();
        Serial.print("IP Address: ");
        Serial.println(ip);
    
        // print the received signal strength:
        long rssi = WiFi.RSSI();
        Serial.print("signal strength (RSSI):");
        Serial.print(rssi);
        Serial.println(" dBm");
    }