@@ -126,6 +126,7 @@ class MQTT:
126
126
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
127
127
:param socket socket_pool: A pool of socket resources available for the given radio.
128
128
:param ssl_context: SSL context for long-lived SSL connections.
129
+ :param bool use_binary: Sets if message is passed as bytearray instead of string to callbacks
129
130
130
131
"""
131
132
@@ -141,12 +142,14 @@ def __init__(
141
142
keep_alive = 60 ,
142
143
socket_pool = None ,
143
144
ssl_context = None ,
145
+ use_binary = False ,
144
146
):
145
147
146
148
self ._socket_pool = socket_pool
147
149
self ._ssl_context = ssl_context
148
150
self ._sock = None
149
151
self ._backwards_compatible_sock = False
152
+ self ._use_binary = use_binary
150
153
151
154
self .keep_alive = keep_alive
152
155
self ._user_data = None
@@ -839,8 +842,9 @@ def _wait_for_msg(self, timeout=0.1):
839
842
pid = pid [0 ] << 0x08 | pid [1 ]
840
843
sz -= 0x02
841
844
# read message contents
842
- msg = self ._sock_exact_recv (sz )
843
- self ._handle_on_message (self , topic , str (msg , "utf-8" ))
845
+ raw_msg = self ._sock_exact_recv (sz )
846
+ msg = raw_msg if self ._use_binary else str (raw_msg , "utf-8" )
847
+ self ._handle_on_message (self , topic , msg )
844
848
if res [0 ] & 0x06 == 0x02 :
845
849
pkt = bytearray (b"\x40 \x02 \0 \0 " )
846
850
struct .pack_into ("!H" , pkt , 2 , pid )
0 commit comments