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

Publish to different topics that have sub-trees not working. #1006

Open HorselessName opened 1 year ago

HorselessName commented 1 year ago

Hello. I even opened a question regarding this on StackOverflow: Question on StackOverflow: [How to publish to two topics from the same MQTT Client?](https://stackoverflow.com/questions/75923589/how-to-publish-to-two-topics-from-the-same-mqtt-client)

The problem, basically, is that when we use two publish methods in a row, even if we establish different topic names, the MQTT Client is actually sending the following messages to the first topic mentioned only.

  if (mqtt_client.connected()) { // Verificando se o cliente está conectado antes de publicar
    // Publicar os Topicos no MQTT Broker
    mqtt_client.publish(mqtt_topic_arduino_mac, arduino_mac.c_str());
    mqtt_client.loop(); // Wait until message is sent

    mqtt_client.publish(mqtt_topic_arduino_ip, arduino_ip.c_str());
    mqtt_client.loop(); // Wait until message is sent
  }

The if statement above, instead of sending a message to topic 1 with message X, and a message to topic 2 with message Y, it is actually sending message to topic 1 with message X and message to topic 1 with message Y.

Can you help?