TimeNTP
From: https://github.com/thijse/Arduino-Code-and-Libraries/blob/master/Libraries/Time/Examples/TimeNTP/TimeNTP.pde
/*H*******************************************************
* 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>
#include <UdpBytewise.h> // UDP lib from: bjoern@cs.stanford.edu 12/30/2008
#if UDP_TX_PACKET_MAX_SIZE <64 || UDP_RX_PACKET_MAX_SIZE < 64
#error : UDP packet size to small - modify UdpBytewise.h to set buffers to 64 bytes
#endif
//************************* DEFINES ************************************
#define BAUD 9600
typedef unsigned long ulong;
//************************* PROTOTYPES ************************************
void digitalClockDisplay();
ulong getNtpTime();
ulong sendNTPpacket( byte *address );
ulong getUlong();
void write_n( int what, int how_many );
//************************* VARIABLES ************************************
/*H*******************************************************
* YOU MAY NEED TO MODIFY UdpBytewise.h LIBRARY TO ALLOW ENOUGH SPACE IN BUFFERS FOR NTP PACKETS
* Open up UdpBytewse.h and set following buffers to 64 bytes:
* #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, 1, 44 }; // set the IP address to an unused address on your network
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 DIGITAL CLOCK WAS DISPLAYED
const long timeZoneOffset = 0L; // SET OFFSET IN SECONDS TO YOUR LOCAL TIME;
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( 9600 );
Ethernet.begin( mac,ip );
Serial.println( "waiting for sync" );
setSyncProvider( getNtpTime );
while( timeStatus()== timeNotSet )
; // WAIT UNTIL TIME IS SET BY SYNC PROVIDER
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
if( now() != prevDisplay ) // UPDATE DISPLAY ONLY IF TIME HAS CHANGED
{
prevDisplay = now();
digitalClockDisplay();
}
}
/*F********************************************************************
* DIGITAL CLOCK DISPLAY OF TIME
**********************************************************************/
void
digitalClockDisplay()
{
Serial.print( hour() );
printDigits( minute() );
printDigits( second() );
Serial.print( " " );
Serial.print( day() );
Serial.print( " " );
Serial.print( month() );
Serial.print( " " );
Serial.print( year() );
Serial.println();
}
/*F********************************************************************
* DIGITAL CLOCK DISPLAY: PRINTS PRECEDING COLON AND LEADING 0
**********************************************************************/
void
printDigits( int digits )
{
Serial.print( ":" );
if( digits < 10 )
Serial.print( '0' );
Serial.print( digits );
}
/*F********************************************************************
* NTP code
**********************************************************************/
ulong
getNtpTime()
{
sendNTPpacket( SNTP_server_IP );
delay( 1000 );
if( UdpBytewise.available() )
{
for( int i =0; i < 40; i++ )
UdpBytewise.read(); // IGNORE EVERY FIELD EXCEPT TIME
const ulong seventy_years = 2208988800UL + timeZoneOffset;
return( getUlong() - seventy_years );
}
return( 0 ); // RETURN 0 IF UNABLE TO GET TIME
}
/*F********************************************************************
*
**********************************************************************/
ulong
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();
}
/*F********************************************************************
*
**********************************************************************/
ulong
getUlong()
{
ulong ulong = (ulong)UdpBytewise.read() << 24;
ulong |= (ulong)UdpBytewise.read() << 16;
ulong |= (ulong)UdpBytewise.read() << 8;
ulong |= (ulong)UdpBytewise.read();
return( ulong );
}
/*F********************************************************************
*
**********************************************************************/
void
write_n( int what, int how_many )
{
for( int i = 0; i < how_many; i++ )
UdpBytewise.write( what );
}