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-03-Uebung by th.iotkit.ch

Committer:
stefan1691
Date:
Sun Feb 22 14:30:18 2015 +0000
Revision:
4:35afb6a34591
Parent:
3:ca2a69bdba22
9.3 Licht zeitgesteuert Ein- und Ausschalten, z.B. h:m:45 Ein, h:m:50 Aus.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stefan1691 4:35afb6a34591 1 /** 9.3 Licht zeitgesteuert Ein- und Ausschalten, z.B. h:m:45 Ein, h:m:50 Aus.
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 4:35afb6a34591 11 // Licht
stefan1691 4:35afb6a34591 12 DigitalOut led( D10 );
stefan1691 4:35afb6a34591 13
stefan1691 0:8107357917ce 14 int main()
stefan1691 0:8107357917ce 15 {
stefan1691 2:c56233cb8520 16 // Ethernet Interface Initialisieren
stefan1691 2:c56233cb8520 17 printf("Initialize Ethernet\n" );
stefan1691 2:c56233cb8520 18 eth.init();
stefan1691 2:c56233cb8520 19 eth.connect();
stefan1691 0:8107357917ce 20
stefan1691 2:c56233cb8520 21 // Zeit vom Time Server holen
stefan1691 2:c56233cb8520 22 printf("Trying to update time...\r\n");
stefan1691 2:c56233cb8520 23 if (ntp.setTime("1.pool.ntp.org") == 0)
stefan1691 2:c56233cb8520 24 {
stefan1691 2:c56233cb8520 25 printf("Set time successfully\r\n");
stefan1691 2:c56233cb8520 26 time_t ctTime;
stefan1691 2:c56233cb8520 27 ctTime = time(NULL);
stefan1691 2:c56233cb8520 28 printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
stefan1691 2:c56233cb8520 29 }
stefan1691 2:c56233cb8520 30 else
stefan1691 2:c56233cb8520 31 printf("Error\r\n");
stefan1691 2:c56233cb8520 32 eth.disconnect();
stefan1691 0:8107357917ce 33
stefan1691 0:8107357917ce 34 // display the time
stefan1691 0:8107357917ce 35 while(1)
stefan1691 0:8107357917ce 36 {
stefan1691 0:8107357917ce 37 time_t seconds = time(NULL);
stefan1691 1:731bf468ab9f 38 struct tm * now = localtime( & seconds );
stefan1691 1:731bf468ab9f 39 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 4:35afb6a34591 40
stefan1691 4:35afb6a34591 41 // h:m:15 Ein, h:m:20 Aus.
stefan1691 4:35afb6a34591 42 if ( now->tm_sec >= 15 && now->tm_sec <= 20 )
stefan1691 4:35afb6a34591 43 led = 1;
stefan1691 4:35afb6a34591 44 // h:m:45 Ein, h:m:50 Aus.
stefan1691 4:35afb6a34591 45 else if ( now->tm_sec >= 45 && now->tm_sec <= 50 )
stefan1691 4:35afb6a34591 46 led = 1;
stefan1691 4:35afb6a34591 47 else
stefan1691 4:35afb6a34591 48 led = 0;
stefan1691 4:35afb6a34591 49
stefan1691 0:8107357917ce 50 wait(1);
stefan1691 0:8107357917ce 51 }
stefan1691 0:8107357917ce 52 }