0

LASTVALUE METHOD FOR Adafruit_MQTT LIBRARY
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

LASTVALUE METHOD FOR Adafruit_MQTT LIBRARY

by sayedurrchowdhury on Sun Jun 03, 2018 4:30 pm

Like you have described (viewtopic.php?f=56&t=130325) a "lastValue" method in Adafruit IO library (Arduino), do we have an equivalent in Adafruit_mqtt library (ESP8266)?

I tried using Adafruit_IO with ESP8266, but couldn't get past the compiler errors. Please help.

sayedurrchowdhury
 
Posts: 1
Joined: Sun Jun 03, 2018 3:41 pm

Re: LASTVALUE METHOD FOR Adafruit_MQTT LIBRARY

by brubell on Mon Jun 04, 2018 10:38 am

I tried using Adafruit_IO with ESP8266, but couldn't get past the compiler errors. Please help.


Could you post the compiler errors from Arduino?

brubell
 
Posts: 35
Joined: Fri Jul 17, 2015 10:33 pm

Re: LASTVALUE METHOD FOR Adafruit_MQTT LIBRARY

by abachman on Tue Jun 05, 2018 1:47 pm

Sayed,


lastValue is part of the HTTP API, so it can't be called from the Adafruit_MQTT library, you'd have to use an HTTP library to make the call.

But, since we added that feature, we also added an MQTT-based method of retrieving the last value for a feed (similar to MQTT's retain feature) that may be helpful in your case. I've published a script that demonstrates its use with the Adafruit_MQTT library here: https://gist.github.com/abachman/f6c2c4 ... 10ac580c0d

The key parts are setting up the subscription feed that should receive the last value and the publishing feed that will trigger it:
Code: Select all | TOGGLE FULL SIZE
// Notice MQTT topics for AIO follow the form: <username>/feeds/<feedname>
// and the get topic adds `/get` to the feed topic you want to receive the value on.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
Adafruit_MQTT_Publish onoff_get = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/onoff/get");


And then, actually triggering the resend looks like this:
Code: Select all | TOGGLE FULL SIZE
onoff_get.publish(0);


In the example sketch I included the .publish call in the MQTT_connect function so that it's triggered whenever the device has to reconnect for any reason (power cycling, wifi interference, transient network errors, etc.).


- adam b.

abachman
 
Posts: 212
Joined: Mon Feb 01, 2010 12:48 pm

Re: LASTVALUE METHOD FOR Adafruit_MQTT LIBRARY

by infov0re on Wed Jun 13, 2018 5:59 pm

Bingo! This solves the issue I was having in this thread - viewtopic.php?f=56&t=135762 - the key is to send the null byte only when you know you have a connection to Adafruit IO. Thanks, Adam.

infov0re
 
Posts: 3
Joined: Thu May 17, 2018 6:44 pm

Please be positive and constructive with your questions and comments.