I am working with the Adafruit MQTT library, and I have the sample programs working fine.
In the MQTT_2subs_esp8266 example sketch two subscriptions are set up as follows:
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider");
In my application, I won't be hardcoding the subscriptions. I need to create subscriptions based on run-time user data. So I think that I need to create an array of subscriptions, and populate them as needed maintaining an index of which array element is which.
To incrementally explore moving from hard coded variables (such as slider and onoffbutton above), I tried to modify the code as follows:
Adafruit_MQTT_Subscribe MQSubs[2];
MQSubs[0] = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
MQSubs[1] = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider");
Unfortunately this results in a compilation error (on the first of the 3 lines above) of:
error: no matching function for call to 'Adafruit_MQTT_Subscribe::Adafruit_MQTT_Subscribe()'
I am confused by what the Adafruit_MQTT_Subscribe object/ type/class is, and how to create an array of them.
In the public section of the class Adafruit_MQTT in Adafruit_MQTT.h I find:
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout = 0);
Adafruit_MQTT_Subscribe *handleSubscriptionPacket(uint16_t len);
also:
class Adafruit_MQTT_Subscribe; // forward decl
and
class Adafruit_MQTT_Subscribe {
public:
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname,
uint8_t q = 0);
...
I believe this latter definition is what I am dealing with, a Class with a constructor that expects a pointer an Adafruit_MQTT object, a pointer to a char array and an integer. I think I am trying to create an array of uninitialized Class objects. Perhaps there needs to be a second constructer that does not demand any parameters...?
I suspect that my lack of understanding of how to use the correct syntax to create an array of whatever these things are is the problem. I can't work out what these 'objects' are as the variables are declared as it they are a 'type' yet their initial values are assigned using a function/constructor using the identical name.
What approach should I take?
I am happy to initially create them with null values and empty string parameters and reset them at runtime when the topics to be subscribed to are known.
I also understand that subscriptions need to be declared before connecting to the MQTT server, and I plan on disconnecting and reconnecting if the subscriptions need to change.
I am trying to run on ESP8266.