NTP over UDP?


anyone have luck receiving ntp packets via udp? i'm using official ether shield, , udp libraries written bjorn hartmann.

i can send request, single question mark...

code: [select]
#include <ethernet.h>
#include <udpstring.h>
#include <wstring.h>

/* attempt ntp time nist
* based on examples of how send , receive packets on udp
* using udpstring library bjoern@cs.stanford.edu 12/30/2008
*/

/* ethernet configuration
* arduino: set mac, ip address of ethernet shield, gateway,
* , local port listen on incoming packets */
byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed }; //mac address 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 listen on

/* target: set ip/port of computer receive
* udp messages arduino */
byte targetip[] = { 192, 43, 244, 18}; //time.nist.gov
int targetport = 123;

/* strings hold packets want send */
string asciistring;

string packet(32); //packet can 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 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() {


}

the result (in 'packet') not null-terminated string print/println routines expect. need loop through bytes in packet , print values individually.

--phil.


Arduino Forum > Forum 2005-2010 (read only) > Software > Interfacing > NTP over UDP?


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums