micropython-umqtt.robust2 icon indicating copy to clipboard operation
micropython-umqtt.robust2 copied to clipboard

Constant disconnects, and publish example

Open jt274 opened this issue 1 year ago • 16 comments

Describe the bug I recently switched to this library from umqtt.simple. The client does not subscribe to anything, only publishes data every 30 seconds. On the previous library, it would work consistently for 8-10 hours, then have some kind of crash (I assume a network error). After switching to this library, it publishes data 2-3 times and then says there is a connection error and reconnects. It does this constantly.

The error it shows is (MQTTException(7,), 9)

Relevant code:

from umqtt.robust2 import MQTTClient

def mqtt_connect():
    mqtt_client = MQTTClient(mqtt_client_id, broker, 1883, mqtt_user, mqtt_pass, keepalive=60)
    mqtt_client.set_last_will(last_will_topic, 'Disconnected', retain=True)
    mqtt_client.connect()
    mqtt_client.publish(last_will_topic, 'Connected', retain=True)
    return mqtt_client

def mqtt_reconnect():
    print('Could not connect to MQTT broker, reconnecting: ' + str(mqtt_client.conn_issue))
    mqtt_client.reconnect()
    mqtt_client.publish(last_will_topic, 'Connected', retain=True)

try:
    mqtt_client = mqtt_connect()
    print('MQTT Connected')
except OSError as e:
    print('Error connecting to MQTT, retrying...')
    mqtt_reconnect()

while True:
    if mqtt_client.is_conn_issue():
            while mqtt_client.is_conn_issue():
                mqtt_reconnect()
    else:
        mqtt_client.publish(topic, data, qos=1)
        mqtt_client.check_msg()
        mqtt_client.send_queue()
        sleep(30)

I'd also request an example for a publishing (not subscribing) client. There doesn't seem to be much documentation on that.

Details (please complete the following information):

  • Device board: RPi Pico W
  • Micropython version: latest nightly build
  • Library version 2.1.1

jt274 avatar Apr 23 '23 16:04 jt274