Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 9 months ago.
NTPClient::setTime returns NTP_OK even when ethernet cable is unplugged?
I was trying to use NTPClient::setTime as a way to check whether internet access is available, somehow it doesn't work. As stated in the tittle, even when Ethernet cable is unplugged, setTime returns 0(NTP_OK).
This seems rather wired. Did this happen to anyone of you?
Thanks, ZL
Question relating to:
1 Answer
9 years, 9 months ago.
Hi ZL,
Donatien's sample is simplified, to focus the reader only on the NTP client functions. For a more detailed, and robust solution, the program needs to check the return values from the prior function calls.
This is extracted from one of my applications (and then I modified it for the NTP example).
RawSerial pc(USBTX, USBRX); EthernetInterface eth; // This function returns the current status of connection. bool get_link_status(void) { uint32_t tmp = lpc_mii_read_data(); return (tmp & DP8_VALID_LINK) ? true : false; } ... pc.printf("Initializing network interface...\r\n"); if (0 == eth.init()) { // It should always take this path do { pc.printf("Connecting to network...\r\n"); if (0 == eth.connect()) { pc.printf("Ethernet connected as %s\r\n", eth.getIPAddress()); while (get_link_status()) { //pc.printf("Connection is good.\r\n"); if (pc.readable()) { int c = pc.getc(); switch (c) { case 'r': mbed_reset(); break; case 't': if (ntp.setTime("0.pool.ntp.org") == 0) { printf("Set time successfully\r\n"); } break; default: pc.printf("Menu:\r\n" " t - Time set from NTP server.\r\n" " r - reset\r\n" ); break; } // switch } // if pc.readable() } // while (get_link_status()) pc.printf("lost connection.\r\n"); eth.disconnect(); } else { pc.printf(" ... failed to connect.\r\n"); } } while (1); } else { pc.printf(" ... failed to initialize, rebooting...\r\n"); mbed_reset(); }