You are viewing an older revision! See the latest version
NTP Client
Packages¶
Precompiled version: http://mbed.org/users/donatien/programs/NetServicesLPC1768
Library¶
Architecture¶
The NTP client is a simple UDP client that will update the mbed's RTC.
Includes¶
#include "NTPClient.h"
Reference¶
NTPClient()
Instantiate the NTP client.
Requests
NTPResult setTime(const Host& host) //Blocking NTPResult setTime(const Host& host, void (*pMethod)(NTPResult)) //Non blocking NTPResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
Update the time using the server host. If a callback is provided, the function returns immediately and calls the callback on completion or error, otherwise it blocks until completion.
void setOnResult( void (*pMethod)(NTPResult) ) void setOnResult( T* pItem, void (T::*pMethod)(NTPResult) ) void doSetTime(const Host& host)
Alternatively, you can set a callback function for all requests using setOnResult, and then call the non-blocking doSetTime method.
Result codes
enum NTPResult
{
NTP_OK,
NTP_PROCESSING,
NTP_PRTCL, //Protocol error
NTP_TIMEOUT, //Connection timeout
NTP_DNS //Could not resolve DNS Addr
};
Examples¶
This example updates the RTC.
#include "mbed.h"
#include "EthernetNetIf.h"
#include "NTPClient.h"
EthernetNetIf eth;
NTPClient ntp;
int main() {
printf("Start\n");
printf("Setting up...\n");
EthernetErr ethErr = eth.setup();
if(ethErr)
{
printf("Error %d in setup.\n", ethErr);
return -1;
}
printf("Setup OK\r\n");
time_t ctTime;
ctTime = time(NULL);
printf("Current time is (UTC): %s\n", ctime(&ctTime));
Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
ntp.setTime(server);
ctTime = time(NULL);
printf("\nTime is now (UTC): %s\n", ctime(&ctTime));
while(1)
{
}
return 0;
}
This program can be imported from here : http://mbed.org/users/donatien/programs/NTPClientExample.