bertmelis / espMqttClient

MQTT 3.1.1 client library for the Espressif devices ESP8266 and ESP32 on the Arduino framework.
https://www.emelis.net/espMqttClient/
MIT License
100 stars 21 forks source link

The espMqttClient library does support subscribing to multiple topics?? #162

Open Mausam678 opened 1 week ago

Mausam678 commented 1 week ago

The espMqttClient library does support subscribing to multiple topics.

When I attempt to subscribe to multiple topics, I encounter disconnection issues from the MQTT broker. However, when I subscribe to a single topic, the code functions correctly. I'm unsure of the root cause behind this issue and how to resolve it.

bertmelis commented 1 week ago

Yes, the library supports subscribing to multiple topics at once. You have to pass pairs of topic c-strings and qos values. The callback for (un)successful subscribing returns an array of qos values.

The library is tested on this point according the MQTT specification: https://github.com/bertmelis/espMqttClient/blob/main/test/test_packets/test_packets.cpp#L420 https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Figure_3.23_- There is no automated test against a broker but I use this myself on the latest (containerized) mosquitto broker.

Do you have access to the brokers logs?

Mausam678 commented 1 week ago

I am encountering a problem with subscribing to multiple topics using my MQTT client. The code provided below demonstrates the issue: when I attempt to subscribe to multiple topics (with different QoS levels) by uncommenting all subscription lines, the MQTT client disconnects. However, when subscribing to a single topic, the client functions as expected.

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);

  // Subscribe to topic with QoS 0
  uint16_t packetIdSub0 = mqttClient.subscribe(SUBSCRIBE, 0);
  Serial.print("Subscription Packet ID for QoS 0: ");
  Serial.println(packetIdSub0);

  // Subscribe to another topic with QoS 1
  // uint16_t packetIdSub1 = mqttClient.subscribe("AT/OT", 1);
  // Serial.print("Subscription Packet ID for QoS 1: ");
  // Serial.println(packetIdSub1);

  // Subscribe to a third topic with QoS 2
  // uint16_t packetIdSub2 = mqttClient.subscribe("AT/OTR", 2);
  // Serial.print("Subscription Packet ID for QoS 2: ");
  // Serial.println(packetIdSub2);

  uint16_t packetIdPub0 = mqttClient.publish(PUBLISH, 0, false, "ALTID01");
  Serial.println("Publishing at QoS 0, packetId: ");
  Serial.println(packetIdPub0);
}
bertmelis commented 1 week ago

Ah, you're not subscribing to multiple topics in one call.

Would you share the brokers log? Would it be possible to build your code with debug logging enabled and share the logs generated by the library?

Mausam678 commented 1 week ago

I haven't set up broker logs, and I don't have knowledge about it. Could you provide me with code that includes broker logging and subscribes to multiple topics?

bertmelis commented 6 days ago

Let's start with getting the debug output from the ESP. This will already show who is disconnecting: the client or the broker.

Arduino IDE tools-->core debug level --> verbose edit logging.h --> add #define DEBUG_ESP_MQTT_CLIENT 1 just under #pragma once debug statements will be visible on the standard Serial port

Platformio add this to platformio.ini:

build_flags =
  -D DEBUG_ESP_MQTT_CLIENT=1
  -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
  -D LOG_LEVEL=LOG_LEVEL_VERBOSE
build_type = debug

If you could share the output? Irrelevant (or sensitive) parts can be left out of course.

Another question: your problem seems to be with subscribing to multiple topics, but the second and third subscription is with a QoS > 0. Are you sure your broker supports this? Could you try to subscribe to a single topic with QoS 1 or 2?