@@ -102,7 +102,6 @@ def unpretty_ip(ip): # pylint: disable=no-self-use, invalid-name
102
102
103
103
class MQTT :
104
104
"""MQTT Client for CircuitPython
105
- :param socket: Socket object for provided network interface
106
105
:param str broker: MQTT Broker URL or IP Address.
107
106
:param int port: Optional port definition, defaults to 8883.
108
107
:param str username: Username for broker authentication.
@@ -112,6 +111,7 @@ class MQTT:
112
111
:param bool is_ssl: Sets a secure or insecure connection with the broker.
113
112
:param bool log: Attaches a logger to the MQTT client, defaults to logging level INFO.
114
113
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
114
+
115
115
"""
116
116
# pylint: disable=too-many-arguments,too-many-instance-attributes, not-callable, invalid-name, no-member
117
117
def __init__ (self , broker , port = None , username = None ,
@@ -177,6 +177,7 @@ def __exit__(self, exception_type, exception_value, traceback):
177
177
def deinit (self ):
178
178
"""De-initializes the MQTT client and disconnects from
179
179
the mqtt broker.
180
+
180
181
"""
181
182
self .disconnect ()
182
183
@@ -186,6 +187,7 @@ def last_will(self, topic=None, message=None, qos=0, retain=False):
186
187
:param str message: Last will disconnection message.
187
188
:param int qos: Quality of Service level.
188
189
:param bool retain: Specifies if the message is to be retained when it is published.
190
+
189
191
"""
190
192
if self ._is_connected :
191
193
raise MMQTTException ('Last Will should be defined before connect() is called.' )
@@ -202,10 +204,25 @@ def last_will(self, topic=None, message=None, qos=0, retain=False):
202
204
def connect (self , clean_session = True ):
203
205
"""Initiates connection with the MQTT Broker.
204
206
:param bool clean_session: Establishes a persistent session.
207
+
205
208
"""
206
- global _the_interface # pylint: disable=global-statement, invalid-name
207
- global _the_sock # pylint: disable=global-statement, invalid-name
209
+ try :
210
+ proto , dummy , self .broker , path = self .broker .split ("/" , 3 )
211
+ # replace spaces in path
212
+ path = path .replace (" " , "%20" )
213
+ except ValueError :
214
+ proto , dummy , self .broker = self .broker .split ("/" , 2 )
215
+ path = ""
216
+ if proto == "http:" :
217
+ self .port = MQTT_TCP_PORT
218
+ elif proto == "https:" :
219
+ self .port = MQTT_TLS_PORT
220
+ else :
221
+ raise ValueError ("Unsupported protocol: " + proto )
208
222
223
+ if ":" in self .broker :
224
+ self .broker , port = self .broker .split (":" , 1 )
225
+ port = int (port )
209
226
210
227
if self .port == 8883 :
211
228
try :
@@ -218,7 +235,7 @@ def connect(self, clean_session=True):
218
235
if isinstance (self .broker , str ):
219
236
addr = _the_sock .getaddrinfo (self .broker , self .port )[0 ]
220
237
else :
221
- addr = (self .broker , self .port )
238
+ addr = (self .broker , 0x21 , self .port )
222
239
try :
223
240
if self .logger is not None :
224
241
self .logger .debug ('Attempting to establish insecure MQTT connection...' )
0 commit comments