Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ethernet begin blocks for 60 seconds if there's no Ethernet cable

I'm building a clock. I want to set the clock by plugging an Ethernet cable into the clock. Most of the time the clock would not be plugged into the Internet.

I have an Arduino board and an Ethernet shield that can successfully connect to a time server and read the time (See the UdpNtpClient example file under Examples > Ethernet).

The problem is that to configure the Ethernet shield, the Ethernet.begin() call blocks for 60 sec if the shield is not connected to the Internet. I would like the clock to tell the time and periodically check to see if it has an Ethernet cable plugged in, and if so, make any corrections to the time. Most of the time this check is going to have a negative result, however, so I can't have the clock freeze for 60 sec each time.

Is it is possible to detect if the cable is connected in a quicker way than the Ethernet.begin() function? Is it possible to write a "multithreading" solution, where Ethernet.begin() is non-blocking?

like image 478
rob Avatar asked Nov 05 '22 09:11

rob


1 Answers

Looking at the stock Ethernet library, it's not possible to prevent it from blocking.

I'm guessing you're using DHCP? This appears to be where the blocking comes from. Do you get the same problem when using a static IP address?

There's a number of blog posts available on Google covering this exact issue, including some forks of the Ethernet library that would allow you to do this in a non-blocking fashion.

like image 164
Luke Antins Avatar answered Nov 09 '22 07:11

Luke Antins