diff --git a/adafruit_espatcontrol/adafruit_espatcontrol.py b/adafruit_espatcontrol/adafruit_espatcontrol.py index 3b8e005..cf61be9 100644 --- a/adafruit_espatcontrol/adafruit_espatcontrol.py +++ b/adafruit_espatcontrol/adafruit_espatcontrol.py @@ -105,6 +105,7 @@ def __init__( self._ipdpacket = bytearray(1500) self._ifconfig = [] self._initialized = False + self._conntype = None def begin(self) -> None: """Initialize the module by syncing, resetting if necessary, setting up @@ -187,6 +188,9 @@ def socket_connect( can be an IP address or DNS (we'll do the lookup for you. Remote port is integer port on other side. We can't set the local port""" # lets just do one connection at a time for now + if conntype == self.TYPE_UDP: + # always disconnect for TYPE_UDP + self.socket_disconnect() while True: stat = self.status if stat in (self.STATUS_APCONNECTED, self.STATUS_SOCKETCLOSED): @@ -209,7 +213,12 @@ def socket_connect( ) replies = self.at_response(cmd, timeout=10, retries=retries).split(b"\r\n") for reply in replies: - if reply == b"CONNECT" and self.status == self.STATUS_SOCKETOPEN: + if reply == b"CONNECT" and ( + conntype == self.TYPE_TCP + and self.status == self.STATUS_SOCKETOPEN + or conntype == self.TYPE_UDP + ): + self._conntype = conntype return True return False @@ -232,6 +241,8 @@ def socket_send(self, buffer: bytes, timeout: int = 1) -> bool: raise RuntimeError("Didn't get data prompt for sending") self._uart.reset_input_buffer() self._uart.write(buffer) + if self._conntype == self.TYPE_UDP: + return True stamp = time.monotonic() response = b"" while (time.monotonic() - stamp) < timeout: @@ -314,6 +325,7 @@ def socket_receive(self, timeout: int = 5) -> bytearray: def socket_disconnect(self) -> None: """Close any open socket, if there is one""" + self._conntype = None try: self.at_response("AT+CIPCLOSE", retries=1) except OKError: