42
42
"""
43
43
import struct
44
44
import time
45
+ import gc
45
46
from random import randint
46
47
from micropython import const
47
48
import adafruit_logging as logging
62
63
MQTT_PINGRESP = const (0xD0 )
63
64
MQTT_SUB = b"\x82 "
64
65
MQTT_UNSUB = b"\xA2 "
65
- MQTT_PUB = bytearray (b"\x30 \0 " )
66
+ MQTT_PUB = bytearray (b"\x30 " )
66
67
MQTT_DISCONNECT = b"\xe0 \0 "
67
68
68
69
# Variable CONNECT header [MQTT 3.1.2]
@@ -440,16 +441,16 @@ def publish(self, topic, msg, retain=False, qos=0):
440
441
msg = str (msg ).encode ("ascii" )
441
442
elif isinstance (msg , str ):
442
443
msg = str (msg ).encode ("utf-8" )
443
- print (type (msg ))
444
444
else :
445
445
raise MMQTTException ("Invalid message data type." )
446
446
if len (msg ) > MQTT_MSG_MAX_SZ :
447
447
raise MMQTTException ("Message size larger than %d bytes." % MQTT_MSG_MAX_SZ )
448
448
self ._check_qos (qos )
449
449
450
450
# 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
+
453
454
454
455
# variable header
455
456
pub_hdr_var = bytearray ()
@@ -462,7 +463,7 @@ def publish(self, topic, msg, retain=False, qos=0):
462
463
463
464
remaining_length = 2 + len (msg ) + len (topic )
464
465
if qos > 0 :
465
- remaining_length += 2 + len ( qos )
466
+ remaining_length += 2 + qos
466
467
467
468
# Remaining length calculation
468
469
if remaining_length > 0x7f :
0 commit comments