Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 19:31 12 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Odd WEB NTP command behaviour ...

Author Message
Taipan
Newbie

Joined: 02/01/2025
Location: Australia
Posts: 8
Posted: 11:06pm 03 Jan 2025
Copy link to clipboard 
Print this post

I built the  Silicon Chip Modem/Router Watchdog but found that it seemed to be too quick to find fault with my internet connection and power-cycle my HFC modem.  So I set about modifying the code to temper it's enthusiasm by doubling the timeout of WEB NTP to 10 seconds and if the first WEB NTP failed, try a second WEB NTP.

That seems to have helped, but I occasionally see odd behaviour.  

This is a normal response:

[09:38:56] First attempt to connect to a NTP server
[09:38:56] ntp address 103.165.180.123
[09:38:58] got ntp response: 04/01/2025 08:38:56
[09:38:58] OK - connected to NTP 1st attempt

Notice that about 2 seconds elapses between initiating the connection and success.

However, I occasionally see this:

[09:40:58] First attempt to connect to a NTP server
[09:40:58] Second attempt to connect to a NTP server
[09:40:58] Failed to connect to a NTP server
[09:41:03] Checking Google
[09:41:03] tcp address 142.251.221.78
[09:41:03] Connected
[09:41:03] OK - connected to Google

Notice that both the first failed attempt and the second failed attempt are all over in less than 1 second.  

It seems that the 10 second timeout for the WEB NTP commands is being ignored?


Edited 2025-01-04 09:07 by Taipan
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2611
Posted: 11:30pm 03 Jan 2025
Copy link to clipboard 
Print this post

I allow up to 15 attempts before giving up.
Sub MM.STARTUP
 Pause 200
 On error skip 4     'test for WiFi link
 If MM.Info(ip address) = "0.0.0.0" Then
   Pause 8000
   CPU restart
 EndIf

 Local float TZ = 10, DS = 1    'Time Zone, Daylight Saving
 Local integer n = 0
 Do While Date$ = "01-01-2024" 'test internet connection if wrong date
  On error skip 2
  WEB NTP TZ+DS, "162.159.200.123"
  Inc n : Print n,DateTime$(now),
  Pause 2000
  If n>14 Then
   Print "Failed to get NTP"
   End Sub
  Endif
 Loop
 Print
 Run 'start main program
End Sub
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6269
Posted: 11:56pm 03 Jan 2025
Copy link to clipboard 
Print this post

I would see if your ISP provides a NTP server. Most do (or should)
That would be more consistent and fewer chances of lost packets.

Second choice is use 0.au.pool.ntp.org or 1.au.pool.ntp.org etc which will give you an Australian server.


Upgrade the MMBasic .uf2 to the latest. Reliability has been improved.

Jim
VK7JH
MMedit
 
Andy-g0poy
Regular Member

Joined: 07/03/2023
Location: United Kingdom
Posts: 75
Posted: 12:05am 04 Jan 2025
Copy link to clipboard 
Print this post

  phil99 said  I allow up to 15 attempts before giving up.
Sub MM.STARTUP
...

  Inc n : Print n,DateTime$(now),
  Pause 2000
  If n>14 Then
   Print "Failed to get NTP"
   End Sub
  Endif
 Loop
 Print
 Run 'start main program
End Sub


is the pause 2000 command what you modified?

if so pause is in mS so pause 2000 is a two second pause
you want pause 10000 for a 10 second delay

Also do not use ip addresses for ntp servers, you never know when an ip address  will change, use the name of your areas ntp pool, as ,mentioned in the previous post

Andy
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2611
Posted: 12:34am 04 Jan 2025
Copy link to clipboard 
Print this post

The 15 attempts gives 30 seconds all up, which so far I have never needed.

You make a good point about addresses changing, though so far it has not happened.

Taking Jim's advice found these:-

ntp1.tpg.com.au - 203.12.160.2
ntp2.tpg.com.au - 203.26.24.6
 
Taipan
Newbie

Joined: 02/01/2025
Location: Australia
Posts: 8
Posted: 12:50am 04 Jan 2025
Copy link to clipboard 
Print this post

Thanks for the prompt replies and suggestions.  To answer your questions:
  TassyJim said  Upgrade the MMBasic .uf2 to the latest. Reliability has been improved.Jim

Yes, I had already upgraded before running the above test.

  Andy-g0poy said  is the pause 2000 command what you modified?

No, the command I used was:
WEB NTP 10, , 10000

So, I expected that 10 seconds would have to elapse before moving onto the next command?
I am guessing that I am getting a quick response from the NTP server, but as it is not the correct one, an error occurs, thus advancing to the second try?

  TassyJim said  Second choice is use 0.au.pool.ntp.org or 1.au.pool.ntp.org etc which will give you an Australian server.

OK, I will give one of those a try to see if it is more reliable.

  phil99 said  I allow up to 15 attempts before giving up.

That too makes good sense, but I will have to get my head around your code first, before I can implement it ...  
 
Andy-g0poy
Regular Member

Joined: 07/03/2023
Location: United Kingdom
Posts: 75
Posted: 01:24am 04 Jan 2025
Copy link to clipboard 
Print this post

  Andy-g0poy said  is the pause 2000 command what you modified?

No, the command I used was:
WEB NTP 10, , 10000

So, I expected that 10 seconds would have to elapse before moving onto the next command?
I am guessing that I am getting a quick response from the NTP server, but as it is not the correct one, an error occurs, thus advancing to the second try?




The only web ntp command I can see in the code you posted was:

WEB NTP TZ+DS, "162.159.200.123"

Which will default the timeout to 5 seconds

However that is a timeout for a response. That response may not be a valid NTP time, it may be some other message like "go away" :-)

How such things are handled in MMBASIC I don't know. It may just say OK and get on with things.

If it does exit the command for ANY type of response then that could explain the erratic behaviour

Andy
 
Taipan
Newbie

Joined: 02/01/2025
Location: Australia
Posts: 8
Posted: 01:46am 04 Jan 2025
Copy link to clipboard 
Print this post

  Andy-g0poy said  The only web ntp command I can see in the code you posted was:

WEB NTP TZ+DS, "162.159.200.123"

Which will default the timeout to 5 seconds

I am not sure where you got that from as I didn't post any code ...
What I posted was the PRINT output to the Maximite Control Centre window.

But, as you suggest, it seems that the 10s timeout is ignored when an invalid response is received.  

I guess I assumed that the WEB NTP command would keep trying to get a valid response until the 10s had expired.
 
Taipan
Newbie

Joined: 02/01/2025
Location: Australia
Posts: 8
Posted: 02:30am 04 Jan 2025
Copy link to clipboard 
Print this post

  TassyJim said  Second choice is use 0.au.pool.ntp.org or 1.au.pool.ntp.org etc which will give you an Australian server.


OK, I tried 1.au.pool.ntp.org and now it fails every time on both the first and second attempts.  Obviously I am doing something wrong?

This is the command that I was using:
WEB NTP 10, "1.au.pool.ntp.org", 10000


However, if I use just "au.pool.ntp.org" as the address of the server to connect to, it works fine again ...

Here is an example of where the first attempt did timeout after 10 seconds, but the second attempt was successful:
[13:24:13] First attempt to connect to a NTP server
[13:24:13] ntp address 103.152.64.212
[13:24:23] Second attempt to connect to a NTP server
[13:24:23] ntp address 103.152.64.212
[13:24:23] got ntp response: 04/01/2025 12:24:23
[13:24:23] OK - connected to NTP 2nd attempt
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2611
Posted: 02:33am 04 Jan 2025
Copy link to clipboard 
Print this post

That is in my MM.STARTUP Sub. The key to it is to test if the date has been changed from the default 01-01-2024 that MMBasic starts with. (01-01-2000 for older firmware)

An updated version of the relevant part.
Do While Date$ = "01-01-2024" 'exit the loop when the date gets updated
 On error skip 'ignore a failed attempt, just go around the loop again
 WEB NTP TZ+DS, "au.pool.ntp.org" 'replace with your ISP's NTP address
 Inc n 'increment the failure counter
 Print n,DateTime$(now), 'show the result
 Pause 2000
 If n>14 Then 'if the date is still wrong after 15 tries give up
  Print "Failed to get NTP"
  End Sub
 Endif
Loop

Edited 2025-01-04 12:35 by phil99
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6269
Posted: 06:43am 04 Jan 2025
Copy link to clipboard 
Print this post

using "1.au.pool.ntp.org" gave an error:
Error : Invalid address format

I guess that the leading 1 made it think that it was an IP not a domain name.
either way, dropping the 1. works so no real issue.

Jim
VK7JH
MMedit
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 07:55am 04 Jan 2025
Copy link to clipboard 
Print this post

  phil99 said  That is in my MM.STARTUP Sub. The key to it is to test if the date has been changed from the default 01-01-2024 that MMBasic starts with. (01-01-2000 for older firmware)

An updated version of the relevant part.
Do While Date$ = "01-01-2024" 'exit the loop when the date gets updated
 On error skip 'ignore a failed attempt, just go around the loop again
 WEB NTP TZ+DS, "au.pool.ntp.org" 'replace with your ISP's NTP address
 Inc n 'increment the failure counter
 Print n,DateTime$(now), 'show the result
 Pause 2000
 If n>14 Then 'if the date is still wrong after 15 tries give up
  Print "Failed to get NTP"
  End Sub
 Endif
Loop

Er... I think the End Sub ought to be Exit Sub

John
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025