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: EthernetNetIf SimpleSocket 1.0 mbed
Diff: ntpclient.cpp
- Revision:
- 39:108499af2b53
diff -r 000000000000 -r 108499af2b53 ntpclient.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ntpclient.cpp Wed Jun 06 03:34:16 2012 +0000
@@ -0,0 +1,34 @@
+#include "EthernetNetIf.h"
+#include "SimpleSocket.h"
+
+void ntpclient() {
+ EthernetNetIf eth;
+ eth.setup();
+
+ char *NTP_SERVER = "pool.ntp.org";
+ printf("ntp server = %s\n", NTP_SERVER);
+
+ while (true) {
+ DatagramSocket datagram;
+ char buf[48] = {0x23}; // 00100011 LI(0), Version(4), Mode(3: Client)
+ datagram.write(buf, sizeof(buf));
+ datagram.send(NTP_SERVER, 123);
+
+ if (datagram.receive() > 0) {
+ if (datagram.read(buf, sizeof(buf)) > 0) {
+ unsigned long seconds = 0;
+ for (int i = 40; i <= 43; i++)
+ seconds = (seconds << 8) | buf[i];
+ set_time(time_t(seconds - 2208988800ULL));
+ char timestamp[16];
+ time_t jstime = time(NULL) + 9 * 3600;
+ strftime(timestamp, sizeof(timestamp), "%m/%d %X", localtime(&jstime));
+ printf("Time: %s\n", timestamp);
+ break;
+ }
+ } else {
+ printf("no answer\n");
+ wait(1.0);
+ }
+ }
+}
\ No newline at end of file