I had some problems to get "Time_SNP.pde" up and running. Main reason was wrong buffer length in UdpEtherne.h. So i introduced some changes to "Time_NTP.pde" code to ease beginners to bring it to operation.
1) Added some comments how to change UdpEthernet.h buffer size
2) removed gateway adress, this would normaly not be neccesary.
3) renamed Server-IP Variable to "SNTP_server_IP[]" for better international reading.
4) introduced some more time servers as a comment to easy try some other servers.
5) added a const unsigned long for time zone correction, which ist used in unsigned long getNtpTime() function. This could also be done by definig 24 different like e.g.
const unsigned long seventy_years_CET = 22089XXXXUL;
// const unsigned long seventy_years_UTC = 2208988800UL;
// const unsigned long seventy_years_ ____ and so on ____
Which version would you or a beginner prefer for time zone correction ? i can provide the changes if someone wants me to do

Hi mem,
please review my changes and introduce them to your library.
Here is my code :
/*
* Time_NTP.pde
* Example showing time sync to NTP time source
*
* This sketch uses the Ethenet library with the user contributed UdpBytewise extension
*/
#include <Time.h>
#include <Ethernet.h>
// the follwing library should be copied to the Ethernet directory
#include <UdpBytewise.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// You will also need to modify the UdpBytewise.h library to allow enough space in the buffers for the NTP packets.
// Open up UdpBytewse.h and change these lines:
//
// #define UDP_TX_PACKET_MAX_SIZE 32
// #define UDP_RX_PACKET_MAX_SIZE 32
//
// to this:
//
// #define UDP_TX_PACKET_MAX_SIZE 64
// #define UDP_RX_PACKET_MAX_SIZE 64
//
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168, 178, 100 }; // fill in your local Arduino ip adress
// byte SNTP_server_IP[] = { 192, 43, 244, 18}; // time.nist.gov
// byte SNTP_server_IP[] = { 130,149,17,21}; // ntps1-0.cs.tu-berlin.de
byte SNTP_server_IP[] = {
192,53,103,108}; // ptbtime1.ptb.de
time_t prevDisplay = 0; // when the digital clock was displayed
// time zone correction [sec]. e.g. CET = -3600 sec
const unsigned long add_time_zone=-3600UL;
void setup()
{
Ethernet.begin(mac,ip);
Serial.begin(19200);
Serial.println ("waiting for NTP server ...");
setSyncProvider(getNtpTime);
while(timeStatus()== timeNotSet)
; // wait until the time is set by the sync provider
}
void loop()
{
if( now() != prevDisplay) //update the display only if the time has changed
{
prevDisplay = now();
digitalClockDisplay();
}
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" sec ");
Serial.print(day());
Serial.print(".");
Serial.print(month());
Serial.print(".");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/*-------- NTP code ----------*/
unsigned long getNtpTime()
{
sendNTPpacket(SNTP_server_IP);
delay(200); // for roundtriptiime in slow environtment
if ( UdpBytewise.available() ) {
for(int i=0; i < 40; i++){
UdpBytewise.read(); // ignore every field except the time
}
const unsigned long seventy_years = 2208988800UL+add_time_zone;
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(byte what, int how_many)
{
for( int i = 0; i < how_many; i++ )
UdpBytewise.write(what);
}
Happy playing with Arduino !
Ditmar