Node Red, MQTT NodeMCU and LWT questions

I have a baker's dozen or so Nodemcu's running MQTT back to Node red. For the most part they all work fine doing what they are supposed to do. I have one that refuses to stay connected for extended periods of time. It may be a week, a day or even a few minutes then it falls off the network. I have done some Node red stuff to interrogate the devices and that seems to work OK for the most part but I decided to try something in LWT to tell me exactly (or close) when it quits. I am suspicious the chip itself is intermittent and I am confident it is not the WIFI, I have moved it around to numerous locations over the months and have set it a few feet from the WAP and it still quits. Back to my question, in the IDE I use the following message to connect to MQTT using the command
MQTTclient.connect("random3,null,null,LWT,1,true,random3 disconnect,null");
It compiles, it uploads, it seems to run fine. According to the documentation I would think on disconnect I would get the message "random3 disconnect" on the topic LWT. I have a MQTT node in Node red set to listen to the topic LWT. In testing it does receive on that topic. I would think if I pulled the power on the chip I would get a message in Node red on the LWT topic that says "random 3 disconnect" but I don't. Is that a valid test? What am I missing? Is there a better more valid test for the LWT?
Thanks
yes, not exactly a node red question but I am hopeful enough in here use arduino's to help
thanks again

Does LWT work on any of the devices? Not all brokers support it. What broker are you using?

Check the broker logs to see what it says about that device connecting and disconnecting.

Sorry, I left out some basic info. Running mosquitto 2.011 The only device I am trying to setup LWT on is the one, so I don't know if others would work. It does show the device (random3) connecting up in the logs. Haven't seen a spot where it disconnects. This test pi has been rebooted several times today though.

Are you specifying a keepalive period in the Nodemcu client?

Does Mosquitto see any PINGREQ messages?

Am NOT specifying any keepalive so I assume it is the default of 15. I have spent a little time going through the logs and have not seen any PINGREQ messages.

This is only speculation but maybe the LWT message only gets published if at least one ping exchange has succeeded.

Is there an option to set a keepalive period at connection? Maybe setting it explicitly will improve matters.

I did find a keepalive command and set it to 10, couldn't hurt.

Is that really what you have in the code? Which development tool is it that expects a single string as the parameter? In the Arduino IDE I have

mqtt_client.connect(host, mqtt_user, mqtt_password, mqtt_will_topic, 1, true, mqtt_will_message))

I'm using Arduino 1.8.19 IDE on the Nodemcu chip.
here's my total mqtt code

void setup_mqtt() { //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
MQTTclient.setKeepAlive (10);
MQTTclient.setServer(mqtt_server, 1883);
//boolean rc = MQTTclient.connect("random3");
MQTTclient.connect("random3,null,null,LWT,1,true,random3 disconnect,null");
MQTTclient.setCallback(callback);
if (!MQTTclient.connected()) {
//Serial.println("Connecting to MQTT...");
}

if (MQTTclient.connect("random3")) {
//Serial.println("connected");
} else {
//Serial.print("failed with state ");
//Serial.print(MQTTclient.state());
delay(2000);
}
MQTTclient.subscribe("random3_subscribe");
MQTTclient.subscribe("Heart");
}

OK, that isn't much like the way I do it, but if it works on the others it must be ok I suppose. Is that using PubSubClient?
Have you got a loop in there somewhere so that if it disconnects then it reconnects again?

Yep, down in the loop I look for both wifi and mqtt disconnect. This code is a Mashup of stuff off the internet and for the most part , it works. If you'd care to share I'd like to see how you do it.
Oops, forgot , it does use pubsubclient

This is definitely not right. The API shows separate arguments: Arduino Client for MQTT

That's what I used, in fact, it's bookmarked, printed out and I sleep with it under my pillow. I will re-evaluate and maybe ask further questions if needed?

The only required parameter is clientID.
Looks like it's using "random3,null,null,LWT,1,true,random3 disconnect,null" as the client id; the last will and testament is not set up at all.
Should be possible to confirm this from the logs.

so I tried this
boolean MQTTclient.connect ("random3", ["LWT", 1, true, "random3 disconnected"]);
and it won't even compile. I have to confess I'm slowly getting to the point of extreme frustration

I think the [ and ] indicate optional parameters, not that these parameters should be wrapped in brackets.

boolean connect (clientID, [username, password], [willTopic, willQoS, willRetain, willMessage], [cleanSession])

Try this?
MQTTclient.connect ("random3",,,"LWT",1,true,"random3 disconnect")

won't compile

this compiles
MQTTclient.connect ("random3","LWT",1,true,"random3 disconnect");

@jbudd you got me real close, after uploading and pulling power I now get a LWT message. I call that a win.
Thanks

2 Likes

sorry this is so late, but you should install MQTT Explorer which could have shown you it the LWT existed. Here is an example from my site
wqttexplorer
a great tool to have in your back pocket