Closed dattasaurabh82 closed 1 year ago
This library does not support subscribing to multiple topics in one call.
I am using this solution and it works:
// Insert here topics that the device will publish to broker
const char *TopicsToPublish[] = {
"DemoBase/data",
"DemoBase/info"
};
// Insert here the topics the device will listen from broker
const char *TopicsToSubscribe[] = {
"DemoBase/reset",
"DemoBase/update",
"DemoBase/builtinled"
};
/*+--------------------------------------------------------------------------------------+
*| Connect to MQTT client |
*+--------------------------------------------------------------------------------------+ */
void MQTTconnect() {
if(!MQTTclient.connected()) { // Check if MQTT client is connected
Serial.println();
Serial.println("MQTT Client : [ not connected ]");
MQTTclient.setServer(BROKER_MQTT, 1883); // MQTT port, unsecure
MQTTclient.setBufferSize(1024);
Serial.println("MQTT Client : [ trying connection ]");
if (MQTTclient.connect(ID)) {
Serial.println("MQTT Client : [ broker connected ]");
for(int i=0; i<=((sizeof(TopicsToPublish) / sizeof(TopicsToPublish[0]))-1); i++){
Serial.print("MQTT Client : [ publishing to ");
Serial.print(TopicsToPublish[i]);
Serial.println(" ]");
}
for(int i=0; i<=((sizeof(TopicsToSubscribe) / sizeof(TopicsToSubscribe[0]))-1); i++){
Serial.print("MQTT Client : [ subscribing to ");
Serial.print(TopicsToSubscribe[i]);
Serial.println(" ]");
MQTTclient.subscribe(TopicsToSubscribe[i]);
}
}
}
}
Amazing! :) That helped!
Dev environment: vs code and platform io HW platform: esp32
Context:
So one might require a solution, where I do not want to subscribe, one topic at a time, directly in the subscription func, hard coded by hand.
Is there a way I can declare an array of the subscription topic
char arrays
? (each subscription topic and payload must bechar arrays
)What have I tried?
So I tried something like this (necessary bits)
Issue:
This way the client connects and publishes the status message but during the for loop of subscription, it keeps connecting and disc-connecting, again and again.
Ask: