MQTT Last Will and Testament Examples

last-will-messages-iconThe last will and testament message is used to notify subscribers of an unexpected shut down of the publisher.

The basic process is.

  1. The publisher tells the broker to notify all subscribers to a topic, using the last will message , in the event that the connection breaks
  2. If the broker detects a connection break it sends the last will message to all subscribers of that topic.

Again you will find more detail in this MQTT essentials article here



Example Outline and Objectives

In this Example  I will be using a 2 python scripts that represent two clients called Python1 and Python2.

One client is used as the subscriber to some topics and the other script is used to publish messages  on those topics.

The objective is to see what happens when the publisher gets disconnected.

Procedure :

  1. Use a client to publish messages.
  2. The publisher will set a last will message for a topic.
  3. We monitor the publisher using a subscribing client.
  4. We break the connection between the publisher and broker first using an normal disconnect and secondly using an abnormal disconnect.
  5. We observer the result.

Example 1- Normal Disconnect

In this example we disconnect from the broker by sending a disconnect message and note that no Last will Message is sent.

last-will-diconnect

Notes Refer to screenshot above

1.The subscriber client starts

2 The publisher sets a last will message for one (bulb1).

3. The publisher publishes two test messages one on each topic.

4. The subscriber sees these messages.

5. The connection between the publisher and broker is broken by sending a normal disconnect command. Notice the Broker screen showing this disconnect

6. Notice that no Last will message is received by the subscriber

Example 2- Abnormal Disconnect

In this example we disconnect from the broker by simulating a network fault  and note that a Last will Message is sent.

mqtt-ast-will-example

Notes Refer to screenshot above

1.The subscriber client starts

2 The publisher sets a last will message for one (bulb1).

3. The publisher publishes two test messages one on each topic.

4. The subscriber sees these messages.

5. The connection between the publisher and broker is broken. .I achieved the connection break simply by closing the python IDE for the publisher client. The broker sees it as a socket error. -see broker screen shot below.

mqtt-last-will-broker

6. The Last will message is sent by the broker and received by the subscriber

Python MQTT Client Notes

To set the last will message you use the auxiliary function will_set(). This you must call before you establish the connection.

Here is a code snippet taken for my example scripts.


lwm="Bulb1 Gone Offline" # Last will message
topic1="bulb1"
print("Setting Last will message=",lwm,"topic is",topic1 )
client2.will_set("bulb1",lwm,QOS1,retain=False)

Note: Setting a QOS of 1 for the last will message attempts to makes sure the subscribers see it.

Setting the retain flag to True will guarantee this even to subscribers that don’t currently have a connection.

Last Will Usage Example

A client (sensor or device) could publish it’s current connection status on a topic e.g. sensors/sensor1/connection_status.

The client would update this status when starting and when disconnecting. The Last Will would then be used to update the status in the case of a failure.

Summary

The last will message if used is only sent if a publisher is disconnected abnormally. e.g. through a network error.

If the publisher disconnects uses the MQTT disconnect sequence then no last Will message is sent.

MQTT by Example Series

Related tutorials and Resources:

Save

Save

Save

Facebooktwittergoogle_plusredditpinterestlinkedinmail