adafruit / Adafruit_MQTT_Library

Arduino library for MQTT support
MIT License
571 stars 292 forks source link

2sub Example only reads one subscription at startup #141

Closed gamerpaddy closed 3 years ago

gamerpaddy commented 5 years ago

I noticed that the example code for 2 subs only reads the last subscribed topic on startup.

Example:

Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/lamp/onoff");
Adafruit_MQTT_Subscribe dimming= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/lamp/dim");

...

  mqtt.subscribe(&dimming);
  mqtt.subscribe(&onoff);

(i suspect the error over here, subscribe overwrites the previous subscribe?) ...

Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &onoff ) {
      Serial.print(F("Got onoff: "));
      Serial.println((char *)onoff.lastread);
    }
    if (subscription == &dimming) {
      Serial.print(F("Got dimming: "));
      Serial.println((char *)dimming.lastread);
    }

  }

my most recent Mosquitto feed looks like this:

...
/lamp/dim 54
/lamp/dim 30
/lamp/dim 8
/lamp/onoff 1
/lamp/onoff 0
/lamp/onoff 1
/lamp/dim 23
/lamp/test 1

The ESP8266 with the "2 subs example code "only prints this on startup:

Connecting to MQTT... Dropped a packet
MQTT Connected!
Got onoff: 1

not the /lamp/dim subscription. It only prints that, when i update it after it booted up or when i reverse the order of "mqtt.subscribe(..."

The expected behaviour is that my ESP8266 prints

Got onoff: 1
Got dimming: 23

Since these are the two last known values for this topics.

brentru commented 3 years ago

@gamerpaddy This issue has been fixed in the latest release. Please try it!