umqtt: keepalive feature doesn’t function correctly
Hi!
Here’s some example code to reproduce the issue:
import ssl
import machine
import network
from umqtt.simple import MQTTClient
client_id = machine.unique_id().hex()
mqtt_server = 'XXX.s1.eu.hivemq.cloud'
mqtt_user = 'XXX'
mqtt_password = 'XXX'
mqtt_topic_sub = b'test/topic'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('XXX','XXX')
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
def mqtt_connect():
client = MQTTClient(client_id, mqtt_server, 8883, mqtt_user, mqtt_password, 15, ssl_context)
client.connect()
print('Connected to %s MQTT Broker'%(mqtt_server))
return client
def reconnect():
print('Failed to connect to the MQTT Broker. Reconnecting...')
machine.reset()
def callback(topic, msg):
print(topic, msg)
try:
client = mqtt_connect()
client.set_callback(callback)
client.subscribe(mqtt_topic_sub)
except OSError as e:
reconnect()
while True:
client.wait_msg()
The library allows setting the keepalive to any value (default is 0, which disables it). However, when you set a keepalive value, the library does not automatically send pings to the server as expected. This causes the server to disconnect the client after 1.5 times the specified keepalive duration.
Are there any possible solutions to this?
I tested this with Mosquitto and HiveMQ serverless.
The ping is not sent automatically by the library. It has to be sent by your code, using the libraries ping() function.
Would be good to see that documented explicitly, both next to the method's API doc and in whatever examples there are.