@@ -105,6 +105,7 @@ def __init__(
105105 self ._ipdpacket = bytearray (1500 )
106106 self ._ifconfig = []
107107 self ._initialized = False
108+ self ._conntype = None
108109
109110 def begin (self ) -> None :
110111 """Initialize the module by syncing, resetting if necessary, setting up
@@ -187,6 +188,9 @@ def socket_connect(
187188 can be an IP address or DNS (we'll do the lookup for you. Remote port
188189 is integer port on other side. We can't set the local port"""
189190 # lets just do one connection at a time for now
191+ if conntype == self .TYPE_UDP :
192+ # always disconnect for TYPE_UDP
193+ self .socket_disconnect ()
190194 while True :
191195 stat = self .status
192196 if stat in (self .STATUS_APCONNECTED , self .STATUS_SOCKETCLOSED ):
@@ -209,7 +213,12 @@ def socket_connect(
209213 )
210214 replies = self .at_response (cmd , timeout = 10 , retries = retries ).split (b"\r \n " )
211215 for reply in replies :
212- if reply == b"CONNECT" and self .status == self .STATUS_SOCKETOPEN :
216+ if reply == b"CONNECT" and (
217+ conntype == self .TYPE_TCP
218+ and self .status == self .STATUS_SOCKETOPEN
219+ or conntype == self .TYPE_UDP
220+ ):
221+ self ._conntype = conntype
213222 return True
214223 return False
215224
@@ -232,6 +241,8 @@ def socket_send(self, buffer: bytes, timeout: int = 1) -> bool:
232241 raise RuntimeError ("Didn't get data prompt for sending" )
233242 self ._uart .reset_input_buffer ()
234243 self ._uart .write (buffer )
244+ if self ._conntype == self .TYPE_UDP :
245+ return True
235246 stamp = time .monotonic ()
236247 response = b""
237248 while (time .monotonic () - stamp ) < timeout :
@@ -314,6 +325,7 @@ def socket_receive(self, timeout: int = 5) -> bytearray:
314325
315326 def socket_disconnect (self ) -> None :
316327 """Close any open socket, if there is one"""
328+ self ._conntype = None
317329 try :
318330 self .at_response ("AT+CIPCLOSE" , retries = 1 )
319331 except OKError :
0 commit comments