NTP Client

Table of Contents

  1. Packages
  2. Library
  3. License

Packages

Precompiled version:

» Import this library into a programNTPClient_NetServices

NTP Client for the "old" NetServices libraries

Library

Architecture

The NTP client is a simple UDP client that will update the mbed's RTC.

Includes

#include "NTPClient.h"

Reference

» Import this program

Public Member Functions

  NTPClient ()
  Instantiates the NTP client.
NTPResult   setTime (const Host &host)
  Gets current time (blocking)
NTPResult   setTime (const Host &host, void(*pMethod)( NTPResult ))
  Gets current time (non-blocking)
template<class T >
NTPResult   setTime (const Host &host, T *pItem, void(T::*pMethod)( NTPResult ))
  Gets current time (non-blocking)
void  doSetTime (const Host &host)
  Gets current time (non-blocking)
void  setOnResult (void(*pMethod)( NTPResult ))
  Setups the result callback.
void  close ()
  This flags the service as to be destructed if owned by the pool.

Protected Member Functions

virtual void  poll ()
  This method can be inherited so that it is called on each Net::poll() call.

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 :

License





16 comments:

15 Jul 2010

If I follow the link to import the program, then choose 'download' I get a zip file, not a bin. Is this right? I'm new to this?

When I copy the code into my program, I'm getting timeouts - but the clock still changes to a value that is not correct...

05 Aug 2010

Hi Donatien,

NTP client works fine and my httpserver works fine but I want to combine it with the httpserver. My attempt failed with I combined HTTPserver library with NTPclient example. How should I proceed.

Regards Paul

06 Aug 2010

Hi Paul,

Could you publish your program so that I take a look?

Thanks,

Donatien

06 Aug 2010

Hi Donatien,

Programme published, called server1 http://mbed.org/users/pmr1/programs/server1/5ztls. The web page was just plain text with a bit of javascript to redisplay new data. It's not very elegant and needs more work.

Regards Paul

08 Aug 2010

Hi Donatien, New code compiled, see programme http://mbed.org/users/pmr1/programs/server2/latest, now functions correctly but with a couple of issues. The more puzzling one is that file created on the flash memory is always date stamped with the default time 01/01/2008 11:00 even though I have set localtime.

Regards Paul

16 Oct 2010

Donatien,

I was hoping to look at the source code for the NTPClient library, but I can't figure out how to find it (if it's even available). Would this be possible? Thanks!

14 Feb 2011

Hi Donatien!

i was wondering if your are keeping this project maintained, it works great but it really could use support for different time zones and daylight savings time.

14 Feb 2011

user Sergio Barrios wrote:

it really could use support for different time zones and daylight savings time.

Have a look at my TimeZone library. It requires some manual tweaking for setting the time zone, and takes some amount of memory, but it works...

25 Mar 2011

Does anyone now about a soulution for haveing a NTP-Server on the mbed. I'm setting up a distributed measurement / control system using several mbeds and would like to have the one mbed connected to a GPS serving as NTP server for the others in the network. Any Ideas?

Thanks!

25 Mar 2011

user Elias Breunig wrote:

Does anyone now about a soulution for haveing a NTP-Server on the mbed. I'm setting up a distributed measurement / control system using several mbeds and would like to have the one mbed connected to a GPS serving as NTP server for the others in the network. Any Ideas?

If all of these mbed have network access, why shouldn't they do the NTP queries by themself?

29 Mar 2011

:-) Simple.... I don't have internet access on the paricular platform wich happens to be an airplane... And it seems a bit of "overkill" to take along a iridium or globalstar modem just to get timing.... So is there anything available or has someone a good starting point for developing such functionallity? I have done some research on google but haven't found any good description of what the ntp server actually broadcasts ...

Thanks

Elias

Edit:

Now I've finally found something

http://www.scss.com.au/family/andrew/gps/ntp/

So I will start to make a similiar thing with mbed

Watch the progress at http://mbed.org/users/ejbfly/notebook/gps-based-ntp-server/

29 Mar 2011

@Elias Why would you want to have a full-blown NTP server? I think a "server" role can be doing a broadcast UDP packet with current time once in a while from your "server" MBED, and all other MBED's syncing by that packet. NTP server is designed to work through unreliable networks with undeterministic latencies, and it is a big overkill for your application.

19 Apr 2012

Is there a way to increase the NTP timeout?

I made an LCD clock example using your NTP code and one person seems to be getting the NTP timeout error return code and always gets the date set to Jul 2010? I was thinking perhaps he has a very slow network response time. I had trouble getting an IP address, and I had to increase the default on setup() to get one network to work.

It works just fine on all of my networks. I did give it a bad hostname and got exactly the same crazy time he did after about 12 seconds.

19 Apr 2012

Have you tried the version from Segundo?

I see that it has a NTPClient, is based on Donatien's original EthernetNetIf code, and this version of the library is only available with source so you can try tweaking timeouts in the source.

19 Apr 2012

Thanks! that looks like a great idea. Found some timeout stuff in the source, but have not had time to try it.

The cookbook NTP example led me to this version of the library. So would you be better off with that newer version in any case?

06 Jun 2012

how do i connect to the NTP server by connecting the ethernet cable to the computer instead of connecting the cable to the modem?