Skip to content

Commit 5921ecc

Browse files
author
brentru
committed
recalculate remaining length, append QoS to the end of variable header
1 parent 890a4b0 commit 5921ecc

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,16 @@ def publish(self, topic, msg, retain=False, qos=0):
454454

455455
# variable header
456456
pub_hdr_var = bytearray()
457-
pub_hdr_var.append(len(topic) >> 8) # Topic len MSB
458-
pub_hdr_var.append(len(topic) & 0xFF) # Topic len LSB
457+
pub_hdr_var.append(len(topic) >> 8) # Topic len MSB
458+
pub_hdr_var.append(len(topic) & 0xFF) # Topic len LSB
459459
pub_hdr_var.extend(topic.encode("utf-8")) # Topic structure
460-
# TODO: Add PID to variable header if qos > 0
461460

462461
remaining_length = 2 + len(msg) + len(topic)
463462
if qos > 0:
464-
remaining_length += 2 + qos
463+
remaining_length += 2
464+
pub_hdr_var.append(0x00)
465+
pub_hdr_var.append(self._pid)
466+
self._pid += 1
465467

466468
# Remaining length calculation
467469
if remaining_length > 0x7f:
@@ -483,8 +485,6 @@ def publish(self, topic, msg, retain=False, qos=0):
483485
self._sock.send(pub_hdr_var)
484486
self._sock.send(msg)
485487

486-
import gc
487-
gc.collect()
488488

489489
def subscribe(self, topic, qos=0):
490490
"""Subscribes to a topic on the MQTT Broker.

0 commit comments

Comments
 (0)