homieiot / homie-esp8266

💡 ESP8266 framework for Homie, a lightweight MQTT convention for the IoT
http://homieiot.github.io/homie-esp8266
MIT License
1.36k stars 307 forks source link

Is there a way to send LWT? #163

Closed diazleonardo closed 7 years ago

diazleonardo commented 8 years ago

If there is not, may I suggest adding it? I quickly went through the MQTT standard and found no mention to LWT. Is a description of what, and when to send the LWT somewhere?

If this helps on the implementation, the signature for different clients is as follows:

paho-mqtt: will_set(topic, payload=None, qos=0, retain=False)

NodeMCU client: mqtt:lwt(topic, message[, qos[, retain]])

IBM WebSphere MQ Telemetry: MqttConnectionOptions.setWill(MqttTopic lastWillTopic, byte [] lastWillPayload, int lastWillQos, boolean lastWillRetained)

benzino77 commented 8 years ago

homie will leave LWT in devices/<id>/$online topic. When it not cleanly disconnect from broker (i.e. network failure) broker will set this topic to false.

You can read more about lwt here: http://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament

diazleonardo commented 8 years ago

Fair enough, but is there a way where I can set the topic and message for the LWT? There are many reasons for going offline, in my especial case, deep sleep.

benzino77 commented 8 years ago

In version 2.0 there is new functionality. Have a look at here

Yo can also try to define your own LWT by getting acces to underlying homie mqtt client: mqtt-client, but I'm not sure if it won't broke homie itself. Here is the link to async-mqtt-client used by homie async-mqtt-client

marvinroger commented 8 years ago

LWT is defined in the spec here.

Only one LWT can be defined, and, following the Homie convention, homie-esp8266 defines one as @benzino77 explained. So yes, you can use the prepareForSleep() function which will take care of cleaning the MQTT connection so you can deep sleep.