๐ŸŽ„โ›„โ„๏ธ๐ŸŽ It's beginning to look a lot like ADABOX-mas! The next ADABOX from Adafruit ships soon! Get yours now, or give as a gift! ๐ŸŽ„โ›„โ„๏ธ๐ŸŽ
0

Adafruit IO MQTT Last Will
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Adafruit IO MQTT Last Will

by jmontgomry on Tue Oct 15, 2019 7:19 pm

I must be missing something simple. I am trying to use the Last Will feature to update a feed (online) to OFF when a hard disconnect occurs.

I want the online feed to update to OFF when there is a hard disconnect of the arduino.

I have updated the MQTT header file to:
Code: Select all | TOGGLE FULL SIZE
#define MQTT_CONN_KEEPALIVE 30


so that the last will will update withing 30 seconds.

The definitions are:
Code: Select all | TOGGLE FULL SIZE
const char ONLINE_FEEDPUB[] PROGMEM = AIO_USERNAME "/feeds/online/csv";
Adafruit_MQTT_Publish  OnlineFeedpub = Adafruit_MQTT_Publish(&mqtt, ONLINE_FEEDPUB, MQTT_QOS_1);


in the MQTT connect function, I added the mqtt.will right before the connection attempt:
Code: Select all | TOGGLE FULL SIZE
mqtt.will(ONLINE_FEEDPUB, "OFF,38.1123,-91.2325,0", MQTT_QOS_1, 1);
ret = mqtt.connect();  // connect will return 0 for connected
//
// standard code here from example libraries
//


in the Main loop, every 10 second, i publish ON to the online feed
Code: Select all | TOGGLE FULL SIZE
int r = OnlineFeedpub.publish("ON,38.1123,-91.2325,0");


If there is a hard disconnect, the "last will" never posts the OFF of the feed

Everything else works 100%. I am publishing OK, subscribing OK, but "Last Will" will never update "online" to OFF

What am I missing? Does it have something to do with using csv format publishing with the Last Will?

NOTE: Please don't ask me to publish ALL the code, it is huge.

jmontgomry
 
Posts: 5
Joined: Sun Feb 14, 2016 11:37 pm

Re: Adafruit IO MQTT Last Will

by brubell on Wed Oct 16, 2019 4:09 pm

Heya, there's a few things to make sure of, first:

The last will topic must match the normal IO MQTT topic format for feeds or group publishes.

Last will is only published by the MQTT broker if the client fails to disconnect cleanly by sending the MQTT disconnect packet.

Last will is only published by the MQTT broker if the the keep alive timeout expires, and the last will is not sent if your device reconnects within the timeout window. The Adafruit MQTT Library for Arduino has a default keep alive timeout of 5 minutes.

via https://io.adafruit.com/blog/tips/2016/06/24/last-will/

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

Re: Adafruit IO MQTT Last Will

by jmontgomry on Wed Oct 16, 2019 6:58 pm

brubell,

I had already read the article you posted. I have scanned the iNet looking for any possibility of why this will not work.

I use the same format for posting both the keep alive signal and the last will as you can tell from my example (although I wonder if CSV is a hangup?)

The disconnect is "hard".....I pull the pull the plug on the arduino, so there is no disconnect packet.

As I mentioned, I changed the timeout to 30 seconds, although I have waited over an hour with no reconnect.....still no "last will"

jmontgomry
 
Posts: 5
Joined: Sun Feb 14, 2016 11:37 pm

Re: Adafruit IO MQTT Last Will

by jmontgomry on Thu Oct 17, 2019 4:59 pm

brubell,

Are we sure that "Last Will" feature works with the CSV version of publishing?

Any other suggestions on what it could be based on my example code?

jmontgomry
 
Posts: 5
Joined: Sun Feb 14, 2016 11:37 pm

Please be positive and constructive with your questions and comments.