5

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rob
  • 4,069
  • 3
  • 34
  • 41

2 Answers2

1

In the DHCP.h header file you can find the class definition for a new DHCP connection. Then you can see that there is a default timeout value of 60000ms.

(helpful hint: if you get past the initial effort, and start using eclipse to manage your adruino projects, its really great because you can just press F3 on a functions like Ethernet.begin and take a bit of a trip through the libraries to find these types of settings)

Its difficult to know how long the timeout should be. But a minute seems like a really long time. Of course you don't want to go to short. I wouldn't go less than 15 seconds.

/David Cox

SpiRail
  • 1,377
  • 19
  • 28
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luke Antins
  • 2,010
  • 1
  • 19
  • 15
  • Thanks! I think this will work. Someone also said that I can get the time from a GPS unit, so I may try that too. – rob Dec 16 '11 at 20:41
  • GPS sounds like a grand idea to me, we do that with our hardware to sync the clocks at the beginning. Just remember GPS time is a couple of seconds out of normal UTC time... Also, no need to plugin the ethernet cable. Good luck with your clock project =D – Luke Antins Dec 16 '11 at 21:46
  • Where do I find those "blog posts available on Google covering this exact issue"? – powtac Dec 17 '12 at 09:11
  • @powtac Check the link "using DHCP": http://gkaindl.com/software/arduino-ethernet/dhcp – Luke Antins Feb 18 '13 at 00:24
  • Link does not work – Olejan Apr 10 '23 at 13:57