diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index a3b0d77..fdbc508 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -178,6 +178,7 @@ def __init__( self.mac_address = mac self.src_port = 0 self._dns = 0 + # Set DHCP self._dhcp_client = None if is_dhcp: @@ -196,6 +197,17 @@ def set_dhcp(self, hostname=None, response_timeout=30): """ if self._debug: print("* Initializing DHCP") + + # First, wait link status is on + # to avoid the code during DHCP - assert self.link_status, "Ethernet cable disconnected!" + start_time = time.monotonic() + while True: + if self.link_status or ((time.monotonic() - start_time) > 5): + break + time.sleep(1) + if self._debug: + print("My Link is:", self.link_status) + # Return IP assigned by DHCP self._dhcp_client = dhcp.DHCP( self, self.mac_address, hostname, response_timeout, debug=self._debug diff --git a/examples/wiznet5k_simpleserver.py b/examples/wiznet5k_simpleserver.py index deb42fc..548af79 100644 --- a/examples/wiznet5k_simpleserver.py +++ b/examples/wiznet5k_simpleserver.py @@ -28,9 +28,10 @@ server.bind((server_ip, server_port)) # Bind to IP and Port server.listen() # Begin listening for incoming clients +conn, addr = server.accept() # Wait for a connection from a client. while True: - conn, addr = server.accept() # Wait for a connection from a client. with conn: - data = conn.recv() - print(data) - conn.send(data) # Echo message back to client + data = conn.recv(1024) + if data: # Wait for receiving data + print(data) + conn.send(data) # Echo message back to client