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
main.cpp@2:c56233cb8520, 2015-02-22 (annotated)
- Committer:
- stefan1691
- Date:
- Sun Feb 22 14:15:58 2015 +0000
- Revision:
- 2:c56233cb8520
- Parent:
- 1:731bf468ab9f
- Child:
- 3:ca2a69bdba22
9.2 Holen der Zeit vom Internet, interne Uhr setzen und Ausgabe auf Display
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefan1691 | 2:c56233cb8520 | 1 | /** 9.2 Holen der Zeit vom Internet, interne Uhr setzen und Ausgabe auf Display |
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 | #include "DigitDisplay.h" |
stefan1691 | 1:731bf468ab9f | 8 | |
stefan1691 | 2:c56233cb8520 | 9 | EthernetInterface eth; |
stefan1691 | 2:c56233cb8520 | 10 | NTPClient ntp; |
stefan1691 | 1:731bf468ab9f | 11 | DigitDisplay display(PTC5, PTC7); |
stefan1691 | 1:731bf468ab9f | 12 | |
stefan1691 | 0:8107357917ce | 13 | int main() |
stefan1691 | 0:8107357917ce | 14 | { |
stefan1691 | 2:c56233cb8520 | 15 | // Ethernet Interface Initialisieren |
stefan1691 | 2:c56233cb8520 | 16 | printf("Initialize Ethernet\n" ); |
stefan1691 | 2:c56233cb8520 | 17 | eth.init(); |
stefan1691 | 2:c56233cb8520 | 18 | eth.connect(); |
stefan1691 | 0:8107357917ce | 19 | |
stefan1691 | 2:c56233cb8520 | 20 | // Zeit vom Time Server holen |
stefan1691 | 2:c56233cb8520 | 21 | printf("Trying to update time...\r\n"); |
stefan1691 | 2:c56233cb8520 | 22 | if (ntp.setTime("1.pool.ntp.org") == 0) |
stefan1691 | 2:c56233cb8520 | 23 | { |
stefan1691 | 2:c56233cb8520 | 24 | printf("Set time successfully\r\n"); |
stefan1691 | 2:c56233cb8520 | 25 | time_t ctTime; |
stefan1691 | 2:c56233cb8520 | 26 | ctTime = time(NULL); |
stefan1691 | 2:c56233cb8520 | 27 | printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); |
stefan1691 | 2:c56233cb8520 | 28 | } |
stefan1691 | 2:c56233cb8520 | 29 | else |
stefan1691 | 2:c56233cb8520 | 30 | printf("Error\r\n"); |
stefan1691 | 2:c56233cb8520 | 31 | eth.disconnect(); |
stefan1691 | 0:8107357917ce | 32 | |
stefan1691 | 0:8107357917ce | 33 | // display the time |
stefan1691 | 0:8107357917ce | 34 | while(1) |
stefan1691 | 0:8107357917ce | 35 | { |
stefan1691 | 0:8107357917ce | 36 | time_t seconds = time(NULL); |
stefan1691 | 1:731bf468ab9f | 37 | struct tm * now = localtime( & seconds ); |
stefan1691 | 1:731bf468ab9f | 38 | 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 | 1:731bf468ab9f | 39 | |
stefan1691 | 1:731bf468ab9f | 40 | display.write(0, now->tm_min / 10); |
stefan1691 | 1:731bf468ab9f | 41 | display.write(1, now->tm_min % 10); |
stefan1691 | 1:731bf468ab9f | 42 | display.write(2, now->tm_sec / 10); |
stefan1691 | 1:731bf468ab9f | 43 | display.write(3, now->tm_sec % 10); |
stefan1691 | 1:731bf468ab9f | 44 | |
stefan1691 | 0:8107357917ce | 45 | wait(1); |
stefan1691 | 0:8107357917ce | 46 | } |
stefan1691 | 0:8107357917ce | 47 | } |