@PaulS
thanks for your hint! Now I got it.
But there is another problem where I don't have a clue, what goes wrong, as I just use the code that is used in 99% of examples with minimal modification.
It seems to me, that there is no response from the time server, but I can't believe that.
Before I implemented the dns code for the usage of NTP pools, I used fixed IP's for the time server. Then the effect was, that I found one NTP server where I got the time always immediately. Nearly all other server IP's I used, led to the same situation as I have it now with 99% of the IP's from the pool: the program will stuck in the endless loop in the setup() :
lcd.print("NTP sync ... ");
setSyncProvider(getNtpTime);
while(timeStatus()== timeNotSet)
; // wait until the time is set by the sync provider
The complete code is
// NTP time
// IDE 1.0.3 Win7/64
// DHCP Server required
// LCD connected for debugging
#include <MsTimer2.h>
#include <Time.h>
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Dns.h>
// LCD
#define LCD_RS 15
#define LCD_E 16
#define LCD_D4 21
#define LCD_D5 20
#define LCD_D6 19
#define LCD_D7 18
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
// Ethernet library configuration
byte mac[] = { 0x24, 0x7B, 0xA2, 0x4A, 0x52, 0x10 };
IPAddress timeServer(0, 0, 0, 0);
/* ALTER THESE VARIABLES AT YOUR OWN RISK */
// local port to listen for UDP packets
unsigned int localPort = 75;
// NTP time stamp is in the first 48 bytes of the message
const int NTP_PACKET_SIZE= 48;
// Buffer to hold incoming and outgoing packets
byte packetBuffer[NTP_PACKET_SIZE];
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
boolean s_flag = false;
void setup() {
lcd.begin(20, 4);
// Setup Ethernet by DHCP
lcd.setCursor(0,0);
lcd.print("Wait for DHCP Server");
if (Ethernet.begin(mac) == 0) { // start Ethernet by DHCP conf.
lcd.setCursor(0,0);
lcd.print("DHCP config failed! ");
}
DNSClient dns;
dns.begin(Ethernet.dnsServerIP());
dns.getHostByName("pool.ntp.org",timeServer); // at this point the function works
lcd.setCursor(0,1); //col,row
lcd.print("NTP: ");
lcd.print(timeServer);
Udp.begin(localPort);
lcd.setCursor(0,0);
lcd.print("NTP sync ... ");
setSyncProvider(getNtpTime);
while(timeStatus()== timeNotSet)
; // wait until the time is set by the sync provider
setSyncInterval(86400); // Set seconds between re-sync 86400 = 24hr
//setSyncInterval(120);
MsTimer2::set(1000, update1s); // 1s period
MsTimer2::start();
}
void loop() {
if (s_flag){
s_flag=false;
digitalClockDisplay();
ntpSync();
}
}
void update1s(){
// das Flag zeigt an, ob 1s um ist
s_flag=true;
}
void ntpSync(){
if (((minute() % 3)==0) && (second()==0)){
// dns.getHostByName("pool.ntp.org",timeServer); //at this point the function causes a Compiler error
setSyncProvider(getNtpTime);
}
}
void digitalClockDisplay(){
// digital clock display of the time
lcd.setCursor(0,0); //col,row
lcd.print(day());
lcd.print(".");
lcd.print(month());
lcd.print(".");
lcd.print(year()-2000);
lcd.print(" ");
lcd.print(hour());
printDigits(minute());
if(timeStatus()!= timeNeedsSync) // if ntp sync lost, don't show the seconds to indicate this
printDigits(second());
else
lcd.print(" ");
lcd.print(" ");
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding
// colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
/*-------- NTP code ----------*/
unsigned long getNtpTime()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
delay(500);
if ( Udp.parsePacket() ) {
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read packet into buffer
//the timestamp starts at byte 40, convert four bytes into a long integer
unsigned long hi = word(packetBuffer[40], packetBuffer[41]);
unsigned long low = word(packetBuffer[42], packetBuffer[43]);
// this is NTP time (seconds since Jan 1 1900
unsigned long secsSince1900 = hi << 16 | low;
// Unix time starts on Jan 1 1970
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch = secsSince1900 - seventyYears + adjustDstEurope(); // subtract 70 years, add Daylight Saving and Timezone adjust
return epoch;
}
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
//unsigned long sendNTPpacket(IPAddress address)
void sendNTPpacket(IPAddress address)
{
memset(packetBuffer, 0, NTP_PACKET_SIZE); // set all bytes in the buffer to 0
// Initialize values needed to form NTP request
packetBuffer[0] = B11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum
packetBuffer[2] = 6; // Max Interval between messages in seconds
packetBuffer[3] = 0xEC; // Clock Precision
// bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
packetBuffer[12] = 49; // four-byte reference ID identifying
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// send the packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
}
int adjustDstEurope()
{
// last sunday of march
int beginDSTDate= (31 - (5* year() /4 + 4) % 7);
//Serial.println(beginDSTDate);
int beginDSTMonth=3;
//last sunday of october
int endDSTDate= (31 - (5 * year() /4 + 1) % 7);
//Serial.println(endDSTDate);
int endDSTMonth=10;
// DST is valid as:
if (((month() > beginDSTMonth) && (month() < endDSTMonth))
|| ((month() == beginDSTMonth) && (day() >= beginDSTDate))
|| ((month() == endDSTMonth) && (day() <= endDSTDate)))
return 7200; // DST europe = utc +2 hour
else return 3600; // nonDST europe = utc +1 hour
}