Last Will message is published by MQTT Broker while client is still connected

33 views
Skip to first unread message

Mohammad Nazmul Hasan Pias

unread,
Oct 7, 2020, 11:32:15 AM10/7/20
to emqtt
I'm using Nodemcu-32S as MQTT Client for connecting with an MQTT broker. The broker is EMQ X, it is hosted in VPS (Windows Server 2016,  4 core, 8GB Ram).

The problem is, MQTT broker sends last will message within 10 to 20 seconds of connecting to the server,  while the client is still connected and publishing and subscribing data properly.


Last will message topic is "Connection/online".

When the Nodemcu-32S is connected, it publishes, "// 1234 1 12:10:25T121020//" 

When the Nodemcu-32S ungracefully disconnected like power or network failure , it publishes, "// 1234 0 12:10:25T121020//" 

My code is :


#include <Wire.h> 
#include "SH1106Wire.h"
#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "*******";
const char* password = "******";

//*******************  Time Settings ******************************
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 21600;
const int   daylightOffset_sec = 0;   // no need for dhaka

char time_stmp[50];



//************************* MQTT Settings ***************************


const char* mqtt_server = "207.***.232.***";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[100];
int value = 0;

char dev_id[]="DEV_002";

byte willQoS = 1;
const char willTopic[] = "Connection/online";
//const char willTopic[] = "$SYS/brokers/node1/clients/clientid1/disconnected";
char willMessage[50];
boolean willRetain = false;

char txt[50];  // for reply

//********************************** OLED *******************************************

SH1106Wire display(0x3c, 21, 22);  // ADDRESS, SDA, SCL  , NODEMCU-32S ESP32

//***********************************************************************************

void print_LocalTime()
{
  struct tm timeinfo;
 
  if(!getLocalTime(&timeinfo))
  {
    Serial.println("Failed to obtain time");
    return;
  }
strftime(time_stmp, sizeof(time_stmp), "%H:%M:%ST%d.%m.%y", &timeinfo);
}

void setup_wifi()
{
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

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

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

  // Switch on the LED if an 1 was received as first character

  if (strcmp(topic,"esp_out")==0)
  {
      // whatever you want for this topic
 
   
    if ((char)payload[0] == '1')
    {
      digitalWrite(2, HIGH);   // 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)
    }
    if ((char)payload[0] == '2')
    {
      digitalWrite(2, LOW);  // Turn the LED off by making the voltage HIGH
    }
  }
}

void reconnect()
{
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
   
   
    //String clientId = "ESP8266Client-";
    String clientId = "1234";
   
    //clientId += String(random(0xffff), HEX);
   
    // Attempt to connect
    //if (client.connect(clientId.c_str()))

    print_LocalTime();

    snprintf (willMessage, sizeof(willMessage), "// %s 0 %s//",clientId,time_stmp);
   
    //boolean rc= client.connect(clientId.c_str(), willTopic, willQoS, willRetain, willMessage);
    if (client.connect(clientId.c_str(), willTopic, willQoS, willRetain, willMessage))
    {
      Serial.println("connected");
      // Once connected, publish an announcement...
      snprintf (txt, sizeof(txt), "// %s 1 %s//",clientId,time_stmp);
      boolean rcc = client.publish(willTopic, txt);
      if ( rcc)
      {
      Serial.print(willTopic);
      Serial.print(" : ");
      Serial.print(txt);
      Serial.println("  Oaky");
      }
      else
      Serial.println(" Online Status publish error");
      // ... and resubscribe
      client.subscribe("esp_out");

     
     
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{

  pinMode(2, OUTPUT); // buzzer
  Serial.begin(115200);
  delay(100);
  display.init();
  display.flipScreenVertically();
  //display.setFont(ArialMT_Plain_10);
  delay(100);
  display.clear();
 
 
  setup_wifi();
  delay(100);
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); 
  print_LocalTime();
  delay(100);
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
 
  //logo_print();
}



void loop()
{

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
 
  if (now - lastMsg > 500)
  {

    lastMsg = now;
    ++value;
   
    snprintf (msg, 100, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("esp_in", msg);
  }

}


The last will message, "// 1234 0 12:10:25T121020//" , is supposed to be published in topic "Connection/online", if the NOdemcu-32S MQTT client is disconnected ungracefully. But this message is published while the client is still connected, and publishing and subscribing data properly.

Dave

unread,
Mar 1, 2021, 12:30:38 AM3/1/21
to emqtt
Did you ever get this working?
Reply all
Reply to author
Forward
0 new messages