Reputation: 1033
Using the python, mosquitto (mqtt) library, what is the correct way of writing the mqtt reconnect command?
I have tired below, but that doesn't work... Have I missed something?!
mqttc = mosquitto.Mosquitto()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.reconnect_delay_set(120, 300, True)
mqttc.connect("test.mosquitto.org", 1883, 60)
mqttc.subscribe("/foo/bar", 0)
Upvotes: 0
Views: 2601
Reputation: 11608
This needs better documenting, the C library has some documentation but the Python module doesn't.
Your syntax is correct:
def reconnect_delay_set(self, delay, delay_max, exponential_backoff):
...
delay
is the number of seconds to wait between successive reconnect attempts. By default this is set to 1. delay_max
is the maximum number of seconds to wait between reconnection attempts and is also set to 1 by default. This means that the default behaviour is to attempt to reconnect every second.
If delay_max
is greater than delay
, then exponential_backoff
comes into play. Starting with the default of it being set to False, each time the reconnection attempt fails, the delay used increases by delay
, up to a maximum of delay_max
. So if delay=3
and delay_max=15
, then you would get delays of 3, 9, 12, 15, 15, ... In other words delay*reconnect_failures
. Once reconnection succeeds, the delay is reset.
If exponential_backoff
is True, then the behaviour should be to set the delay to delay*reconnect_failures^2
. In your case this would give delays of 120, 240, 300, 300, ...
It appears as though there is an extra term in the case for exponential_backoff=True
so you actually end up getting delays of 300, 300, ... That will be fixed for the upcoming 1.2.2 release.
Upvotes: 3