NTP Client/Server Issues

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.

// 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)?

[quote author=Nick Gammon link=topic=95040.msg716570#msg716570 date=1331065640]
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.[/quote]

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

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

Please show the declaration for packetBuffer.

... 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?).

Xenosis:
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?

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.

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.

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

  2. 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?

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?

the setup is fairly standard

arduino eth shield > hub > router (subnet gateway)

ntp server pc > hub > router

ntp request from arduino works on severs running in local subnet (tested both windows 7 and ubuntu) however I am not able to utilize my dyndns domain name to send a request.

i verified that the domain is properly getting the IP of my router
also added firewall rules on the server and router to allow incoming udp and tcp packets via port 123

am I missing something that allows UFW (firewall on ubuntu) to accept incoming external requests that are sent to the external IP (via the domain name)

I imagine at this point it is more of a ubuntu question than an arduino one.

Xenosis:
am I missing something that allows UFW (firewall on ubuntu) to accept incoming external requests that are sent to the external IP (via the domain name)

I am having trouble visualizing an external request being sent to an external IP. Where does the internal stuff come into it?

however I am not able to utilize my dyndns domain name to send a request.

A request to what?

I think the OP is saying he wants his Arduino to make an ntp request to his dyndns provided hostname, which routes back to his pc, so requiring a path external to his lan. I'm not sure it would leave his lan though. His router would get the external IP back, and know it was it's own address.

Oh. Why?

Hopefully, he can answer that. I could be wrong, but that's how I read it.

dxw00d:
I think the OP is saying he wants his Arduino to make an ntp request to his dyndns provided hostname, which routes back to his pc, so requiring a path external to his lan. I'm not sure it would leave his lan though. His router would get the external IP back, and know it was it's own address.

Yes, this is exactly it. I'm not 100% sure on the details but my understanding was that it would first go to the dyndns servers and then back to my IP. The reason for using a service like this is so that in the event my routers IP changes (its not static), the dyndns service will update to send traffic to the new IP. That way, I would never have to change the NTP look-up.

This only applies to my PC's/laptop however as I don't think a domain can be used in place of an IP in the arduino code.

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.

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.

[quote author=Nick Gammon link=topic=95040.msg716570#msg716570 date=1331065640]
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.[/quote]

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

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

Renamed thread to better describe the issues listed in the post above.

This should also probably be moved to the networking section if a mod sees this.