Sitemap

Using MQTT “Last Will or Testament” with Python

3 min readDec 4, 2023

--

In IoT systems, the MQTT protocol stands out for its lightweight, flexible, reliable, and low-bandwidth consumption, facilitating data transmission between devices. In this context, the MQTT broker, especially the “Last Will and Testament” feature, plays a crucial role in handling unexpected situations.

What is Last Will or Testament?

MQTT’s “Last Will and Testament” feature is used to notify other devices when a device unexpectedly loses its connection. For example, “Hello! This device has lost its connection. Let all subscribed devices be informed.” This feature enables the MQTT broker to instantly provide information to all devices on your behalf.

How Does Last Will or Testament Work?

Last Will or Testament allows a device to publish its last message through the broker on a specified topic. This enables other devices to quickly detect this situation and take the necessary precautions.

Last Will Parameters:

· Last Will Topic: Communication is established through a specific topic, expressing the device’s state.

· Last Will Message: This is the message that will be published when the device’s connection is lost. For example, it may contain information like “Connection Lost.”

Get Kardelen Yurtkuran’s stories in your inbox

Join Medium for free to get updates from this writer.

· Last Will Retain: This parameter determines whether the message will be retained by the broker or not. It controls whether this message will still be published when the connection is restored.

· Last Will QoS Level: This parameter defines the message’s quality of service for transmission. A QoS level is set for high-quality transmission.

Why Use Last Will or Testament?

In IoT systems, data flow is continuous. If a device unexpectedly loses its connection, other devices must detect this situation and take the necessary precautions. Last Will or Testament is an ideal solution for handling such scenarios.

Python Example with Last Will or Testament

Python and MQTT are powerful tools commonly used in IoT systems. Below is a simple example of using the “Last Will and Testament” feature with a Python MQTT client. In this example, the will_set function determines the message to be sent when the connection is lost on the "topic/ad" topic. Additionally, the on_disconnect function is automatically called when the connection is lost, thereby sending the "Last Will" message.

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
print("Connected. Code: " + str(rc))
client.subscribe("topic/ad")

def on_message(client, userdata, msg):
print("Message Received: " + msg.payload.decode())

# Setting up Last Will and Testament
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Connection Lost. Sending Last Will...")
client.publish("topic/ad", payload="Connection Lost", qos=2, retain=True)

client = mqtt.Client()
# Setting up functions for connection and disconnection events
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.will_set("topic/ad", payload="Connection Lost", qos=2, retain=True)
client.connect("broker_address", 1883, 60)
client.loop_forever()

This example ensures that a specific message is published if a device unexpectedly loses its connection, allowing other devices or systems to take appropriate measures.

--

--