4242"""
4343import struct
4444import time
45+ import gc
4546from random import randint
4647from micropython import const
4748import adafruit_logging as logging
6263MQTT_PINGRESP = const (0xD0 )
6364MQTT_SUB = b"\x82 "
6465MQTT_UNSUB = b"\xA2 "
65- MQTT_PUB = bytearray (b"\x30 \0 " )
66+ MQTT_PUB = bytearray (b"\x30 " )
6667MQTT_DISCONNECT = b"\xe0 \0 "
6768
6869# Variable CONNECT header [MQTT 3.1.2]
@@ -440,16 +441,16 @@ def publish(self, topic, msg, retain=False, qos=0):
440441 msg = str (msg ).encode ("ascii" )
441442 elif isinstance (msg , str ):
442443 msg = str (msg ).encode ("utf-8" )
443- print (type (msg ))
444444 else :
445445 raise MMQTTException ("Invalid message data type." )
446446 if len (msg ) > MQTT_MSG_MAX_SZ :
447447 raise MMQTTException ("Message size larger than %d bytes." % MQTT_MSG_MAX_SZ )
448448 self ._check_qos (qos )
449449
450450 # fixed header
451- pub_hdr_fixed = bytearray (b"\x30 " )
452- pub_hdr_fixed [0 ] |= qos << 1 | retain
451+ pub_hdr_fixed = MQTT_PUB
452+ pub_hdr_fixed [0 ] |= retain | qos << 1
453+
453454
454455 # variable header
455456 pub_hdr_var = bytearray ()
@@ -462,7 +463,7 @@ def publish(self, topic, msg, retain=False, qos=0):
462463
463464 remaining_length = 2 + len (msg ) + len (topic )
464465 if qos > 0 :
465- remaining_length += 2 + len ( qos )
466+ remaining_length += 2 + qos
466467
467468 # Remaining length calculation
468469 if remaining_length > 0x7f :
0 commit comments