How to keep trying until establishing ethernet connection #9853
-
Hi, I'm programming a wiznet w5100s evb Pico (Pico with integrated ethernet connector). At the moment I have the code below which works, sort of: -- The init function runs What could be a different approach to keep trying indefinitely? EDIT: Thanks! My connecting function:
The output:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You are calling init_ethernet() from inside itself, hence the recursion. have_network = False
while not have_network:
try:
# Keep track of number of attempts
global connection_attempts
connection_attempts += 1
print(f'Connecting... attempt: {connection_attempts}')
# Try nic.active takes 17-18 seconds
nic.active(True)
have_network = True
except OSError as error:
now = time.localtime()
print(f'Connection error: {error}, {now[3]:02d}:{now[4]:02d}:{now[5]:02d}')
# In case of connection error, run this function again |
Beta Was this translation helpful? Give feedback.
-
Besides to what @karfas said, you could set up a timer with a callback, which tries to connect, and when successful, either stops the timer or continues, watching the connections and reconnects, if the connection was broken. If you do that within a class, you can have the status information bound to the class. |
Beta Was this translation helpful? Give feedback.
You are calling init_ethernet() from inside itself, hence the recursion.
A simple loop will use way less resources, e..g.