9.1 Holen der Zeit vom Internet und interne Uhr setzen

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

Fork of 09-02-Uebung by th.iotkit.ch

Revision:
2:c56233cb8520
Parent:
1:731bf468ab9f
Child:
3:ca2a69bdba22
diff -r 731bf468ab9f -r c56233cb8520 main.cpp
--- a/main.cpp	Sun Feb 22 14:02:37 2015 +0000
+++ b/main.cpp	Sun Feb 22 14:15:58 2015 +0000
@@ -1,28 +1,34 @@
-/** 8.2 Setzen der Zeit, mittels der Seriellen Schnittstelle und Ausgabe Stunden und Minuten auf dem Display.
- * Beispiel von http://developer.mbed.org/blog/entry/103/
+/** 9.2 Holen der Zeit vom Internet, interne Uhr setzen und Ausgabe auf Display
  * Informationen um Zeit zu holen von http://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c
 */
 #include "mbed.h"
-
+#include "EthernetInterface.h"
+#include "NTPClient.h"
 #include "DigitDisplay.h"
 
+EthernetInterface eth;
+NTPClient ntp;
 DigitDisplay display(PTC5, PTC7);
 
 int main()
 {
-    // get the current time from the terminal
-    struct tm t;
-    printf("Enter current date and time:\n");
-    printf("YYYY MM DD HH MM SS[enter]\n");
-    scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
-          , &t.tm_hour, &t.tm_min, &t.tm_sec);
+    // Ethernet Interface Initialisieren
+    printf("Initialize Ethernet\n" );
+    eth.init();
+    eth.connect();
 
-    // adjust for tm structure required values
-    t.tm_year = t.tm_year - 1900;
-    t.tm_mon = t.tm_mon - 1;
-
-    // set the time
-    set_time( mktime(&t) );
+    // Zeit vom Time Server holen
+    printf("Trying to update time...\r\n");
+    if (ntp.setTime("1.pool.ntp.org") == 0) 
+    {
+        printf("Set time successfully\r\n");
+        time_t ctTime;
+        ctTime = time(NULL);
+        printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
+    } 
+    else
+        printf("Error\r\n");
+    eth.disconnect();
 
     // display the time
     while(1)