Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface NTPClient mbed-rtos mbed
Fork of MbedClock by
main.cpp@12:bcced6833b8f, 2016-02-17 (annotated)
- Committer:
- ismaia
- Date:
- Wed Feb 17 16:03:09 2016 +0000
- Revision:
- 12:bcced6833b8f
- Parent:
- 11:4d4334d909d3
Reading and showing RTC time
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudanian | 0:4e6ae21cbd31 | 1 | #include "mbed.h" |
dudanian | 0:4e6ae21cbd31 | 2 | #include "rtos.h" |
dudanian | 0:4e6ae21cbd31 | 3 | #include "EthernetInterface.h" |
ismaia | 7:0a56268d858c | 4 | #include "NTPClient.h" |
dudanian | 0:4e6ae21cbd31 | 5 | #include <string> |
dudanian | 1:c47a2f0816bb | 6 | |
dudanian | 0:4e6ae21cbd31 | 7 | |
ismaia | 8:9693d69ec483 | 8 | EthernetInterface eth; |
dudanian | 2:c939d0501184 | 9 | TCPSocketConnection server; |
ismaia | 8:9693d69ec483 | 10 | NTPClient ntp; |
ismaia | 11:4d4334d909d3 | 11 | DigitalOut led1(LED1); |
ismaia | 11:4d4334d909d3 | 12 | DigitalOut led2(LED2); |
ismaia | 12:bcced6833b8f | 13 | time_t rtcTime; |
ismaia | 12:bcced6833b8f | 14 | char tStr[32]; |
ismaia | 12:bcced6833b8f | 15 | |
ismaia | 11:4d4334d909d3 | 16 | |
ismaia | 12:bcced6833b8f | 17 | int main() |
ismaia | 12:bcced6833b8f | 18 | { |
ismaia | 12:bcced6833b8f | 19 | |
ismaia | 8:9693d69ec483 | 20 | printf("Setting up ethernet interface...\r\n"); |
ismaia | 8:9693d69ec483 | 21 | if (eth.init() == 0 ) { //Use DHCP |
ismaia | 12:bcced6833b8f | 22 | printf("Ethernet setup OK\r\n"); |
ismaia | 12:bcced6833b8f | 23 | } else { |
ismaia | 12:bcced6833b8f | 24 | printf("Error: cannot set ethernet interface\r\n"); |
ismaia | 12:bcced6833b8f | 25 | return 1; |
ismaia | 8:9693d69ec483 | 26 | } |
ismaia | 12:bcced6833b8f | 27 | |
ismaia | 8:9693d69ec483 | 28 | printf("Trying to connect...\r\n"); |
ismaia | 8:9693d69ec483 | 29 | wait(0.5); |
ismaia | 12:bcced6833b8f | 30 | if ( eth.connect(30000) == 0 ) { |
ismaia | 12:bcced6833b8f | 31 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
ismaia | 12:bcced6833b8f | 32 | } else { |
ismaia | 12:bcced6833b8f | 33 | printf("Error: cannot set ethernet interface\r\n"); |
ismaia | 12:bcced6833b8f | 34 | return 1; |
ismaia | 7:0a56268d858c | 35 | } |
ismaia | 12:bcced6833b8f | 36 | |
ismaia | 12:bcced6833b8f | 37 | printf("Trying to update time...\r\n"); |
ismaia | 12:bcced6833b8f | 38 | if (ntp.setTime("0.fr.pool.ntp.org") == 0) { //set RTC time |
ismaia | 12:bcced6833b8f | 39 | printf("Set time successfully\r\n"); |
ismaia | 12:bcced6833b8f | 40 | wait(1); |
ismaia | 12:bcced6833b8f | 41 | rtcTime = time(NULL); //read the time |
ismaia | 12:bcced6833b8f | 42 | printf("Current time is: %s\r\n", ctime(&rtcTime)); |
ismaia | 12:bcced6833b8f | 43 | } else { |
ismaia | 12:bcced6833b8f | 44 | printf("Error: Cannot set time\r\n"); |
dudanian | 0:4e6ae21cbd31 | 45 | } |
ismaia | 12:bcced6833b8f | 46 | |
ismaia | 12:bcced6833b8f | 47 | printf("Disconnecting...\r\n"); |
ismaia | 12:bcced6833b8f | 48 | eth.disconnect(); |
ismaia | 12:bcced6833b8f | 49 | wait(1); |
ismaia | 12:bcced6833b8f | 50 | printf("Ethernet disconnected\r\n"); |
ismaia | 12:bcced6833b8f | 51 | |
ismaia | 12:bcced6833b8f | 52 | while(1) { |
ismaia | 12:bcced6833b8f | 53 | rtcTime = time(NULL); //read the time |
ismaia | 12:bcced6833b8f | 54 | strftime(tStr, 32, "%X\n", localtime(&rtcTime)); |
ismaia | 12:bcced6833b8f | 55 | printf("Time as a custom formatted string = %s\r\n", tStr); |
ismaia | 12:bcced6833b8f | 56 | wait(1); |
ismaia | 12:bcced6833b8f | 57 | } |
ismaia | 12:bcced6833b8f | 58 | } |