martin-ger / esp_mqtt

MQTT Broker/Bridge on the ESP8266
MIT License
293 stars 68 forks source link

Publishing an LWT message when the client is disconnected #63

Closed NOV62 closed 3 years ago

NOV62 commented 3 years ago

Hello there! I really liked Your project. I have a question: can I configure the broker to publish a message in the LWT topic when the client is disconnected? Can I use the "client disconnected" event in the script?

martin-ger commented 3 years ago

No, currently you cannot set an LWT topic as a client. You have to do this, when you connect to an external broker and there is no parameter so far.

There was a static event in https://github.com/martin-ger/esp_mqtt/blob/master/user/user_main.c#L916, but it is disabled now. If you recompile it, you can enable it. If I find some time, I will add config variables for that.

NOV62 commented 3 years ago

Thanks for the answer. Unfortunately, I have a very poor understanding of programming, use Arduino IDE a little and don't know how to recompile your code. The uMQTTBroker library does not suit me because of its very limited capabilities. As far as I understand, if the showmqtt command displays a list of connected clients, then this information could be used in a script, for example: "on client_disconnect do publish LWT / "$ this_client-id" disconnect [retained] ". Why did you disable the above event?

martin-ger commented 3 years ago

We have to distinguish two cases: 1) This esp_mqtt software as a client to another broker: here we have no LWT topic so far as explained above. 2) Other clients connected to this esp_mqtt broker: in this case the client can define its LWT topic on connection and the broker software will automatically publish it to the remaining clients according to the MQTT specification, when the connection is lost. No further action required here. It is up to the client to set the LWT topic. This works.

The rule you are specifying above will publish a topic to the clients of this esp_mqtt broker, when the software disconnects from the other external broker. You can do that, but it is not a classic LWT, as you usuall even don't have a local broker and a connection to another external broker at once.

NOV62 commented 3 years ago

Excellent! I have customized LWT theme for local clients (using ESPEasy), here is a screenshot изображение but esp_mqtt broker does not post message when client disconnects. Where can this be configured? esp_mqtt works in bridge mode.

martin-ger commented 3 years ago

Hmm, this should work without further config.

NOV62 commented 3 years ago

I'm sorry, I need to check again. I expected the LWT disconnect message to be sent to an external broker, but it is clear that this is not happening. This means you need to create a rule for relaying LWT messages from other clients for local clients. It's a crutch, but it should work.

NOV62 commented 3 years ago

OK, the broker is working fine. The cause of ESPEasy firmware problems is extremely unstable WiFi operation, constant reconnections cause data loss and lead to the crash of the broker. After flashing the clients, everything worked. Thanks!