Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface SimpleSocket mbed-rtos mbed
ntpclient.cpp
00001 #include "SimpleSocket.h" 00002 00003 void ntpclient() { 00004 char *NTP_SERVER = "pool.ntp.org"; 00005 printf("ntp server = %s\n", NTP_SERVER); 00006 00007 while (true) { 00008 DatagramSocket datagram; 00009 char buf[48] = {0x23}; // 00100011 LI(0), Version(4), Mode(3: Client) 00010 datagram.write(buf, sizeof(buf)); 00011 datagram.send(NTP_SERVER, 123); 00012 00013 //datagram.setTimeout(5.0); 00014 if (datagram.receive() > 0) { 00015 if (datagram.read(buf, sizeof(buf)) > 0) { 00016 unsigned long seconds = 0; 00017 for (int i = 40; i <= 43; i++) 00018 seconds = (seconds << 8) | buf[i]; 00019 set_time(time_t(seconds - 2208988800ULL)); 00020 char timestamp[16]; 00021 time_t jstime = time(NULL) + 9 * 3600; 00022 strftime(timestamp, sizeof(timestamp), "%m/%d %X", localtime(&jstime)); 00023 printf("Time: %s\n", timestamp); 00024 break; 00025 } 00026 } else { 00027 printf("no answer\n"); 00028 wait(1.0); 00029 } 00030 } 00031 }
Generated on Mon Jul 18 2022 00:17:10 by
1.7.2