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("\r\nSetting up...\r\n");
EthernetErr ethErr = eth.setup();
if(ethErr)
{
printf("Error %d in setup.\n", ethErr);
return -1;
}
printf("\r\nSetup OK\r\n");
time_t ctTime;
ctTime = time(NULL);
printf("\r\nCurrent time is : %s UTC\r\n", ctime(&ctTime));
Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
ntp.setTime(server);
ctTime = time(NULL);
printf("\r\nTime is now : %s UTC\r\n", ctime(&ctTime));
while(1)
{
}
return 0;
}
This program can be imported from here : http://mbed.org/users/donatien/programs/NetNtpClientExample.