Go Down

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

subsoniq

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 :)


robtillaart


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)

forex2

you have ethernet examples
it contains udpntpclient which you should try

nickgammon

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

forex2

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 132.246.11.231 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:
   delay(200);
       //ethernet
   Ethernet.begin(mac,ip,gateway,mask);  
   delay(500);  
   
       //premiere requete ntp    
   lcd.setCursor(0,0);
   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();
   digitalClockDisplay();  
 }
 
     //cycle 1Sec //////////////////////////////////////:
   delay(1000); // maj toutes les sec
}

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

}

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

unsigned long getNtpTime()
{
 sendNTPpacket(SNTP_server_IP);
 delay(1000);
 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.begin(123);
 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
 UdpBytewise.write(49);
 UdpBytewise.write(0x4E);
 UdpBytewise.write(49);
 UdpBytewise.write(52);
 write_n(0, 32); //Reference and time stamps  
 UdpBytewise.endPacket();  
}

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++ )
   UdpBytewise.write(what);
}



 

subsoniq

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.

subsoniq

Thank you once again!

http://arduino.cc/forum/index.php/topic,40254.msg294710.html#msg294710

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.

subsoniq

so this came to use pretty soon :)
http://vegardpaulsen.wordpress.com/Valentineduino/
used in my valentines day gift.


emmanaty

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 :

http://www.inmojo.com/store/inmojo-market/item/inmojo-xbee-ethernet-arduino-server/

SurferTim

The ip 192.43.244.18 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