I'm using PubSubClient to pub/sub MQTT messages using ESP8266.

As far as I know "Keep-alive" detects when a client went off-line after a period of time.

I'm using library's example mqtt_ESP8266 with 2 minor adjusments (noted "<----- 1/2"), to use keepAlive.

  1. Timeout was set to 45 sec (client.setKeepAlive(45);), in order to be "far enough" from reboot.
  2. Pressing on-board Reset button.
  3. Although Wifi+MQTT connection establish after 10 sec, and mqtt sends messages as shown loop, a "DEAD" publish is sent to designated topic after roughly 45 seconds from (2) , whuch makes no sense since MQTT was reconnected 35 seconds before.

Appreciate assistance, Guy

  Basic ESP8266 MQTT example
  This sketch demonstrates the capabilities of the pubsub library in combination
  with the ESP8266 board/library.
  It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic" every two seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off
  It will reconnect to the server if the connection is lost using a blocking
  reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
  achieve the same result without blocking the main loop.
  To install the ESP8266 board, (using Arduino 1.6.4+):
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
  - Select your ESP8266 in "Tools -> Board"

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "Wifi";
const char* password = "Password";
const char* mqtt_server = "";

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

void setup_wifi() {

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {


  Serial.println("WiFi connected");
  Serial.println("IP address: ");

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  for (int i = 0; i < length; i++) {

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    client.setKeepAlive(45);  \\ <--------- Line 1
    if (client.connect(clientId.c_str(), "guy", "kupelu9e", "myHome/log", 0, true, \\ < "DEAD")) { \\ <---- Line 2
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  client.setServer(mqtt_server, 1883);

void loop() {

  if (!client.connected()) {

  unsigned long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
    Serial.print("Publish message: ");
    client.publish("myHome/debug", msg);

1 Answer 1


This is the correct behavior.

The “last will” message is sent by the broker when it detects that the client has gone away.

When you reset the ESP8266, the original connection to the broker is destroyed. When it restarts, it creates a new, second connection, it does not resume the original connection. So the broker correctly transmits the last will message when the original connection times out.

  • even it connects in same client ID ? so what's the point?
    – guyd
    Jun 17, 2022 at 10:18
  • The point is to notify other clients when a client terminates without disconnecting. The client ID is irrelevant unless you use a session with MQTT 5. Both the client and broker must support MQTT 5 for that to work; in that case, if the client reconnects before the “last will delay”, the will won’t be sent. pubsubclient does not support MQTT 5.
    – romkey
    Jun 17, 2022 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.