get internet time on k64f

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Fork of Internet_LCD_Clock by jim hamblen

Revision:
0:5c5226aac712
Child:
1:09fcc9b81f23
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Internet_LCD_Clock.cpp	Thu Apr 12 01:44:02 2012 +0000
@@ -0,0 +1,44 @@
+#include "mbed.h"
+#include "EthernetNetIf.h"
+#include "NTPClient.h"
+#include "TextLCD.h"
+// Internet of Things clock example: LCD time is set via internet NTP time server
+TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d0-d3
+EthernetNetIf eth;
+NTPClient ntp;
+
+int main() {
+//system time structure
+    time_t ctTime;
+    //clear LCD
+    lcd.cls();
+    // lcd.printf prints to LCD display;
+    lcd.printf("Get IP addr...");
+    EthernetErr ethErr = eth.setup();
+    //Get an Internet IP address using DHCP
+    if (ethErr) {
+        //error or timeout getting an IP address
+        printf("Error %d in setup.\n\r", ethErr);
+        lcd.cls();
+        lcd.printf("Network Error \n\r %d",ethErr);
+        return -1;
+    }
+    lcd.cls();
+    ctTime = time(NULL);
+    lcd.printf("Reading Time...\n\r");
+    //specify time server URL
+    Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
+    //Read time from server
+    ntp.setTime(server);
+    ctTime = time(NULL);
+    lcd.printf("Time set");
+    //Delay for human time to read LCD display
+    wait(1);
+    while (1) {
+        // loop and periodically update the LCD's time display
+        lcd.cls();
+        ctTime = time(NULL);
+        lcd.printf("UTC:  %s", ctime(&ctTime));
+        wait(.25);
+    }
+}