So the situation is that the Adafruit_MQTT_Client object needs a bunch of constructor parameters
Adafruit_MQTT_Client mqtt(&client, mqtt_serv, mqtt_port, mqtt_name, mqtt_pass);
And these are read from a json file /config.json . So you can't simply define mqtt globally. However, the loop() function needs it, so it can't be defined locally in the setup() function.
You have a more serious problen than this: because your mqtt varible is defined locally in setup(), it dissapears once setup() is complete. The memory will be overwritten by subsequent activity, completely scrambling it. If mqtt and client are to have a lifetime beyond the execution of setup(), they need to be static. You can do this by adding the 'static' modifier to the variable declaration, or by declaring them globally, outside the function.
Ordinarily, use of dynamic memory is considered harmful (Goto - Wikipedia) , but here it's warranted. Using dynamic memory is a problem mostly when things are allocated and freed repeatedly, causing the hep to become fragmented. But here, you are only doing it once.
So, add global variables
WiFiClient client;
Adafruit_MQTT_Client *mqtt;
and use the global mqtt rather than redefining it in your function:
mqtt = new Adafruit_MQTT_Client(&client, mqtt_serv, mqtt_port, mqtt_name, mqtt_pass);
This will mean that all instances of mqtt.something need to be changed to mqtt->something , and all instances of &mqtt change to mqtt (without the ampersand). We no longer need to pass a pointer to mqtt, because mqtt is a pointer.
You will need to do the same thing to OpenClose (incidentally, initial caps are for class names, not variables).
----SNIP! ----
WiFiClient client;
Adafruit_MQTT_Client *mqtt;
Adafruit_MQTT_Subscribe *openClose;
----SNIP! ----
void setup() {
----SNIP! ----
// load json, etc
mqtt = new Adafruit_MQTT_Client(&client, mqtt_serv, mqtt_port, mqtt_name, mqtt_pass);
//Set up the feed you're subscribing to
openClose = new Adafruit_MQTT_Subscribe(mqtt, mqtt_name "/f/OpenClose");
----SNIP! ----
//Subscribe to the onoff feed
mqtt.subscribe(&openClose);
----SNIP! ----
}
void loop() {
----SNIP! ----
if (!mqtt->ping())
{
mqtt->disconnect();
}
----SNIP! ----
}
And you will also need to change those dots to arrows in MQTT_connect().
Having said all that, I'm surprised that there isn't a null constructor and an init method. Unless, of course, there is. Something like:
----SNIP! ----
WiFiClient client;
Adafruit_MQTT_Client mqtt;
Adafruit_MQTT_Subscribe openClose;
----SNIP! ----
void setup() {
----SNIP! ----
// load json, etc
mqtt.init(&client, mqtt_serv, mqtt_port, mqtt_name, mqtt_pass);
//Set up the feed you're subscribing to
openClose.subscribe(&mqtt, mqtt_name "/f/OpenClose");
Read the docs, or at least read the .h file, to find out.