Last Will and Testament using PubSubClient.h and AWS IoT, ESP8266

I am using PubSubClient.h to connect with AWS IoT and it worked fine with shadow topics with the following line

while (!virtual_thing.connect(virtual_thing_name.c_str())){
}

when I tried to do the same for Last Will and Testament topic with the following line , it failed with -4 MQTT state

while (!virtual_thing.connect(virtual_thing_name.c_str(), NULL, NULL, "theEndTopic", 1, true, "offline")){
}

exactly, where is the issue lying ??

error 4 in MQTT usually relates to "Connection Refused: Bad username or password" but are you getting this from the PubSubClient library or from the server?

WiFiClientSecure espClient;
PubSubClient virtual_thing(espClient);

this works

espClient.setTrustAnchors(&cert);
  espClient.setClientRSACert(&client_crt, &key);
  virtual_thing.setServer(MQTT_HOST, MQTT_PORT);
  virtual_thing.setCallback(parse);
  virtual_thing.setBufferSize(2048);
  virtual_thing.setKeepAlive(120);

  //const char* mqttUsername = NULL;
  //const char* mqttPassword = NULL;

  const char* willTopic = "/last/will/topic";
  uint8_t willQoS = 1;
  bool willRetain = true;
  const char* willMessage = "Offline";
  bool cleanSession = true;

  while (!virtual_thing.connect(virtual_thing_name.c_str())){
  //while (!virtual_thing.connect(virtual_thing_name.c_str(),NULL,NULL,willTopic,willQoS,willRetain,willMessage,cleanSession)) {       
    Serial.print(virtual_thing.state());    
    Serial.print(".");
    delay(1000);
  }

this too works

  espClient.setTrustAnchors(&cert);
  espClient.setClientRSACert(&client_crt, &key);
  virtual_thing.setServer(MQTT_HOST, MQTT_PORT);
  virtual_thing.setCallback(parse);
  virtual_thing.setBufferSize(2048);
  virtual_thing.setKeepAlive(120);

  //const char* mqttUsername = NULL;
  //const char* mqttPassword = NULL;

  const char* willTopic = "/last/will/topic";
  uint8_t willQoS = 1;
  bool willRetain = true;
  const char* willMessage = "Offline";
  bool cleanSession = true;

  //while (!virtual_thing.connect(virtual_thing_name.c_str())){
  while (!virtual_thing.connect(virtual_thing_name.c_str(),NULL,NULL/*,willTopic,willQoS,willRetain,willMessage,cleanSession*/)) {       
    Serial.print(virtual_thing.state());    
    Serial.print(".");
    delay(1000);
  }

but this is not working

  espClient.setTrustAnchors(&cert);
  espClient.setClientRSACert(&client_crt, &key);
  virtual_thing.setServer(MQTT_HOST, MQTT_PORT);
  virtual_thing.setCallback(parse);
  virtual_thing.setBufferSize(2048);
  virtual_thing.setKeepAlive(120);

  //const char* mqttUsername = NULL;
  //const char* mqttPassword = NULL;

  const char* willTopic = "/last/will/topic";
  uint8_t willQoS = 1;
  bool willRetain = true;
  const char* willMessage = "Offline";
  bool cleanSession = true;

  //while (!virtual_thing.connect(virtual_thing_name.c_str())){
  while (!virtual_thing.connect(virtual_thing_name.c_str(),NULL,NULL,willTopic,willQoS,willRetain,willMessage,cleanSession)) {       
    Serial.print(virtual_thing.state());    
    Serial.print(".");
    delay(1000);
  }

it shows - 2 in serial monitor

OK this might be related to Can't connect with LWT Last Will and Testament · Issue #678 · knolleary/pubsubclient · GitHub

try to increase the size of the buffer size

Buffersize manipulation is not working.

Those guys in github left for ESP32 instead of solving for ESP8266 :zipper_mouth_face:

I suggest opening a GitHub issue then

Thanks for reaching out. I found the solution.

last will and testament message has to be in proper json

Because of the malformed message, aws iot didn't get connected in my case.

Thanks for sharing the solution

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.