MQTT’s last will feature allows you to ask the Adafruit IO servers to publish a message to a feed or group on your behalf. This is helpful if you would like to track when your device unexpectedly disconnects due to issues like network or power outages.
Here are some helpful tips from the IO forum that might help you if you are looking to use MQTT’s last will feature with Adafruit IO:
- 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.
Here’s an example of setting a last will message using the Adafruit MQTT Library for Arduino:
// always set the last will message before calling connect. // if the device unexpectedly disconnects, the 'disconnect' // feed will receive the 'water monitor disconnected' message. mqtt.will("your_username_here/feeds/disconnect", "water monitor disconnected"); mqtt.connect();