Problems with Mosquitto and last will (testament)

mosquitto, mqtt, paho, python

Solution

You can try using the single method to publish just one message like this:

import paho.mqtt.publish as publish

publish.single('hello/world', 'Regular msg', 0, False, 'localhost' , 1883, 'publisher', 10, {'topic': 'hello/will', 'payload': 'Will msg', 'qos': 0, 'retain': False})

https://pypi.python.org/pypi/paho-mqtt#single

I would guess the problem is that you are disconnecting before the publish has actually completed which may be why you are seeing the will message.

EDIT - When I run your code with mosquitto_sub -v -t 'hello/#' I see both the normal message and the will being delivered.

EDIT2 -

This works fine for me:

import paho.mqtt.client as mqtt

client = mqtt.Client()

client.will_set('hello/will', 'Last will', 0, False)
client.connect('localhost', 1883)

client.publish('hello/world', 'Regular msg', 0, False)
client.loop();
client.disconnect()
client.loop();

Problem

I'm using Mosquitto and the Python implementation of Paho to try to communicate a couple of programmes. I'm getting some troubles when I use the last will function. My code is this: Suscriber: ``` import paho.mqtt.client as mqtt def on_message(client, userdata, msg): print 'Received: ' + msg.payload client = mqtt.Client() client.on_message = on_message client.connect('localhost', 1883) client.subscribe('hello/#') client.loop_forever() ``` Publisher: ``` import paho.mqtt.client as mqtt client = mqtt.Client() client.will_set('hello/will', 'Last will', 0, False) client.connect('localhost', 1883) client.publish('hello/world', 'Regular msg', 0, False) client.disconnect() ``` The output: ``` Received: Last will ``` I should receive only the regular message because I use `client.disconnect()` to close the connection. If I comment the `will_set` line, I get the regular message. I also tried publishing both on the same topic and it doesn't work.

Original source