/* * Connect any NTP-Pool server to query the current time. */ static void QueryDateAndTime(void) { uint32_t ip; time_t now; int i; /* First connect the DNS server to get the IP address. */ printf("Is There Anybody Out There? "); ip = NutDnsGetHostByName((uint8_t *) "pool.ntp.org"); if (ip == 0) { puts("No?"); } else { /* We do have an IP, now try to connect it. */ printf("Yes, found %s\n", inet_ntoa(ip)); for (i = 0; i < 3; i++) { printf("What's the time? "); if (NutSNTPGetTime(&ip, &now) == 0) { /* We got a valid response, display the result. */ printf("%s GMT\n", Rfc1123TimeString(gmtime(&now))); return; } else { /* It failed. May be the server is too busy. Try again in 5 seconds. */ puts("Sorry?"); NutSleep(5000); } } } /* We give up after 3 trials. */ puts("You missed the starting gun."); }
//Haal tijd op via ntp tm getNTPTime(){ time_t ntp_time = 0; tm *ntp_datetime; uint32_t timeserver = 0; _timezone = -1 * 60 * 60; //GMT+1 LogMsg_P(LOG_INFO, PSTR("Retrieving time from 78.192.65.63...")); timeserver = inet_addr("78.192.65.63"); int i; for (i = 0;i < 20; i++) { if (NutSNTPGetTime(×erver, &ntp_time) == 0) { break; } else { NutSleep(1000); LogMsg_P(LOG_INFO, PSTR("Fout bij ophalen ntp tijd, pogingen over:%d"),(20-i)); } } ntp_datetime = localtime(&ntp_time); LogMsg_P(LOG_INFO, PSTR("NTP time is: %02d:%02d:%02d\n"), ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec); //printf("NTP time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec); tm gmt; gmt.tm_hour = ntp_datetime->tm_hour; gmt.tm_min = ntp_datetime->tm_min; gmt.tm_sec = ntp_datetime->tm_sec; gmt.tm_mday = ntp_datetime->tm_mday; gmt.tm_mon = ntp_datetime->tm_mon; gmt.tm_year = ntp_datetime->tm_year; return gmt; }
void RetrieveTime() { tm test; for (;;) { if (NutSNTPGetTime(×erver, &ntp_time) == 0) { break; } else { NutSleep(200); puts("Time Error"); } } ntp_datetime = localtime(&ntp_time); tm RTCdate={ntp_datetime->tm_sec, ntp_datetime->tm_min, ntp_datetime->tm_hour, ntp_datetime->tm_mday, ntp_datetime->tm_mon, ntp_datetime->tm_year,1,1,1}; setDateTime(&RTCdate); isSynchronizedTimer = 60; getDateTime(&test); printf(" RTC time check = %d : %d : %d \n ", test.tm_hour, test.tm_min, test.tm_sec); }
int NtpSync(void){ /* Ophalen van pool.ntp.org */ isSyncing = true; setTimeZone(50); httpGet("/gettimezone.php", parsetimezone); _daylight = 0; printf("Timezone is: %d", TIME_ZONE); if(TIME_ZONE == 50) { return 0; } NutDelay(100); //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)"); timeserver = inet_addr("213.154.229.24"); for (;;) { if (NutSNTPGetTime(×erver, &ntp_time) == 0) { break; } else { NutSleep(400); puts("Fout bij het ontvangen van de tijd"); } } //puts("Opgehaald.\n"); ntp_datetime = localtime(&ntp_time); printf("NTP time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec); printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900)); X12RtcSetClock(ntp_datetime); NtpWriteTimeToEeprom(*ntp_datetime); isSyncing = false; validTime = true; return 1; }