@@ -32,14 +32,6 @@ void setup() {
3232
3333 Serial.print (" Connecting to Adafruit IO" );
3434
35- // because Adafruit IO doesn't support the MQTT
36- // retain flag right now, we need to load the
37- // last value for the "counter" feed manually and
38- // send it our handleMessage function
39- if (counter->exists ()) {
40- handleMessage (counter->lastValue ());
41- }
42-
4335 // start MQTT connection to io.adafruit.com
4436 io.connect ();
4537
@@ -52,12 +44,23 @@ void setup() {
5244 // wait for an MQTT connection
5345 // NOTE: when blending the HTTP and MQTT API, always use the mqttStatus
5446 // method to check on MQTT connection status specifically
55-
5647 while (io.mqttStatus () < AIO_CONNECTED) {
5748 Serial.print (" ." );
5849 delay (500 );
5950 }
6051
52+ // Because Adafruit IO doesn't support the MQTT retain flag, we can use the
53+ // get() function to ask IO to resend the last value for this feed to just
54+ // this MQTT client.
55+ //
56+ // Behind the scenes, the library is MQTT publishing an empty message to the
57+ // {username}/f/counter/csv/get topic to trigger a resend on the
58+ // {username}/f/counter/csv topic that this client is already subscribed to.
59+ // That means calling get() will cause the handleMessage function we attached
60+ // to this feed to be called with the most recent data record published to
61+ // the feed.
62+ counter->get ();
63+
6164 // we are connected
6265 Serial.println ();
6366 Serial.println (io.statusText ());
0 commit comments