9.2 Holen der Zeit vom Internet, interne Uhr setzen und Ausgabe auf Display

Dependencies:   DigitDisplay EthernetInterface mbed-rtos mbed

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

Files at this revision

API Documentation at this revision

Comitter:
stefan1691
Date:
Sun Feb 22 14:15:58 2015 +0000
Parent:
1:731bf468ab9f
Commit message:
9.2 Holen der Zeit vom Internet, interne Uhr setzen und Ausgabe auf Display

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Sun Feb 22 14:15:58 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/EthernetInterface/#65b0d840274c
--- 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) 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sun Feb 22 14:15:58 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#83e169389a69