MBED clock sync using NTP Server from from internet
Dependencies: EthernetInterface NTPClient mbed-rtos mbed
Fork of MbedClock by
main.cpp
- Committer:
- ismaia
- Date:
- 2016-02-11
- Revision:
- 8:9693d69ec483
- Parent:
- 7:0a56268d858c
- Child:
- 10:d9ed3d95e5eb
File content as of revision 8:9693d69ec483:
#include "mbed.h" #include "rtos.h" #include "EthernetInterface.h" #include "NTPClient.h" #include <string> EthernetInterface eth; TCPSocketConnection server; NTPClient ntp; DigitalOut myled(LED1); time_t ctTime; int main() { printf("Setting up ethernet interface...\r\n"); if (eth.init() == 0 ) { //Use DHCP printf("Ethernet setup OK\r\n"); }else { printf("Error: cannot set ethernet interface\r\n"); return 1; } printf("Trying to connect...\r\n"); wait(0.5); if ( eth.connect(30000) == 0 ){ printf("IP Address is %s\n", eth.getIPAddress()); }else { printf("Error: cannot set ethernet interface\r\n"); return 1; } printf("Trying to update time...\r\n"); if (ntp.setTime("0.fr.pool.ntp.org") == 0) { printf("Set time successfully\r\n"); printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); } else { printf("Error: Cannot set time\r\n"); } eth.disconnect(); ctTime = time(NULL); while(1) { myled = 1; wait(1); myled = 0; wait(1); printf("Current time is: %s\r\n", ctime(&ctTime)); } }