Skip to content

ESP32 MQTT Last Will Message Not Published

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 MQTT Last Will Message Not Published. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 MQTT Last Will and Testament (LWT) message is not published when the device disconnects unexpectedly.

Quick Fix

Wrong

client.connect("esp32");  // No Last Will configured
Device disconnects unexpectedly. No offline notification received.
client.setWill("esp32/status", "offline", true, 1);
if (client.connect("esp32")) {
  client.publish("esp32/status", "online", true);
}
Device connects: esp32/status = online
Device loses power: esp32/status = offline (auto-published by broker)

Prevention

Always call setWill() before connect(). Use retained messages (true) for status topics. Use QoS 1 for the will message. Publish online message immediately after connecting. Test by force-disconnecting the device.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### When is the Last Will message published?

The broker publishes the will when it detects an unclean disconnection: network timeout, device crash, or keep-alive expiry.

What is the difference between will and retained?

A will message is published by the broker on unexpected disconnect. A retained message stores the last value. Use both: retained will for the latest status.

Can I update the will message at runtime?

No. The will is set during connect() and cannot be changed without reconnecting. Disconnect and reconnect with a new will.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro