Skip to content

Commit

Permalink
umqtt.simple: add ping_response_received for ping handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
trescenzi committed Apr 23, 2024
1 parent 45ead11 commit 8322f75
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions micropython/umqtt.simple/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions micropython/umqtt.simple/umqtt/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 8322f75

Please sign in to comment.