Skip to content

Commit

Permalink
making this a git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Jaggars committed Apr 6, 2009
0 parents commit af01ea4
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sw*
125 changes: 125 additions & 0 deletions NTP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include "WProgram.h"
#include "NTP.h"

NTP::NTP(byte address[])
{
_address[0] = address[0];
_address[1] = address[1];
_address[2] = address[2];
_address[3] = address[3];
}

NTP::~NTP()
{
}

unsigned long NTP::get_time()
{
call();
return _send_timestamp;
}

int NTP::get_leap_indicator(byte b)
{
return b >> 6;
}

int NTP::get_version(byte b)
{
byte c = b << 2;
return c >> 5;
}

int NTP::get_mode(byte b)
{
byte c = b << 5;
return c >> 5;
}

unsigned long NTP::get_ulong()
{
unsigned long ulong = (unsigned long)UdpBytewise.read() << 24;
ulong |= (unsigned long)UdpBytewise.read() << 16;
ulong |= (unsigned long)UdpBytewise.read() << 8;
ulong |= (unsigned long)UdpBytewise.read();
return ulong;
}

unsigned long NTP::get_time_discard_precision()
{
unsigned long time = get_ulong();
// we are going to discard the sub-second stuff
UdpBytewise.read();
UdpBytewise.read();
UdpBytewise.read();
UdpBytewise.read();
return time;
}

void NTP::write_n(int what, int how_many)
{
for( int i = 0; i < how_many; i++ )
UdpBytewise.write(what);
}

int NTP::send_ntp_packet()
{
UdpBytewise.begin(123);
UdpBytewise.beginPacket(_address, 123);
// LI, Version, Mode
UdpBytewise.write(B11100011);
// Stratum
UdpBytewise.write(0);
// Polling Interval
UdpBytewise.write(6);
// Peer Clock Precision
UdpBytewise.write(0xEC);
// Root Delay
write_n(0, 4);
// Root Dispersion
write_n(0, 4);
// Reference Clock Id
UdpBytewise.write(49);
UdpBytewise.write(0x4E);
UdpBytewise.write(49);
UdpBytewise.write(52);
// Reference CLock Update Time
write_n(0, 8);
// Originate Time Stamp
write_n(0, 8);
// Receive Time Stamp
write_n(0, 8);
// Transmit Time Stamp
write_n(0, 8);
// End
return UdpBytewise.endPacket();
}

void NTP::call()
{
send_ntp_packet();

delay(1000);

if ( UdpBytewise.available() ) {
byte first_byte = UdpBytewise.read();
_leap_indicator = get_leap_indicator(first_byte);
_version = get_version(first_byte);
_mode = get_mode(first_byte);
_stratum = (int)UdpBytewise.read();
_polling_interval = 1 << ((int)UdpBytewise.read());
_precision = (char)UdpBytewise.read();
_delay_interval = (float) get_ulong();
_dispersion = (float) get_ulong();

_ref_clock_id[0] = UdpBytewise.read();
_ref_clock_id[1] = UdpBytewise.read();
_ref_clock_id[2] = UdpBytewise.read();
_ref_clock_id[3] = UdpBytewise.read();

_ref_clock_update_time = get_time_discard_precision();
_orig_timestamp = get_time_discard_precision();
_recv_timestamp = get_time_discard_precision();
_send_timestamp = get_time_discard_precision();
}
}
39 changes: 39 additions & 0 deletions NTP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef NTP_h
#define NTP_h

#include "WProgram.h"
#include <UdpBytewise.h>

class NTP
{
public:
NTP(byte address[]);
~NTP();
unsigned long get_time();
private:
byte _address[4];
int _leap_indicator;
int _version;
int _mode;
int _stratum;
int _polling_interval;
int _precision;
float _delay_interval;
float _dispersion;
byte _ref_clock_id[4];
unsigned long _ref_clock_update_time;
unsigned long _orig_timestamp;
unsigned long _recv_timestamp;
unsigned long _send_timestamp;

int send_ntp_packet();
unsigned long get_ulong();
unsigned long get_time_discard_precision();
void write_n(int what, int how_many);
int get_leap_indicator(byte b);
int get_version(byte b);
int get_mode(byte b);
void call();
};

#endif
13 changes: 13 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This library makes it easy to fetch a timestamp from an NTP timeserver. In order to use this library you will need the UDP libraries from:

http://bitbucket.org/bjoern/arduino_osc/

You will also need to modify the library to allow enough space in the buffers for the NTP packets. Open up UdpBytewise.h and change these lines:

#define UDP_TX_PACKET_MAX_SIZE 32
#define UDP_RX_PACKET_MAX_SIZE 32

to this:

#define UDP_TX_PACKET_MAX_SIZE 64
#define UDP_RX_PACKET_MAX_SIZE 64
22 changes: 22 additions & 0 deletions examples/ntp.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <Ethernet.h>
#include <NTP.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
byte ip[] = { 192, 168, 1, 44 }; // Arduino's IP address
byte gw[] = { 192, 168, 1, 1 }; // Gateway IP address

byte time_dot_nist_dot_gov[] = { 192, 43, 244, 18}; //time.nist.gov

void setup()
{
Serial.begin(9600);
Ethernet.begin(mac,ip,gw);
NTP ntp(time_dot_nist_dot_gov);
unsigned long time = ntp.get_time();
Serial.println(time);
}

void loop()
{
}

17 changes: 17 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#######################################
# Syntax Coloring Map For NTP
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

NTP KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

#######################################
# Constants (LITERAL1)
#######################################

0 comments on commit af01ea4

Please sign in to comment.