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 NTPClient_HelloWorld by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "NTPClient.h" 00004 00005 EthernetInterface eth; 00006 NTPClient ntp; 00007 00008 int main() 00009 { 00010 int ret = eth.init(); //Use DHCP 00011 if (!ret) { 00012 printf("Initialized, MAC: %s\n", eth.getMACAddress()); 00013 } else { 00014 printf("Error eth.init() - ret = %d\n", ret); 00015 return -1; 00016 } 00017 00018 ret = eth.connect(); 00019 if (!ret) { 00020 printf("Connected, IP: %s, MASK: %s, GW: %s\n", 00021 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); 00022 } else { 00023 printf("Error eth.connect() - ret = %d\n", ret); 00024 return -1; 00025 } 00026 00027 00028 printf("Trying to update time...\n"); 00029 00030 time_t ctTime; 00031 NTPResult result; 00032 00033 while(1) { 00034 result = ntp.setTime("pool.ntp.org"); 00035 //result = ntp.setTime("pool.ntp.org", NTP_DEFAULT_PORT, 2000); 00036 00037 if (result == NTP_OK) { 00038 time(&ctTime); 00039 printf("Time is set to (UTC): %s\n", ctime(&ctTime)); 00040 break; 00041 } 00042 00043 switch (result) { 00044 case NTP_CONN: ///<Connection error 00045 printf("Connection error\n"); 00046 break; 00047 case NTP_TIMEOUT: ///<Connection timeout 00048 printf("Connection timeout\n"); 00049 break; 00050 case NTP_PRTCL: ///<Protocol error 00051 printf("Protocol error\n"); 00052 break; 00053 case NTP_DNS: ///<Could not resolve name 00054 printf("Could not resolve name\n"); 00055 break; 00056 default: 00057 printf("Error result=%d\n", result); 00058 return -1; 00059 } 00060 00061 wait(5); 00062 } 00063 00064 00065 ret = eth.disconnect(); 00066 if (!ret) { 00067 printf("Disconnected\n"); 00068 } else { 00069 printf("Error eth.disconnect() - ret = %d\n", ret); 00070 } 00071 00072 00073 while(1) { 00074 } 00075 }
Generated on Fri Jul 15 2022 01:10:12 by
1.7.2
