WiFiUdpNtpClient
#include <>
/*H*******************************************************
UDP NTP Client
Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them,
see http://en.wikipedia.org/wiki/Network_Time_Protocol
created 4 Sep 2010
by Michael Margolis
modified 9 Apr 2012
by Tom Igoe
This code is in the public domain.
********************************************************/
#include
#include
#include
//************************* DEFINES ************************************
//************************* PROTOTYPES ************************************
unsigned long sendNTPpacket( IPAddress& address );
//************************* VARIABLES ************************************
int status = WL_IDLE_STATUS;
char ssid[] = "mynetwork"; // your network SSID (name)
char pass[] = "mypassword"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
unsigned int localPort = 2390; // local port to listen for UDP packets
IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( 9600 ); // OPEN SERIAL COMM & WAIT FOR PORT TO OPEN
while( !Serial)
{
; // WAIT FOR SERIAL PORT TO CONNECT. NEEDED FOR NATIVE USB PORT ONLY
}
if( WiFi.status() == WL_NO_SHIELD ) // CHECK FOR THE PRESENCE OF SHIELD
{
Serial.println( "WiFi shield not present");
while( true ); // DON'T CONTINUE:
}
String fv = WiFi.firmwareVersion();
if( fv != "1.1.0")
Serial.println( "Please upgrade the firmware");
while( status != WL_CONNECTED ) // ATTEMPT TO CONNECT TO WiFi NETWORK
{
Serial.print( "Attempting to connect to SSID: ");
Serial.println( ssid );
// CHANGE THIS LINE IF USING OPEN OR WEP NETWORK
status = WiFi.begin( ssid, pass ); // CONNECT TO WPA/WPA2 NETWORK
delay( 10000 ); // WAIT 10 SECONDS FOR CONNECTION
}
Serial.println( "Connected to WiFi");
printWifiStatus();
Serial.println( "\nStarting connection to server...");
Udp.begin( localPort );
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
sendNTPpacket( timeServer ); // SEND AN NTP PACKET TO A TIME SERVER
delay( 1000 ); // WAIT TO SEE IF A REPLY IS AVAILABLE
if( Udp.parsePacket() )
{
Serial.println( "packet received" );
// RECEIVED A PACKET, READ THE DATA FROM IT
Udp.read( packetBuffer, NTP_PACKET_SIZE );// READ PACKET INTO BUFFER
// TIMESTAMP STARTS AT BYTE 40 OF RECEIVED PACKET AND IS FOUR BYTES,
// OR TWO WORDS, LONG. fIRST, EXTRACT THE TWO WORDS:
unsigned long highWord = word( packetBuffer[40], packetBuffer[41] );
unsigned long lowWord = word( packetBuffer[42], packetBuffer[43] );
// COMBINE FOUR BYTES (TWO WORDS) INTO A LONG INTEGER
// THIS IS NTP TIME (SECONDS SINCE jAN 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print( "Seconds since Jan 1 1900 = ");
Serial.println( secsSince1900);
// NOW CONVERT NTP TIME INTO EVERYDAY TIME:
Serial.print("Unix time = " );
// UNIX TIME STARTS ON jAN 1 1970. iN SECONDS, THAT'S 2208988800:
const unsigned long seventyYears = 2208988800UL;
// SUBTRACT SEVENTY YEARS:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println( epoch );
// PRINT HOUR, MINUTE AND SECOND:
Serial.print( "The UTC time is " );// UTC IS TIME AT GREENWICH MERIDIAN
Serial.print((epoch % 86400L) / 3600); // PRNT HR (86400 == SECS / DAY)
Serial.print(':');
if( ((epoch % 3600) / 60) < 10 )
Serial.print( '0' ); // ADD LEADING ZRO
Serial.print( (epoch % 3600) / 60); // PRINT MINUTE (3600 EQUALS SECS PER MINUTE)
Serial.print( ':' );
if( (epoch % 60) < 10 )
Serial.print( '0' ); // ADD LEADING ZRO
Serial.println( epoch % 60); // PRINT SECOND
}
delay( 10000 ); // WAIT TEN SECONDS BEFORE ASKING FOR TIME AGAIN
}
/*F********************************************************************
* SEND AN NTP REQUEST TO THE TIME SERVER AT THE GIVEN ADDRESS
**********************************************************************/
unsigned long
sendNTPpacket( IPAddress& address )
{
//Serial.println("1");
memset( packetBuffer, 0, NTP_PACKET_SIZE ); // CLEAR BUFFER
// INITIALIZE VALUES NEEDED TO FORM NTP REQUEST
// (SEE URL ABOVE FOR DETAILS ON PACKETS)
//Serial.println("2");
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;
//Serial.println("3");
// 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
//Serial.println( "4" );
Udp.write( packetBuffer, NTP_PACKET_SIZE );
//Serial.println("5");
Udp.endPacket();
//Serial.println("6");
}
/*F********************************************************************
*
**********************************************************************/
void
printWifiStatus()
{
// PRINT SSID OF NETWORK YOU'RE ATTACHED TO:
Serial.print( "SSID: " );
Serial.println( WiFi.SSID() );
// PRINT YOUR WiFi SHIELD'S IP ADDRESS
IPAddress ip = WiFi.localIP();
Serial.print( "IP Address: " );
Serial.println( ip );
long rssi = WiFi.RSSI();
Serial.print( "signal strength (RSSI):" ); // PRINT RCVD SIGNAL STRENGTH
Serial.print( rssi );
Serial.println( " dBm" );
}