Go Down

Topic: Ethernet shield getting time from internet clock server? (Read 26976 times) previous topic - next topic


Hi everybody.
Im working on building a weather sensor station based upon posting sensordata via the ethernet shield online to php and i really need to post the time the data gets collected along with the sensordata.

My first solution was a rt clock module but then i realised maybe the ethernet shield could get the real time clock from the web somewhere?
Is that even possible?

I'll write some more bout the weather station in another thread :)


Check - http://www.arduino.cc/playground/Main/DS1307OfTheLogshieldByMeansOfNTP
might be helpfull
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


you have ethernet examples
it contains udpntpclient which you should try


I should caution you that NTP was not intended for getting the time every time you want it. A while back there was a big fuss when some router manufacturer used NTP to get the time every time it needed to log some activity. The extra calls to NTP servers (or one server in particular from memory) brought it to its knees and it had to be disabled.

Or alternatively you might find that an annoyed NTP time operator discovers that your IP address is polling it every few seconds and adds you to a block list. Then you won't have the time at all.

Also, since getting the time itself takes time, it could slow down collecting your data (depending on how often you test your sensors).

What would be a reasonable compromise would be to get the time in setup, and then remember that. Then get the time from startup with millis(), and add one to the other. So for example, if you started up at 9 am on 4th June, and 4 hours have elapsed, clearly it is 1 pm.

You might reset the time (get it again from the NTP server) every 24 hours or so to allow for timer drift.

In your particular case, since you are posting the time to PHP, can't you just remember the time it was posted (at the PHP end)? Or are you batching it up?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


try this code which is working for me and doing approximately what nick gammon says
about ntp server, it is true that you should not abuse their service.
once a day should be a reasonable pace.
if you want to call the server on an rather intensive manner, you can contact the owner by mail.
you'll find lists of server on the net with their policy and mails

Code: [Select]

//   time ntp avec aff date time en clair
#include <SPI.h>
#include <Time.h>
#include <Ethernet.h>
#include <NTP.h>
#include <LiquidCrystal.h>

byte mac[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; // ma mac adresse
byte ip[] = { 192, 168, 0, 5 }; //  ip fixe arduino ethernet shield
byte gateway[] = { 192, 168, 0, 254 }; // freebox
byte mask[] = { 255,255,255,0}; // masque reseau

byte SNTP_server_IP[] = {132,246,11,231}; //ntp server canon.inria.fr {192,93,2,20}; sinon time.nrc.ca
time_t prevDisplay = 0; // when the digital clock was displayed
const  long timeZoneOffset = -3600L; // set this to the offset in seconds to your local time; ici 1 heure d hiver

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 7, 5, 6, 3, 2);

void setup()
   //aff lcd
   lcd.begin(20,4); // set up the LCD's number of columns and rows:
       //premiere requete ntp    
   lcd.print("Init NTP");
   while(timeStatus()== timeNotSet)
     setSyncProvider(getNtpTime); // wait until the time is set by the sync provider  


void loop()

 // gestion time NTP ///////////////////////////
 if( now() != prevDisplay) //update the display only if the time has changed
   prevDisplay = now();
     //cycle 1Sec //////////////////////////////////////:
   delay(1000); // maj toutes les sec

void digitalClockDisplay(){
   // affichage sur aff lcd
   lcd.setCursor(0, 0); // ligne 1 date
   lcd.print(" ");
   if (month()<10)
   lcd.print(" ");
   lcd.print(" - ");
   if (minute()<10)
   lcd.print(" ");


/*-------- NTP code ----------*/

unsigned long getNtpTime()
 if ( UdpBytewise.available() ) {
   for(int i=0; i < 40; i++)
      UdpBytewise.read(); // ignore every field except the time
   const unsigned long seventy_years = 2208988800UL + timeZoneOffset;        
   return getUlong() -  seventy_years;    
 return 0; // return 0 if unable to get the time

unsigned long sendNTPpacket(byte *address)
 UdpBytewise.beginPacket(address, 123);
 UdpBytewise.write(B11100011);   // LI, Version, Mode
 UdpBytewise.write(0);    // Stratum
 UdpBytewise.write(6);  // Polling Interval
 UdpBytewise.write(0xEC); // Peer Clock Precision
 write_n(0, 8);    // Root Delay & Root Dispersion
 write_n(0, 32); //Reference and time stamps  

unsigned long getUlong()
   unsigned long ulong = (unsigned long)UdpBytewise.read() << 24;
   ulong |= (unsigned long)UdpBytewise.read() << 16;
   ulong |= (unsigned long)UdpBytewise.read() << 8;
   ulong |= (unsigned long)UdpBytewise.read();
   return ulong;

void write_n(int what, int how_many)
 for( int i = 0; i < how_many; i++ )



Thank you guys!
Its very true about the hammering the ntp servers and my purpose would be using the time library and set the time once every 24 hours or so.

I have gone through the TimeNtp example that comes with arduino 0021 and i cant get it to compile.
It seems that they did not have the UdpBytewise library with the release and when i try to install it on my own it doesnt work.

compiler halts on the codeline:
  if ( UdpBytewise.available() ) {
line 83 in the example project.

Any clue what i can do to get the library working ?

Ran Talbott

Another alternative: I have a weather station that posts readings to a PHP server by doing (roughly) "GET base_URL?time=ticks&temp=degrees&wind=mph", where "ticks" is the Unix-style timestamp generated by the DateTime library.  The server sends back a response with its own Unix timestamp, which the Arduino uses to keep its internal clock in sync with the server's.

It eliminates the need for the Arduino to sync up with an NTP server,  and avoids errors if there are delays due to network latency or other delays. At one point, I was planning to have the Arduino buffer readings in case the wireless network link hiccuped, but we decided it wasn't worth the trouble, given the low frequency of outages and the limited buffer space available.


Thank you once again!


the code there from mem on reply 5 worked when i installed the time library and NOT the bjørn hartmann library on arduino 0022.
Hope that helps anyone else trying in the future.
Make sure you keep the ethernet library clean and don't install anything else in there.
if you don't get it working. reinstall latest arduino release and just install time library and compile the code. and it will work.


so this came to use pretty soon :)
used in my valentines day gift.


Hi, I compiled the code that I found in the link posted by Vegard Paulsen in reply 8:

http://arduino.cc/forum/index.php/topic,40254.msg294710.html#msg294710   (code in reply 5)

And I had no errors during the compiling process but the serial monitor just shows ' waiting for sync'. It never synchronizes. I'm in Colombia, I don't know if  it is a problem to the NTP server.

I'm using Arduino 0019 and this board :



The ip is no longer valid as a NTP server. It no longer responds.

You have two options.
1) Use nslookup to resolve pool.ntp.org to get a valid NTP server
or what I recommend
2) use the ethernet shield dns to resolve pool.ntp.org in setup

Go Up