PubSub help- is there any way to make the constructor dynamic?

When working on a project, I often have a breadboard copy of my hardware to experiment with.
However, I sometimes forget to manually change the constructor when I create the WiFiClient and PubSubClient objects.

WiFiClient GardenClient2;                // The constructor MUST be unique on the network.
PubSubClient client(GardenClient2);

Is there any way to make the constructor dynamic, or at least different for each copy of the sketch on a new board so that I don't have to remember to chanfge the code?

Is there any way to make the constructor dynamic, or at least different for each copy of the sketch on a new board so that I don't have to remember to chanfge the code?

I suspect there is, but without the code it is a little hard to tell. You see if the WiFiClient GardenClient2; is global and if PubSubClient client(GardenClient2);needs to be global as well, you could still have multple object of the same type and reference the object of you choosing when you want to use it.

how do you determine the board you're using?

As written, your question makes no sense. The name of the object you're instantiating ('GardenClient2' in this case) no longer exists after the compiler / linker have done their jobs. It's just collection of memory locations in the processor. So, no matter what you call them, each new board will have a "different copy".

If that's not what you meant, try phrasing your question better.

gfvalvo:
As written, your question makes no sense. The name of the object you're instantiating ('GardenClient2' in this case) no longer exists after the compiler / linker have done their jobs. It's just collection of memory locations in the processor. So, no matter what you call them, each new board will have a "different copy".

If that's not what you meant, try phrasing your question better.

No, you have it correct, and that's what I thought- that the compiled code would be different on different boards.

If I use the same constructor as shown, on two different boards, the MQTT broker will only handle messages from one board.

I asked Nick O'Leary this a year ago, and his response (if I recall accurately) was that the client ID had to be unique on the network.

Further research into the broker, From: MqttClient

"A client identifier clientId must be specified and be less that 65535 characters. It must be unique across all clients connecting to the same server. The clientId is used by the server to store data related to the client, hence it is important that the clientId remain the same when connecting to a server if durable subscriptions or reliable messaging are required."

So, yes, the constructor in this line is lost after compile

WiFiClient GardenClient2;

But the documentation for pubsub says that the client ID has to be the same as the constructor:

PubSubClient client(GardenClient2);

But, unique on the network.

So, I was wondering if anyone had found a workaround for this.

Looks like the Client ID is assigned when you call connect(), not with the constructor. From PubSubClient.h:

   boolean connect(const char* id);
   boolean connect(const char* id, const char* user, const char* pass);
   boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
   boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);