knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.78k stars 1.46k forks source link

How to declare a list of topics? #976

Closed dattasaurabh82 closed 1 year ago

dattasaurabh82 commented 1 year ago

Not really an issue but more of a guidance req for type usage.

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.

client.subscribe("subs_topic_1");
client.subscribe("subs_topic_2");
client.subscribe("subs_topic_3");

Is there a way I can declare an array of the subscription topic char arrays? (each subscription topic and payload must be char arrays)

What have I tried?

So I tried something like this (necessary bits)

String MQTT_PUBS_TOPICS[2] = {"client_state", "button_state"};
String MQTT_SUBS_TOPICS[2] = {"pot", "slider"};

boolean connectToBroker(){
  if (client.connected()){
    // Once connected, publish an announcement ...
    client.publish(MQTT_PUBS_TOPICS[0].c_str(), "online");
    // And re-subscribe to necessary topics
    for(int i=0; i<3; i++){
      client.subscribe(MQTT_SUBS_TOPICS[i].c_str());  
    }
 }
  return client.connected();
}

void loop(){
  if (!client.connected()){
    long now = millis();
    if (now - lastReconnectAttempt > 5000){
      lastReconnectAttempt = now;
      if (connectToBroker()){
        lastReconnectAttempt = 0;
      }
    }
  }else{
    client.loop();
  }
}

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:

  1. Is this the right way of declaring an array of topics?
  2. If yes, can you guys confirm that this is working. That way, may be something is wrong in the HW.
  3. If no, what is the procedure for the afore described intent?
knolleary commented 1 year ago

This library does not support subscribing to multiple topics in one call.

flaviopuhl commented 1 year ago

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]);
      }

    } 
  }
}
dattasaurabh82 commented 1 year ago

Amazing! :) That helped!