73
73
const (0x04 ) : 'Connection Refused - Incorrect username/password' ,
74
74
const (0x05 ) : 'Connection Refused - Unauthorized' }
75
75
76
+ _the_interface = None # pylint: disable=invalid-name
77
+ _the_sock = None # pylint: disable=invalid-name
78
+
76
79
class MMQTTException (Exception ):
77
80
"""MiniMQTT Exception class."""
78
81
# pylint: disable=unnecessary-pass
79
82
#pass
80
83
84
+ def set_socket (sock , iface = None ):
85
+ """Helper to set the global socket and optionally set the global network interface.
86
+ :param sock: socket object.
87
+ :param iface: internet interface object
88
+
89
+ """
90
+ global _the_sock # pylint: disable=invalid-name, global-statement
91
+ _the_sock = sock
92
+ if iface :
93
+ global _the_interface # pylint: disable=invalid-name, global-statement
94
+ _the_interface = iface
95
+ _the_sock .set_interface (iface )
96
+ print (_the_sock )
97
+
98
+ def unpretty_ip (ip ): # pylint: disable=no-self-use, invalid-name
99
+ """Converts a dotted-quad string to a bytearray IP address"""
100
+ octets = [int (x ) for x in ip .split ('.' )]
101
+ return bytes (octets )
102
+
81
103
class MQTT :
82
104
"""MQTT Client for CircuitPython
83
105
:param socket: Socket object for provided network interface
@@ -92,19 +114,13 @@ class MQTT:
92
114
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
93
115
"""
94
116
# pylint: disable=too-many-arguments,too-many-instance-attributes, not-callable, invalid-name, no-member
95
- def __init__ (self , socket , broker , port = None , username = None ,
96
- password = None , network_manager = None , client_id = None ,
117
+ def __init__ (self , broker , port = None , username = None ,
118
+ password = None , client_id = None ,
97
119
is_ssl = True , log = False , keep_alive = 60 ):
98
- # network management
99
- self ._socket = socket
100
- network_manager_type = str (type (network_manager ))
101
- if 'ESPSPI_WiFiManager' in network_manager_type :
102
- self ._wifi = network_manager
103
- else :
104
- raise TypeError ("This library requires a NetworkManager object." )
120
+ self ._sock = None
105
121
# broker
106
122
try : # set broker IP
107
- self .broker = self . _wifi . esp . unpretty_ip (broker )
123
+ self .broker = unpretty_ip (broker )
108
124
except ValueError : # set broker URL
109
125
self .broker = broker
110
126
# port/ssl
@@ -187,11 +203,10 @@ def connect(self, clean_session=True):
187
203
"""Initiates connection with the MQTT Broker.
188
204
:param bool clean_session: Establishes a persistent session.
189
205
"""
190
- self ._set_interface ()
191
- if self .logger is not None :
192
- self .logger .debug ('Creating new socket' )
193
- self ._sock = self ._socket .socket ()
194
- self ._sock .settimeout (10 )
206
+ global _the_interface # pylint: disable=global-statement, invalid-name
207
+ global _the_sock # pylint: disable=global-statement, invalid-name
208
+
209
+
195
210
if self .port == 8883 :
196
211
try :
197
212
if self .logger is not None :
@@ -201,14 +216,15 @@ def connect(self, clean_session=True):
201
216
raise MMQTTException ("Invalid broker address defined." )
202
217
else :
203
218
if isinstance (self .broker , str ):
204
- addr = self . _socket . getaddrinfo (self .broker , self .port )[0 ][ - 1 ]
219
+ addr = _the_sock . getaddrinfo (self .broker , self .port )[0 ]
205
220
else :
206
221
addr = (self .broker , self .port )
207
222
try :
208
223
if self .logger is not None :
209
224
self .logger .debug ('Attempting to establish insecure MQTT connection...' )
210
- #self._sock.connect((self.broker, self.port), TCP_MODE)
211
- self ._sock .connect (addr , TCP_MODE )
225
+ self ._sock = _the_sock .socket (addr [0 ], 0x21 , addr [2 ])
226
+ self ._sock .settimeout (15 )
227
+ self ._sock .connect (addr [- 1 ], TCP_MODE )
212
228
except RuntimeError as e :
213
229
raise MMQTTException ("Invalid broker address defined." , e )
214
230
@@ -641,6 +657,7 @@ def _wait_for_msg(self, timeout=30):
641
657
"""
642
658
res = self ._sock .recv (1 )
643
659
self ._sock .settimeout (timeout )
660
+ print ("RES: " , res )
644
661
if res in [None , b"" ]:
645
662
return None
646
663
if res == MQTT_PINGRESP :
0 commit comments