Go Down

Topic: NTP Client/Server Issues (Read 1 time) previous topic - next topic

Xenosis

Mar 04, 2012, 08:37 pm Last Edit: Mar 26, 2012, 11:53 pm by Xenosis Reason: 1
I am trying to get the NTP client call implemented into my project and ran into a strange issue. Line for line I have pretty much copied in the Ethernet example for a NTP client but the code execution stops at the Udp.endPacket(); function and does not continue. I did not copy over the line for the MAC address as it would be different for my Ethernet shield

I have a telnet server running on 23 as well in the background but I don't see how it would get in the way. I also implemented slightly differently but it should work. I have a function getTime() that executes in the setup() section which sends the ntp packet to the server using the sub function sendNTPpacket(timeServer); - This is the function that has the endPacket() and gets stuck.

Code: [Select]
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress& address) {
 // set all bytes in the buffer to 0
 memset(packetBuffer, 0, NTP_PACKET_SIZE);
 lcd.print(".");
 // Initialize values needed to form NTP request
 // (see URL above for details on the packets)
 packetBuffer[0] = 0b11100011;   // LI, Version, Mode
 packetBuffer[1] = 0;     // Stratum, or type of clock
 packetBuffer[2] = 6;     // Polling Interval
 packetBuffer[3] = 0xEC;  // Peer Clock Precision
 // 8 bytes of zero for Root Delay & Root Dispersion
 packetBuffer[12]  = 49;
 packetBuffer[13]  = 0x4E;
 packetBuffer[14]  = 49;
 packetBuffer[15]  = 52;
 
 // all NTP fields have been given values, now
 // you can send a packet requesting a timestamp:  
 Udp.beginPacket(address, 123); //NTP requests are to port 123
 lcd.print(".");
 Udp.write(packetBuffer,NTP_PACKET_SIZE);
 lcd.print(".");
 Udp.endPacket();
 lcd.print(".");
 
 return;
}


I also tried changing it to void with a return; and alternately removing the & after IPAddress in the function prototype (not sure what it means). I also changed the variable NTP_PACKET_SIZE to a define with a value of 48.

edit: Just after posting this I verified and uploaded the sketch again and it seems to be passing through the function without any problems on the sending side though it is not detecting any incoming packets. Do I need to port forward any other port to my Ethernet shield (I would think not for the client side)?


What? But your PC isn't an NTP server is it? But even if it is, use its local IP address. This seems a hopelessly muddled way of getting one device on your internal network to talk to another device. You use a host-name lookup of a dynamic address maintained on an external web site, when all you have to do is connect to 192.168.0.22 or whatever it is.


I configured two of my computers as NTP servers and they work fine on the local subnet.The purpose of the external dyndns domain name is so that when I bring my laptop outside my subnet, I can still have it connect to my NTP server by getting the IP of my router dynamically. The problem is that it isn't working with the dyndns domain despite it replying with the right IP when I ping it. As I mentioned above I port forwarded 123 UDP/TCP to the internal NTP server.

This is issue #2


And if you are trying to get to an external NTP server (which sounds a bit more useful in many ways) then your dynamic IP address of your PC is irrelevant.


I would like to also get this to work (and it isn't right now). I tried using the supplied time.nist.gov (might be wrong) IP that is in the NTP Client code with no success on the arduino syncing with it. That same time server works fine on my computers.

This is issue #1

Nick Gammon

Please show the declaration for packetBuffer.

Quote
... and alternately removing the & after IPAddress in the function prototype (not sure what it means)


The & means "pass by reference" so the function gets a reference to the variable, not a copy of it.

The "return" at the end isn't needed, it returns anyway (what else would it do?).

Nick Gammon


Do I need to port forward any other port to my Ethernet shield (I would think not for the client side)?


Can you describe your setup in more detail? What are you forwarding to what?

Xenosis

#3
Mar 05, 2012, 04:21 pm Last Edit: Mar 05, 2012, 05:30 pm by Xenosis Reason: 1
I imported most of the code from : http://arduino.cc/en/Tutorial/UdpNtpClient

As far as I know the request is going out and I have a loop running the send request function every 7 seconds. My PC can synchronize with the time server just fine but the arduino isn't detecting any return udp packets.

edit: I followed some steps I found below to set up a local NTP server on my PC and it synchronized on the first packet send.

Quote
The Win 7 PC was configured as follows:

1.  In the 'Services' window(part of Administrative Tools) Stop the 'Windows Time' service if already running. The 'Startup Type' could be set as Manual or Automatic depending on the user needs.

2.  In the Registry Editor following changes to be made under the Key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time:

** Config -> AnnounceFlags = 5.

** HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\TimeProviders\NtpServer -> Enabled = 1.

3. Start the 'Windows Time' service in the  'Services' window(part of Administrative Tools).

4. Add an inbound firewall rule that allows UDP through port 123.


It is odd that it isn't able to synchronize with an external IP assuming that I shouldn't have to forward any ports on the client side. Any help resolving this matter would still be appreciated as I won't always have my PC on to synchronize the arduino's clock.

Additionally, I am trying to sync my PC's with a dyndns domain name I have set up on my router and forwarded both tcp and udp port 123 to the local machine but this is not working (only working via the subnet).

Is it possible to resolve a domain name from within an arduino as opposed to being restricted to a set IP?

Nick Gammon

You haven't exactly described your setup. What is connected to what? What addresses are you using? Is there a router? Are you currrently port forwarding to your PC?

http://www.portforward.com/

Go Up