LastWill mqtt?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
timsson
Normal user
Posts: 77
Joined: 25 Mar 2016, 22:00

LastWill mqtt?

#1 Post by timsson » 04 Apr 2016, 09:51

Hi,
is somethink like that possible?

Code: Select all

#define MQTT_TOPIC_LAST_WILL "/ESP_02/lwt"
mqttClient.connect("ESP_02", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected");
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);

chrille
Normal user
Posts: 88
Joined: 26 Aug 2015, 15:11
Location: Horsens, Denmark

Re: LastWill mqtt?

#2 Post by chrille » 04 Apr 2016, 14:37

timsson wrote:Hi,
is somethink like that possible?

Code: Select all

#define MQTT_TOPIC_LAST_WILL "/ESP_02/lwt"
mqttClient.connect("ESP_02", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected");
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
I support this! LWT support for MQTT would be great - however I think some kind of integration with the UI is preferred. Maybe just a checkbox under MQTT settings, or under advanced settings

- Jan
Jan Chrillesen, Denmark

timsson
Normal user
Posts: 77
Joined: 25 Mar 2016, 22:00

Re: LastWill mqtt?

#3 Post by timsson » 10 Apr 2016, 10:53

Unfortunately, me is as noob the implementation not possible, but i can wait in this great project!
Thanks alot

timsson
Normal user
Posts: 77
Joined: 25 Mar 2016, 22:00

Re: LastWill mqtt?

#4 Post by timsson » 17 Apr 2016, 12:56

Hi,
can you explain the Command.ino with connect?
Maybe we can use lwt???
??On System#Boot do
connect("ESP_02", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected");
publish(MQTT_TOPIC_LAST_WILL, "connected", true);
??
Thanks

P.S.
Thx for this, it works great!
On <event> do
publish /ESP_01/gpio/2 0
endon

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: LastWill mqtt?

#5 Post by tozett » 19 Apr 2016, 12:34

What is your usage of this?
Would be interested in an example.
Thanks for that, if you could provvide one...

timsson
Normal user
Posts: 77
Joined: 25 Mar 2016, 22:00

Re: LastWill mqtt?

#6 Post by timsson » 28 Apr 2016, 15:21

Hello tozett,
i use it to check the status in openhab/mqtt. If the battery or the wifi are down, lastwill change to "disconnectet"
For me is this a good and fast indicator...

Code: Select all

openhab item:
String ESP_04kalive	"keep alive_4: [%s]" {mqtt="<[localbroker:/ESP_04/lwt:state:default]"}
Thx!
keepalive.jpg
keepalive.jpg (44.67 KiB) Viewed 17570 times

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: LastWill mqtt?

#7 Post by tozett » 29 Apr 2016, 13:20

Ah, ok. thanks for supplying me this info.

i am on r105, did you implement the LWT with rules? could you post your working example?

i read on the net some background,
The Last Will and Testament (LWT) feature is used in MQTT to notify other clients about an ungracefully disconnected client. Each client can specify its last will message (a normal MQTT message with topic, retained flag, QoS and payload) when connecting to a broker. The broker will store the message until it detects that the client has disconnected ungracefully. If the client disconnect abruptly, the broker sends the message to all subscribed clients on the topic, which was specified in the last will message. The stored LWT message will be discarded if a client disconnects gracefully by sending a DISCONNECT message.
and i will take over this function to my system. fantastic state info of the ESPs,
thanks

timsson
Normal user
Posts: 77
Joined: 25 Mar 2016, 22:00

Re: LastWill mqtt?

#8 Post by timsson » 29 Apr 2016, 18:04

tozett wrote:i am on r105, did you implement the LWT with rules? could you post your working example?
I use also r105, fantastic projekt! but for lwt i take my one ino file with exactly this syntax and it works fine:

Code: Select all

#define MQTT_TOPIC_LAST_WILL "/ESP_02/lwt"
mqttClient.connect("ESP_02", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected");
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
or do you need the complete ino?
and i use this library:
PubSubClient by Nick O'Leary Version 2.6.0 mqtt3.1.1
Last edited by timsson on 29 Apr 2016, 18:55, edited 2 times in total.

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: LastWill mqtt?

#9 Post by tozett » 29 Apr 2016, 18:21

you mean, you patch the source?
it is not coded in r105 somehow?

its not in there:
https://github.com/ESP8266nu/ESPEasy/se ... &type=Code
so it is/could be feature-request for r106 ...?

timsson
Normal user
Posts: 77
Joined: 25 Mar 2016, 22:00

Re: LastWill mqtt?

#10 Post by timsson » 29 Apr 2016, 18:33

tozett wrote:so it is/could be feature-request for r106 ...?
sorry, yes - i send you a pm

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: LastWill mqtt?

#11 Post by tozett » 30 Apr 2016, 08:28

thanks,
but i thought martinus will maybe reads this,
and will implement this in the gui.
:roll:
configurable last-will-message ...
:)

--i put this on the feature-request list ... and let us be suprised with r106 ..
:twisted:

JR01
Normal user
Posts: 260
Joined: 14 Feb 2016, 21:04
Location: South Africa

Re: LastWill mqtt?

#12 Post by JR01 » 09 May 2016, 17:46

Would like to use LWT as well, +1+1 here
-----------
IOTPLAY. Tinkerer, my projects are @ http://GitHub.com/IoTPlay, and blog https://iotplay.org. Using RPi, Node-Red, ESP8266 to prove Industry 4.0 concepts.

User avatar
lucaberta
Normal user
Posts: 59
Joined: 09 May 2016, 11:26
Location: Lausanne, Switzerland
Contact:

Re: LastWill mqtt?

#13 Post by lucaberta » 09 May 2016, 18:20

timsson wrote: or do you need the complete ino?
and i use this library:
PubSubClient by Nick O'Leary Version 2.6.0 mqtt3.1.1
@timsson have you patched all the source code to support the lastest 2.6.0 version of PubSubClient?

I had to revert back to 1.9.1 to compile ESPEasy earlier today, and was wondering how difficult it would be to refresh the code to support all the latest versions of the different libraries.

Ciao, Luca

timsson
Normal user
Posts: 77
Joined: 25 Mar 2016, 22:00

Re: LastWill mqtt?

#14 Post by timsson » 21 Jul 2016, 21:10

... oh i see a new pubsubclient version by martinus!
great work!
Who can integrale lwt?

timsson
Normal user
Posts: 77
Joined: 25 Mar 2016, 22:00

Re: LastWill mqtt?

#15 Post by timsson » 25 Jul 2016, 13:19

maybe:

ESPEasy.ino
-------------------------
#define MQTT_TOPIC_LAST_WILL "/ESP_01/lwt"

// Setup MQTT Client
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
if (Protocol[ProtocolIndex].usesMQTT)
MQTTConnect();
MQTTclient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
sendSysInfoUDP(3);
-------------------------

Controller.ino
-------------------------
//boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
MQTTclient.connect("ESP_01", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected");
if ((SecuritySettings.ControllerUser[0] != 0) && (SecuritySettings.ControllerPassword[0] != 0))
------------------------

->
mosquitto_sub -t /ESP_01/# -v
/ESP_01/lwt connected
/ESP_01/sys/wifi -77
/ESP_01/sys/wifi -78
/ESP_01/sys/wifi -76
/ESP_01/sys/wifi -75
/ESP_01/sys/time 2
/ESP_01/sys/freeram 26200
/ESP_01/lumi/lux 1024
/ESP_01/lwt disconnected

Martinus

Re: LastWill mqtt?

#16 Post by Martinus » 03 Aug 2016, 18:58

last will is currently implemented with a fixed "connection lost" message to <template>/status topic. Same topic as used for status requests.

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: LastWill mqtt?

#17 Post by tozett » 04 Aug 2016, 10:29

+1
great, thanks!

JR01
Normal user
Posts: 260
Joined: 14 Feb 2016, 21:04
Location: South Africa

Re: LastWill mqtt? // status on queue <template>/status?

#18 Post by JR01 » 04 Aug 2016, 17:58

last will is currently implemented with a fixed "connection lost" message to <template>/status topic. Same topic as used for status requests
Hi @Martinus, somehow I missed the ability to do a status request, I search the forum, don ton get it. Does one send a message into <template>/status on the ESP, and then get a status back? I tried a few ideas, not getting a response back. Also listened in this queue - whilst rebooting the ESP, nothing. Could you point us in the correct direction?
-----------
IOTPLAY. Tinkerer, my projects are @ http://GitHub.com/IoTPlay, and blog https://iotplay.org. Using RPi, Node-Red, ESP8266 to prove Industry 4.0 concepts.

JR01
Normal user
Posts: 260
Joined: 14 Feb 2016, 21:04
Location: South Africa

Re: LastWill mqtt?

#19 Post by JR01 » 04 Aug 2016, 19:29

Thanks Martinus for the updates,

I am on R108, let me upgrade first.... // {post edited after firmware update}

Okay, done, on R118, with pubsub v2.6, wow, looks stable.... So, how can I use the <>/status MQTT queue? Can I push a question into the queue to get status info back? How?
-----------
IOTPLAY. Tinkerer, my projects are @ http://GitHub.com/IoTPlay, and blog https://iotplay.org. Using RPi, Node-Red, ESP8266 to prove Industry 4.0 concepts.

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: LastWill mqtt?

#20 Post by tozett » 05 Aug 2016, 13:52

subscribe to the status-topic, like to any other topy in mqtt

http://www.hivemq.com/blog/mqtt-essenti ... -subscribe

JR01
Normal user
Posts: 260
Joined: 14 Feb 2016, 21:04
Location: South Africa

Re: LastWill mqtt?

#21 Post by JR01 » 06 Aug 2016, 09:11

Thank you @tozett, I got that part working, get my message "Connection Lost" on the <template>/status MQTT channel. But I was asking if it was also designed to ask questions on that channel and get answers back?
-----------
IOTPLAY. Tinkerer, my projects are @ http://GitHub.com/IoTPlay, and blog https://iotplay.org. Using RPi, Node-Red, ESP8266 to prove Industry 4.0 concepts.

timsson
Normal user
Posts: 77
Joined: 25 Mar 2016, 22:00

Re: LastWill mqtt?

#22 Post by timsson » 22 Aug 2016, 10:02

Hello,
please can you add this:?
MQTTclient.publish(LWTTopic.c_str() , "connected", true);
Thanks!

JR01
Normal user
Posts: 260
Joined: 14 Feb 2016, 21:04
Location: South Africa

Re: LastWill mqtt?

#23 Post by JR01 » 22 Aug 2016, 18:47

Hi @timsson ,

I assume this code you are using for, is to send the opposite of 'Connection Lost' - once only, on mqtt connect?

If so: +1 on this request...

...as I want to store in a MySQL db when connection is lost, with the time, and then do same when the connection is re-established.
-----------
IOTPLAY. Tinkerer, my projects are @ http://GitHub.com/IoTPlay, and blog https://iotplay.org. Using RPi, Node-Red, ESP8266 to prove Industry 4.0 concepts.

User avatar
jsilver
Normal user
Posts: 23
Joined: 24 May 2016, 14:45
Location: Lewes, UK
Contact:

Re: LastWill mqtt?

#24 Post by jsilver » 06 Sep 2016, 08:17

JR01 wrote:I assume this code you are using for, is to send the opposite of 'Connection Lost' - once only, on mqtt connect?
+1 from me too :)

wutu
Normal user
Posts: 35
Joined: 05 Feb 2016, 07:33

Re: LastWill mqtt?

#25 Post by wutu » 28 Nov 2016, 11:10

It would be good to solve this separate topic. With online/connected status.

Now I find out whether the device is reconnected to a broker. Respectively. I have in my system two rules are in addition to achieve this.

See also viewtopic.php?f=6&t=2352

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests