<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=WSPR%3A_Arduino_NTP_Client</id>
	<title>WSPR: Arduino NTP Client - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=WSPR%3A_Arduino_NTP_Client"/>
	<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=WSPR:_Arduino_NTP_Client&amp;action=history"/>
	<updated>2026-04-20T01:51:19Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://lms.onnocenter.or.id/wiki/index.php?title=WSPR:_Arduino_NTP_Client&amp;diff=51126&amp;oldid=prev</id>
		<title>Onnowpurbo: Created page with &quot; /*     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 an...&quot;</title>
		<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=WSPR:_Arduino_NTP_Client&amp;diff=51126&amp;oldid=prev"/>
		<updated>2018-05-25T09:03:43Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; /*     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 an...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt; /*&lt;br /&gt;
 &lt;br /&gt;
  Udp NTP Client&lt;br /&gt;
 &lt;br /&gt;
  Get the time from a Network Time Protocol (NTP) time server&lt;br /&gt;
  Demonstrates use of UDP sendPacket and ReceivePacket&lt;br /&gt;
  For more on NTP time servers and the messages needed to communicate with them,&lt;br /&gt;
  see http://en.wikipedia.org/wiki/Network_Time_Protocol&lt;br /&gt;
 &lt;br /&gt;
  created 4 Sep 2010&lt;br /&gt;
  by Michael Margolis&lt;br /&gt;
  modified 9 Apr 2012&lt;br /&gt;
  by Tom Igoe&lt;br /&gt;
  modified 02 Sept 2015&lt;br /&gt;
  by Arturo Guadalupi&lt;br /&gt;
 &lt;br /&gt;
  This code is in the public domain.&lt;br /&gt;
 &lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
 #include &amp;lt;SPI.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;Ethernet.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;EthernetUdp.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 // Enter a MAC address for your controller below.&lt;br /&gt;
 // Newer Ethernet shields have a MAC address printed on a sticker on the shield&lt;br /&gt;
 byte mac[] = {&lt;br /&gt;
   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 unsigned int localPort = 8888;       // local port to listen for UDP packets &lt;br /&gt;
 &lt;br /&gt;
 char timeServer[] = &amp;quot;id.pool.ntp.org&amp;quot;; // id.pool.ntp.org NTP server&lt;br /&gt;
 &lt;br /&gt;
 const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message&lt;br /&gt;
 &lt;br /&gt;
 byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets&lt;br /&gt;
 &lt;br /&gt;
 // A UDP instance to let us send and receive packets over UDP&lt;br /&gt;
 EthernetUDP Udp;&lt;br /&gt;
 &lt;br /&gt;
 void setup() {&lt;br /&gt;
   // Open serial communications and wait for port to open:&lt;br /&gt;
   Serial.begin(9600);&lt;br /&gt;
   while (!Serial) {&lt;br /&gt;
     ; // wait for serial port to connect. Needed for native USB port only&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   // start Ethernet and UDP&lt;br /&gt;
   if (Ethernet.begin(mac) == 0) {&lt;br /&gt;
     Serial.println(&amp;quot;Failed to configure Ethernet using DHCP&amp;quot;);&lt;br /&gt;
     // no point in carrying on, so do nothing forevermore:&lt;br /&gt;
     for (;;)&lt;br /&gt;
       ;&lt;br /&gt;
   }&lt;br /&gt;
   Udp.begin(localPort);&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void loop() {&lt;br /&gt;
   sendNTPpacket(timeServer); // send an NTP packet to a time server  &lt;br /&gt;
 &lt;br /&gt;
   // wait to see if a reply is available&lt;br /&gt;
   delay(1000);&lt;br /&gt;
   if (Udp.parsePacket()) {&lt;br /&gt;
     // We&amp;#039;ve received a packet, read the data from it&lt;br /&gt;
     Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer  &lt;br /&gt;
 &lt;br /&gt;
     // the timestamp starts at byte 40 of the received packet and is four bytes,&lt;br /&gt;
     // or two words, long. First, extract the two words:  &lt;br /&gt;
&lt;br /&gt;
     unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);&lt;br /&gt;
     unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);&lt;br /&gt;
     // combine the four bytes (two words) into a long integer&lt;br /&gt;
     // this is NTP time (seconds since Jan 1 1900):&lt;br /&gt;
     unsigned long secsSince1900 = highWord &amp;lt;&amp;lt; 16 | lowWord;&lt;br /&gt;
     Serial.print(&amp;quot;Seconds since Jan 1 1900 = &amp;quot;);&lt;br /&gt;
     Serial.println(secsSince1900); &lt;br /&gt;
 &lt;br /&gt;
     // now convert NTP time into everyday time:&lt;br /&gt;
     Serial.print(&amp;quot;Unix time = &amp;quot;);&lt;br /&gt;
     // Unix time starts on Jan 1 1970. In seconds, that&amp;#039;s 2208988800:&lt;br /&gt;
     const unsigned long seventyYears = 2208988800UL;&lt;br /&gt;
     // subtract seventy years:&lt;br /&gt;
     unsigned long epoch = secsSince1900 - seventyYears;&lt;br /&gt;
     // print Unix time:&lt;br /&gt;
     Serial.println(epoch); &lt;br /&gt;
 &lt;br /&gt;
     // print the hour, minute and second:&lt;br /&gt;
     Serial.print(&amp;quot;The UTC time is &amp;quot;);       // UTC is the time at Greenwich Meridian (GMT)&lt;br /&gt;
     Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)&lt;br /&gt;
     Serial.print(&amp;#039;:&amp;#039;);&lt;br /&gt;
     if (((epoch % 3600) / 60) &amp;lt; 10) {&lt;br /&gt;
       // In the first 10 minutes of each hour, we&amp;#039;ll want a leading &amp;#039;0&amp;#039;&lt;br /&gt;
       Serial.print(&amp;#039;0&amp;#039;);&lt;br /&gt;
     }&lt;br /&gt;
     Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)&lt;br /&gt;
     Serial.print(&amp;#039;:&amp;#039;);&lt;br /&gt;
     if ((epoch % 60) &amp;lt; 10) {&lt;br /&gt;
       // In the first 10 seconds of each minute, we&amp;#039;ll want a leading &amp;#039;0&amp;#039;&lt;br /&gt;
       Serial.print(&amp;#039;0&amp;#039;);&lt;br /&gt;
     }&lt;br /&gt;
     Serial.println(epoch % 60); // print the second&lt;br /&gt;
   }&lt;br /&gt;
   // wait ten seconds before asking for the time again&lt;br /&gt;
   delay(10000);&lt;br /&gt;
   Ethernet.maintain();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 // send an NTP request to the time server at the given address&lt;br /&gt;
 void sendNTPpacket(char* address) {&lt;br /&gt;
   // set all bytes in the buffer to 0&lt;br /&gt;
   memset(packetBuffer, 0, NTP_PACKET_SIZE);&lt;br /&gt;
   // Initialize values needed to form NTP request&lt;br /&gt;
   // (see URL above for details on the packets)&lt;br /&gt;
   packetBuffer[0] = 0b11100011;   // LI, Version, Mode&lt;br /&gt;
   packetBuffer[1] = 0;     // Stratum, or type of clock&lt;br /&gt;
   packetBuffer[2] = 6;     // Polling Interval&lt;br /&gt;
   packetBuffer[3] = 0xEC;  // Peer Clock Precision&lt;br /&gt;
   // 8 bytes of zero for Root Delay &amp;amp; Root Dispersion&lt;br /&gt;
   packetBuffer[12]  = 49;&lt;br /&gt;
   packetBuffer[13]  = 0x4E;&lt;br /&gt;
   packetBuffer[14]  = 49;&lt;br /&gt;
   packetBuffer[15]  = 52; &lt;br /&gt;
 &lt;br /&gt;
   // all NTP fields have been given values, now&lt;br /&gt;
   // you can send a packet requesting a timestamp:&lt;br /&gt;
   Udp.beginPacket(address, 123); //NTP requests are to port 123&lt;br /&gt;
   Udp.write(packetBuffer, NTP_PACKET_SIZE);&lt;br /&gt;
   Udp.endPacket();&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
</feed>