9.3 Licht zeitgesteuert Ein- und Ausschalten, z.B. h:m:45 Ein, h:m:50 Aus.

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed

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

Committer:
stefan1691
Date:
Sun Feb 22 14:19:22 2015 +0000
Revision:
3:ca2a69bdba22
Parent:
2:c56233cb8520
Child:
4:35afb6a34591
9.1 Holen der Zeit vom Internet und interne Uhr setzen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stefan1691 3:ca2a69bdba22 1 /** 9.1 Holen der Zeit vom Internet und interne Uhr setzen
stefan1691 1:731bf468ab9f 2 * Informationen um Zeit zu holen von http://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c
stefan1691 0:8107357917ce 3 */
stefan1691 0:8107357917ce 4 #include "mbed.h"
stefan1691 2:c56233cb8520 5 #include "EthernetInterface.h"
stefan1691 2:c56233cb8520 6 #include "NTPClient.h"
stefan1691 1:731bf468ab9f 7
stefan1691 2:c56233cb8520 8 EthernetInterface eth;
stefan1691 2:c56233cb8520 9 NTPClient ntp;
stefan1691 1:731bf468ab9f 10
stefan1691 0:8107357917ce 11 int main()
stefan1691 0:8107357917ce 12 {
stefan1691 2:c56233cb8520 13 // Ethernet Interface Initialisieren
stefan1691 2:c56233cb8520 14 printf("Initialize Ethernet\n" );
stefan1691 2:c56233cb8520 15 eth.init();
stefan1691 2:c56233cb8520 16 eth.connect();
stefan1691 0:8107357917ce 17
stefan1691 2:c56233cb8520 18 // Zeit vom Time Server holen
stefan1691 2:c56233cb8520 19 printf("Trying to update time...\r\n");
stefan1691 2:c56233cb8520 20 if (ntp.setTime("1.pool.ntp.org") == 0)
stefan1691 2:c56233cb8520 21 {
stefan1691 2:c56233cb8520 22 printf("Set time successfully\r\n");
stefan1691 2:c56233cb8520 23 time_t ctTime;
stefan1691 2:c56233cb8520 24 ctTime = time(NULL);
stefan1691 2:c56233cb8520 25 printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
stefan1691 2:c56233cb8520 26 }
stefan1691 2:c56233cb8520 27 else
stefan1691 2:c56233cb8520 28 printf("Error\r\n");
stefan1691 2:c56233cb8520 29 eth.disconnect();
stefan1691 0:8107357917ce 30
stefan1691 0:8107357917ce 31 // display the time
stefan1691 0:8107357917ce 32 while(1)
stefan1691 0:8107357917ce 33 {
stefan1691 0:8107357917ce 34 time_t seconds = time(NULL);
stefan1691 1:731bf468ab9f 35 struct tm * now = localtime( & seconds );
stefan1691 1:731bf468ab9f 36 printf( "%d.%d.%d %2d:%2d:%2d\n", now->tm_mday, now->tm_mon + 1, now->tm_year + 1900, now->tm_hour, now->tm_min, now->tm_sec );
stefan1691 0:8107357917ce 37 wait(1);
stefan1691 0:8107357917ce 38 }
stefan1691 0:8107357917ce 39 }