It works well, however, I can't select connectivity without re-flashing and I would like to add that capability by assigning a pin and based on its state decide if to connect using WiFi or ETH.
I want to get rid of the WIFI or ETH constants and have both classes compile together.
I know I can use different names for WiFI and ETH classes and add a condition in the dozens locations where I call each of the classes but this looks like the worst solution.
I tried leaving WiFi classes before setup() and moved ETH classes inside setup() where I can detect the pin but it didn't work, when the pin was set for ETH Mode the ETH Client was unable to connect.
My questions:
Is it possible to read hardware state in the variable declaration section?
You could just create your own class that contains 1 instance of a WiFi client and one instance of an Eithernet client and a flag to declare which type is being used. To save space, you could even make them a union
I've never used either of those libraries. But, it looks like both the WiFiClient and EthernetClient classes inherit from the Client class. So, maybe polymorphism would work:
#include <Ethernet.h>
#include <WiFiNINA.h>
Client *commonClient;
void setup() {
if (chooseEthernet) {
EthernetClient *ethClient = new EthernetClient;
// Do any setup required for Ethernet
commonClient = ethClient;
} else {
WiFiClient *wifiClient = new WiFiClient;
// Do any setup required for WiFi
commonClient = wifiClient;
}
}
You'd then use the 'commonClient' pointer to access the client created in setup().
of course.
on my last project at qualcomm we needed to support various RF hardware. there were several bits read at startup to ID the hardware.
later bits in a small serial eeprom were used, configured at the factory
on other projects (DSP speakerphone) optional pull-up/pull-downs on output pins were read before configuring the pins as outputs to determiner book mode.
you won't be able to redefine a variable as a different types. there are such things as virtual functions. The serial interface can support use of either UART or Bluetooth
This is very useful.
I am just having troubles declaring the MQTT client, originally written MqttClient mqttClient(wifiClient);
Can you please suggest the correct syntax in the context of your offer?
Thanks!
A trick I use is to create global pointers to all the resources you would/could need. Then, in setup() you create the subset you will actually need at that time dynamically. This way you don't need to rely on anything before setup(). (And you don't want to rely on that "before setup()" stuff)
In fact, this method ended up working so well that I went the next level and made my own class that was a sketch. I base all my handhelds on this. The user selects what she wants to do and the main program spans the sketch to handle it. Its a really slick system.
Can you use the defines that the build process includes for you? That way the code will be compiled for whichever board it is being uploaded to. No manual switching required.
Ah... So you have the "MKR WiFi 1010" and optionally put a "MKR ETH SHIELD" on it. Do you put the ETH Shield on even if you are using WiFi? If not, you might be able to just check for the ETH Shield and assume WiFi if the shield is not present. This code can detect the Ethernet chip type:
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found.");
}
else if (Ethernet.hardwareStatus() == EthernetW5100) {
Serial.println("W5100 Ethernet controller detected.");
}
else if (Ethernet.hardwareStatus() == EthernetW5200) {
Serial.println("W5200 Ethernet controller detected.");
}
else if (Ethernet.hardwareStatus() == EthernetW5500) {
Serial.println("W5500 Ethernet controller detected.");
}
If it comes back with a value of EthernetNoHardware then you should use WiFi.
And if you always have the shield, you can use "Ethernet.linkStatus()" to tell if the Ethernet cable is connected or not.
If the Ethernet cable is always connected to a network but you want to use WiFi, connect a switch to an input pin and flip the switch to select between the two networks.
Sorry John, I guess it wasn't clear in my previous response. I always have both, MKR + ETH, this is how I package it, always, and later in the field a decision is taken to use it with Ethernet cable or just WiFi.
Right, I have that switch on a pin and I am trying to find a way to either detect pin state before setup() or in setup() declare the classes as gfvalvo suggested above (the difficulty I have with that is the syntax for the MQTT Class).