MQTT last will and testament

Hi, does the MQTT library support last will and testament in case of failure?

Let me ping someone that might be able to help, @rickkas7 or @ParticleD are you able to assist?

MQTT Clients specify their will when connecting to a network. The broker stores the will until it detects a disconnect or ungraceful removal from the network. Once that happens, the will is sent to the subscribed devices.

I poked through the library here that was written for the photon and it looks like it does support it, you just need to specify it in your code - Check out line 102 or CTRL+F for “Will”

1 Like

@Coding_stuff did you ever figure this one out? I can’t see how to specify in my code this undocumented feature
@hirotakaster might you be able to advise?


You specify it when you call the connect function. It is in line 102 like in my comment above. From the library:

bool MQTT::connect(const char *id, const char user, const char pass, const char willTopic, EMQTT_QOS willQos, uint8_t willRetain, const char willMessage, bool cleanSession, MQTT_VERSION version)

1 Like

thx @031014 support.

use willTopic/Qos/Retain/Message like this.

client.connect("sparkclient", "name", "pass", "last/will/topic", MQTT::QOS1, 0,  "disconnect!!", true);

Thank you @hirotakaster - I don’t have a username or a password, so shall I modify like so:

client.connect("sparkclient", "", "", "last/will/topic", MQTT::QOS1, 0, "disconnect!!", true);


client.connect("sparkclient", , , "last/will/topic", MQTT::QOS1, 0, "disconnect!!", true);