Anyone have any luck receiving NTP packets via UDP? I'm using the official Ether shield, and the UDP libraries written by Bjorn Hartmann.
I can send the request, but all I get back is a single question mark...
#include <Ethernet.h>
#include <UdpString.h>
#include <WString.h>
/* Attempt to get NTP time from NIST
* Based on examples of how to send and receive packets over UDP
* using the UdpString library by bjoern@cs.stanford.edu 12/30/2008
*/
/* ETHERNET CONFIGURATION
* ARDUINO: set MAC, IP address of Ethernet shield, its gateway,
* and local port to listen on for incoming packets */
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
byte ip[] = { 192, 168, 1, 175 }; // Arduino's IP address
byte gw[] = { 192, 168, 1, 1 }; // Gateway IP address
int localPort = 123; //local port to listen on
/* TARGET: set this to IP/Port of computer that will receive
* UDP messages from Arduino */
byte targetIp[] = { 192, 43, 244, 18}; //time.nist.gov
int targetPort = 123;
/* Strings hold the packets we want to send */
String asciiString;
String packet(32); //packet can be max 32 bytes long
byte remoteIp[4]; // holds recvieved packet's originating IP
unsigned int remotePort[1]; // holds received packet's originating port
int i;
void setup() {
Serial.begin(9600);
Serial.print("Starting Ethernet, UDP... ");
Ethernet.begin(mac,ip,gw);
UdpString.begin(localPort);
asciiString = "/n";
// send a normal, zero-terminated string.
Serial.println("Sending packet...");
UdpString.sendPacket(asciiString,targetIp,targetPort);
delay(1000);
if (UdpString.available()) {
int packetSize = UdpString.readPacket(packet,remoteIp,remotePort);
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From IP ");
for(i=0; i<3; i++) {
Serial.print(remoteIp[i],DEC);
Serial.print(".");
}
Serial.print(remoteIp[3],DEC);
Serial.print(" Port ");
Serial.println(remotePort[0]);
Serial.println("Contents:");
Serial.println(packet);
}
}
void loop() {
}