0

I am trying to get my head around UMQTT.simple. I am looking to handle instances in which my server might disconnect for a reboot. I want to check whether the client is connected, and if not, wait some period and try to reconnect.The guidance seems to be to use client.ping() for this (How to check Micropython umqtt client is connected?).

For the MQTT.paho client I see there is a way to access ping responses in the logs function (see here: http://www.steves-internet-guide.com/mqtt-keep-alive-by-example/). For UMQTT the docs indicate that ping response is handled automatically by wait_msg(): Ping server (response is automatically handled by wait_msg() (https://mpython.readthedocs.io/en/master/library/mPython/umqtt.simple.html). There does not appear to be any analogous logs function mentioned in the UMQTT.simple docs.

This is confounding for a couple of reasons:

  1. If i use client.wait_msg() how do I call client.ping()? client.wait_msg() is a blocking function, so I can't make the ping. The system just disconnects when the keepalive time is reached.
  2. If I call client.check_msg(), and client.ping() intermittently, I can't access the callback. My callback function doesn't have parameters to access pingresponse (params are f(topic, msg) in the docs).

The way I am solving this for now is to set a bunch of try-except calls on my client.connect and then connect-subscribe functions, but its quite verbose. Is this the way to handle or can i take advantage of the pingresponse in UMQTT.simple?

Below is a sample of the code i am running:

#Set broker variables and login credentials
#Connect to the network

#write the subscribe call back
def sub_cb(topic, msg):
    print((topic, msg))            

#write a function that handles connecting and subscribing
def connect_and_subscribe():
    global CLIENT_NAME, BROKER_IP, USER, PASSWORD, TOPIC
    client = MQTTClient(client_id=CLIENT_NAME,
                    server=BROKER_IP,
                    user=USER,
                    password=PASSWORD,
                    keepalive=60)
    client.set_callback(sub_cb)
    client.connect()
    client.subscribe(TOPIC)
    print('Connected to MQTT broker at: %s, subscribed to %s topic' % (BROKER_IP, TOPIC))
    return(client) #return the client so that i can do stuff with it

client = connect_and_subscribe()

#Check messages
now = time.time()
while True:
    try:
        client.check_msg()
    except OSError as message_error: #except if disconnected and check_msg() fails
        if message_error == -1:
            time.sleep(30) #wait for reboot
            try:
                client = connect_and_subscribe() #Try connect again to the server
            except OSError as connect_error: #If the server is still down
                time.sleep(30) #wait and try again
                try:
                    client = connect_and_subscribe()
                except:
                    quit() #Quite so that i don't get stuck in a loop
    time.sleep(0.1)
    if time.time() - now > 80: #ping to keepalive (60 * 1.5)
        client.ping()
        now = time.time() #reset the timer
MorrisseyJ
  • 1,191
  • 12
  • 19

0 Answers0