From e6bc44668dae1779f614c5e3626e936007b55397 Mon Sep 17 00:00:00 2001 From: Thomas Crescenzi Date: Tue, 23 Apr 2024 19:08:14 -0400 Subject: [PATCH] umqtt.simple: Add ping_response_received for ping handling. Signed-off-by: Thomas Crescenzi --- micropython/umqtt.simple/README.rst | 1 + micropython/umqtt.simple/umqtt/simple.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/micropython/umqtt.simple/README.rst b/micropython/umqtt.simple/README.rst index d9d09b970..06afc5991 100644 --- a/micropython/umqtt.simple/README.rst +++ b/micropython/umqtt.simple/README.rst @@ -51,6 +51,7 @@ follows MQTT control operations, and maps them to class methods: clean_session=True argument is used (default)). * ``disconnect()`` - Disconnect from a server, release resources. * ``ping()`` - Ping server (response is processed automatically by wait_msg()). + * ``ping_response_received`` - True once ping has been responded to, False if not. Set by wait_msg() and reset upon next ping(). * ``publish()`` - Publish a message. * ``subscribe()`` - Subscribe to a topic. * ``set_callback()`` - Set callback for received subscription messages. diff --git a/micropython/umqtt.simple/umqtt/simple.py b/micropython/umqtt.simple/umqtt/simple.py index e84e585c4..a53e05352 100644 --- a/micropython/umqtt.simple/umqtt/simple.py +++ b/micropython/umqtt.simple/umqtt/simple.py @@ -34,6 +34,7 @@ def __init__( self.lw_msg = None self.lw_qos = 0 self.lw_retain = False + self.ping_response_received = False def _send_str(self, s): self.sock.write(struct.pack("!H", len(s))) @@ -112,6 +113,7 @@ def disconnect(self): def ping(self): self.sock.write(b"\xc0\0") + self.ping_response_received = False def publish(self, topic, msg, retain=False, qos=0): pkt = bytearray(b"\x30\0\0\0") @@ -181,6 +183,7 @@ def wait_msg(self): if res == b"\xd0": # PINGRESP sz = self.sock.read(1)[0] assert sz == 0 + self.ping_response_received = True return None op = res[0] if op & 0xF0 != 0x30: