NTP giving same time everytime

01 Mar 2011

Hey there,

I just started off using mbed, and got NTPClient (http://mbed.org/cookbook/NTP-Client) from Donation Garnier working. The only thing that bothers me is, I keep getting "Sat Jul 24 19:33:34 2010" as latest time. I tried several servers, but nothing changed. What's happening there? I don't have a clue.

Ever heard of this?

Thanks guys.

Cheers! from Germany Stefan

01 Mar 2011

Can you show your code? In my experience NTP updates without problems.

01 Mar 2011

Hey, sure

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "NTPClient.h"
 
EthernetNetIf  eth;
NTPClient ntp;
  
int main() {

  printf("Init\n");

  printf("Setting up...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("\nSetup OK\n");

 
  // ------------- setting Real Time Clock -----------------------
  printf("Getting time from NTP server...\n");
  time_t theTime; 
  Host server(IpAddr(), 123, "0.de.pool.ntp.org");
  ntp.setTime(server);

  theTime = time(NULL);  
  printf("Time is now (UTC): %s\n\n",ctime(&theTime));

  return 0;
}

Cheers!

01 Mar 2011

Works for me. Do you use the wrong NetServices library? I know that there was in one of the older ones a bug preventing time set after a given date. I have tested with the one under http://mbed.org/users/segundo/libraries/NetServices/ljhqix.

03 Mar 2011

Hm..I imported the library you mentioned and deleted the old one, called NTPClient, recompiled but the issue remains. I keep getting

Time is now (UTC): Sat Jul 24 19:33:34 2010

as output. How the hell can the same program be working fine on your mbed?!?!

03 Mar 2011

I just took your code snippet from above and used it to create a new project. I imported the required librarys.

This is what it output:-

Program output...

Init
Setting up...

Setup OK
Getting time from NTP server...
Time is now (UTC): Thu Mar  3 15:39:09 2011

How the hell indeed!

03 Mar 2011

Stefan Goeckeritz wrote:

How the hell can the same program be working fine on your mbed?!?!

There are multiple networking libraries out there. If you get the wrong one, you get a NTPClient version with the bug you encountered. (I did the same thing as Andy, using the library I mentioned, and got the same result [actually not - I got another time stamp :-)]).

04 Mar 2011

OK guys, here we go again. I set up a new programm, pasted my previously (within this thread) published code into main and imported this library http://mbed.org/users/simon/libraries/NetServices/lic997/docs/ . Compiled and what have I got?

Time is now (UTC): Sat Jul 24 19:33:34 2010

Then I replaced the library with the one from http://mbed.org/users/segundo/libraries/NetServices/ljhqix, the one that hendrik used and I got the exact same result. I'll grab another mbed and try to run the same program on it.

Cheers!

05 Mar 2011

Hi guys

There was a bug in Donatien's original library which meant if it failed to get the time from the server, it would reset the RTC anyway to a fixed timestamp at the end of July 2010:

set_time( 1280000000 ); 

This is what Waldermar is seeing. The NTP request is failing and the RTC is being set to that time.

In Segundo's NetServices library, this bug was fixed, so if the NTP request fails, the RTC is not reset, it will be left as is.

That's probably why Andy got a different result.

Waldemar - I'd check that you have ethernet and other net services (eg. HTTPClient) working before using the NTPClient.

Regards
Daniel

PS Hendrik has found a memory leak in NTPClient which will get fixed in Segundo's library at some point!

05 Mar 2011

For the record, this is the stack I used.

http://mbed.org/users/segundo/libraries/NetServices/ljhqix

Waldemar said above he used this without success, odd.

05 Mar 2011

Hi,

As an aside, i'd appreciate it if anyone doing experiments with ethernet related code (such as this) could try the test library I put up that supports the full ethernet 1500 byte MTU. Technically I think it will be much better, but I'd like to catch anything before merging in to trunk; the main potential for impact is the increase in RAM allocation from the dedicated ethernet RAM block/AHB1. Whilst this won't impact any normal programs/users, it may impact something that has explicitly allocated memory in to this ethernet RAM/AHB1 space where it can no longer all fit in the 16k block; the linker will report an error. It'd be good to find now if there is any common code that does this, and just check it can be fixed.

The thread with the details is here:

Thanks!

Simon

05 Mar 2011

Hi Simon

Can we revisit "Compiler build details of AHBSRAM usage" on the bugs and suggestions cookbook page?

It's all very well having mbed code (which isn't published) allocating memory there, but how are we meant to know how much is free for our own use?

Regards Daniel

06 Mar 2011

Simon Ford wrote:

As an aside, i'd appreciate it if anyone doing experiments with ethernet related code (such as this) could try the test library I put up that supports the full ethernet 1500 byte MTU.

Hi Simon

I compiled Segundo's NetServices_HelloWorld example with the test library, and seems to run ok. That's the full NetServices including NTP, and HTTP client and server (with RPC). Of course, it's possible that other people have allocated more memory in the AHB1 space by fiddling with the values in lwipopts.h if they particularly wanted to improve performance.

What I haven't looked at is if anything needs changing in the NetServices library to take advantage of a large MTU size.

Regards
Daniel

06 Mar 2011

Quote:

I compiled Segundo's NetServices_HelloWorld example with the test library, and seems to run ok.

Well, runs ok, but I did a further test which was to a call to AvailableMemory just before going into the NetPoll loop for the server.

With the mbed library version 28, there were 13860 bytes available.

With the test library, there were only 4052 bytes available, which seems a lot more bytes lost than accounted for just with the MTU size increase. Test program here.

Can you explain where the memory has gone?

Regards
Daniel

14 Mar 2011

Hey guys,

@Daniel Peter thanks for the input. I'll check my connections.

Cheers!

16 Apr 2011

Hi,

using the newest NetServices Library and the NetServices_HelloWorld Programm Programm I noticed, that if I remove the line

HTTPResult r = http.get("http://mbed.org/media/uploads/donatien/hello.txt", &txt);

or point it to a not resolvable server, the time is set to

Time is now (UTC): Sat Jul 24 19:33:34 2010

or something similar.

The http.get can fail, e.g. because it points to a file not available, it just has to be there for ntp.setTime to successfuly update the time.

Haven't seen anyone describe this behavior so may be it helps with solving the problem.

Regards Jan

18 Nov 2011

Anyone another methode to solve this problem because i have the same problem. Tried to install a ntp server on my pc nothing changes. Only get Time is now (UTC): Sat Jul 24 19:33:34 2010... ´ Tried many different libraries.

19 Feb 2012

I implemented the UTC to get the time and dates. How do i create a function which enables me to edit the date and time?

03 Apr 2012

Jan, I have observed the same exact problem. I set up an NTP server on my ubuntu box and I confirmed it is working by using it from another server to set the time. I ran Wireshark to observe what is happening. In my code the ntpclient just times out, I don't even see the transactions in the wireshark trace. However if I do a bogus httpclient get just before the ntp settime then it works great and I see the transaction in the wireshark trace on the ntp server. I have looked at the ntpclient code and cannot figure out why this behavior is occurring. I think Donatien or Segundo might have better luck debugging this.

Thanks.

17 Apr 2012

I also see the same behaviour as Jan. Crazy that it was found over a year ago and no-one seems to care. I haven't seen anyone working on any kind of solid networking stack for mbed, only various failed or abandoned attempts, and the last updates to the major libraries were a long time ago. If I had more c++ i'd be happy to help out, but until then it would be good if someone could put this post somewhere more prominent so that others don't waste days investigating this problem.

17 Apr 2012

I tried NTPClientMin and it seems to work fine for me. I'm connecting to the time service on my 2008R2 server if it makes a difference.

19 Apr 2012

It did also work for me for some time, then suddenly stopped, and never worked again. Even deleting it from the DHCP server tables and so on didn't seem to help. It worked fine in other networks too, so very odd behaviour.

17 May 2012

Donatien's NTPClient works well if it is used in blocking mode:

NTPResult NTPUpdate(void)
{
	terminal.printf("Updating System Time via NTP (Internet Time Protocol)\n"); 
  	Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
	NTPResult res = ntp.setTime(server);
	return res;
}

But using the callback function, setTime always returns NTP_DNS:

void cbTime(NTPResult res)
{
	char str[32];
	switch(res)
	{
		case NTP_OK: 		time_t ctTime;
	                                ctTime = time(NULL);  
                                        memcpy(str,"NTP_OK",6);
					break;
		case NTP_PROCESSING:    memcpy(str,"NTP_PROCESSING",14);
					break; 
		case NTP_PRTCL: 	memcpy(str,"NTP_PRTCL",9);
					break;
		case NTP_TIMEOUT: 	memcpy(str,"NTP_TIMEOUT",11);
					break;
		case NTP_DNS: 		memcpy(str,"NTP_DNS",7);
					break;
	}
	terminal.printf("Time Update resulted: %s\n",str);	
}

NTPResult NTPUpdate(void)
{
  	Host server(IpAddr(), 123, "0.uk.pool.ntp.org");	
	NTPResult res = ntp.setTime(server, &cbTime);
	return res;
}

It would be great if someone writes a good and non-blocking NTP client!

Stefan.

05 Jun 2012

is there a way to synchronise time through wireless methods?

05 Jun 2012

This notebook page of jim hamblen's gives pretty good coverage to time syncs.

http://mbed.org/users/4180_1/notebook/internet-lcd-clock/

05 Jun 2012

I managed to sync using his method. But it requires me to connect the ethernet cable to the modem/router. Is there a way to synchronise time by connecting the ethernet cable to the computer instead?

06 Jun 2012

<<quote blackninja>> I managed to sync using his method. But it requires me to connect the ethernet cable to the modem/router. Is there a way to synchronise time by connecting the ethernet cable to the computer instead? <</quote>> Long ago, in Win XP, I recall seeing a setting to run an NTP time server. I suspect it's gone in Win 7 home, maybe in Pro, surely in server or home server. But indeed, the PC has to be on the internet to get its time.

Or get time from a serial port/USB connected GPS receiver.

Or a $10 battery backed up clock chip connected to the mbed.