<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-CN">
		<id>http:///https//wiki.microduino.cn/index.php?action=history&amp;feed=atom&amp;title=Microduino_ENC_Network_%2812%29%E2%80%94%E2%80%94Use_NTP_get_Internet_time</id>
		<title>Microduino ENC Network (12)——Use NTP get Internet time - 版本历史</title>
		<link rel="self" type="application/atom+xml" href="http:///https//wiki.microduino.cn/index.php?action=history&amp;feed=atom&amp;title=Microduino_ENC_Network_%2812%29%E2%80%94%E2%80%94Use_NTP_get_Internet_time"/>
		<link rel="alternate" type="text/html" href="https//wiki.microduino.cn/index.php?title=Microduino_ENC_Network_(12)%E2%80%94%E2%80%94Use_NTP_get_Internet_time&amp;action=history"/>
		<updated>2026-04-21T09:44:22Z</updated>
		<subtitle>本wiki的该页面的版本历史</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>https//wiki.microduino.cn/index.php?title=Microduino_ENC_Network_(12)%E2%80%94%E2%80%94Use_NTP_get_Internet_time&amp;diff=2649&amp;oldid=prev</id>
		<title>Pkj：Created page with &quot;{{Language | Microduino ENC网络（十二）————用NTP获取Internet时间}} {| style=&quot;width: 800px;&quot; |- | ==Objective== Today’s tutorial is about getting an accura...&quot;</title>
		<link rel="alternate" type="text/html" href="https//wiki.microduino.cn/index.php?title=Microduino_ENC_Network_(12)%E2%80%94%E2%80%94Use_NTP_get_Internet_time&amp;diff=2649&amp;oldid=prev"/>
				<updated>2014-05-10T12:18:13Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{Language | Microduino ENC网络（十二）————用NTP获取Internet时间}} {| style=&amp;quot;width: 800px;&amp;quot; |- | ==Objective== Today’s tutorial is about getting an accura...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Language | Microduino ENC网络（十二）————用NTP获取Internet时间}}&lt;br /&gt;
{| style=&amp;quot;width: 800px;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
==Objective==&lt;br /&gt;
Today’s tutorial is about getting an accurate time via Internet, using the NTP (Network Time Protocol) service.&lt;br /&gt;
&lt;br /&gt;
==Equipment==&lt;br /&gt;
*'''[[Microduino-Core]]'''&lt;br /&gt;
*'''[[Microduino-FT232R]]'''&lt;br /&gt;
*'''[[Microduino-ENC28J60]]'''&lt;br /&gt;
*'''[[Microduino-RJ45]]'''&lt;br /&gt;
&lt;br /&gt;
*Other equipment&lt;br /&gt;
**USB cable&lt;br /&gt;
&lt;br /&gt;
==NTP==&lt;br /&gt;
&lt;br /&gt;
NTP is a client-server protocol at application layer; it uses UDP port 123 as transport protocol.&lt;br /&gt;
&lt;br /&gt;
If you send a request to a NTP server, you receive in its response a 64bit value (timestamp) made by:&lt;br /&gt;
&lt;br /&gt;
*the first 32bits represent the number of seconds since 01/01/1900;&lt;br /&gt;
*the last 32bits are the fraction of the actual second&lt;br /&gt;
[[File:NTP.jpg|600px|center|thumb]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Schematic==&lt;br /&gt;
*Microduino-ENC28J60&lt;br /&gt;
*Microduino-RJ45&lt;br /&gt;
*Microduino-Core&lt;br /&gt;
*Microduino-FT232R&lt;br /&gt;
Stack all modules and then connect the ethernet cable, as follows:&lt;br /&gt;
&lt;br /&gt;
[[File:MicroduinoENCShow.jpg|600px|center|thumb]]&lt;br /&gt;
&lt;br /&gt;
==Program==&lt;br /&gt;
&lt;br /&gt;
https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_ENC/ENCnetworktwelve&lt;br /&gt;
&lt;br /&gt;
==Debug==&lt;br /&gt;
Step 1：Download the EtherCard library and copy to your libraries fold of IDE, then restart IDE.&lt;br /&gt;
https://github.com/jcw/ethercard&lt;br /&gt;
&lt;br /&gt;
Step 2：Explain the program:&lt;br /&gt;
Many NTP servers are available on Internet: in the United States for example the NIST (National Institute of Standards and Technology) runs an entire network of severs. I live in Italy, so I chose as time server for this example the one run by the INRiM (Istituto Nazionale di Ricerca Metereologica):&lt;br /&gt;
    static byte ntpServer[] = {193,204,114,232};&lt;br /&gt;
&lt;br /&gt;
The EtherCard library has two methods for NTP queries:&lt;br /&gt;
*ntpRequest(ntpServer, srcPort), sends a request to the specificed server;&lt;br /&gt;
*ntpProcessAnswer(&amp;amp;timeStamp, srcPort), gets the response and extracts the timestamp (only the first 32bits).&lt;br /&gt;
&lt;br /&gt;
The srcPort parameter is used to find, among the many packets your Ethernet shield receives, the one that contains NTP server’s response: you can choose its value but it has to be the same both in the Request and in the ProcessAnswer.&lt;br /&gt;
Got the timestamp value, you have to convert it in date-time format. The method is the following:&lt;br /&gt;
&lt;br /&gt;
*count the number of complete years contained in the timestamp;&lt;br /&gt;
*then find out how many complete months are in the rest of the timestamp;&lt;br /&gt;
*do the same for days, hours, minutes;&lt;br /&gt;
*the final rest is the number of seconds.&lt;br /&gt;
&lt;br /&gt;
Just a few complications:&lt;br /&gt;
&lt;br /&gt;
Years could be leap years: for those you have to consider 366*86500 seconds instead of 365*86500. To check if actual year is a leap one, you can use the following method:&lt;br /&gt;
&lt;br /&gt;
    boolean isLeapYear(unsigned int year) {&lt;br /&gt;
      return (year % 4 == 0 &amp;amp;amp;&amp;amp;amp; (year % 100 != 0 || year % 400 == 0));&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The number of days in a month is variable: the value if saved in an array:&lt;br /&gt;
&lt;br /&gt;
    static int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};&lt;br /&gt;
&lt;br /&gt;
If we’re in a leap year, February has 29 days:&lt;br /&gt;
&lt;br /&gt;
    if(isLeapYear(year) &amp;amp;amp;&amp;amp;amp; month == 1) seconds = SECONDS_IN_DAY * 29;&lt;br /&gt;
&lt;br /&gt;
Finally, the timestamp value is referred to GMT, if you live in a different time zone, you have to adjust its value:&lt;br /&gt;
&lt;br /&gt;
    #define TIME_ZONE               +1&lt;br /&gt;
    [...]&lt;br /&gt;
    printDate(timeStamp + 3600 * TIME_ZONE);&lt;br /&gt;
&lt;br /&gt;
Step 3：Download the code and compile it.&lt;br /&gt;
&lt;br /&gt;
Step 4: Open the serial to check the time.&lt;br /&gt;
&lt;br /&gt;
==Result==&lt;br /&gt;
&lt;br /&gt;
Serial port will display the time：&lt;br /&gt;
&lt;br /&gt;
[[File:NTPShowTime.jpg|600px|center|thumb]]&lt;br /&gt;
&lt;br /&gt;
==Extention==&lt;br /&gt;
In order to observe the time easily, you can use the OLED to display the time.&lt;br /&gt;
[[File:MicroduinoENCTimeShow1.jpg|600px|center|thumb]]&lt;br /&gt;
[[File:MicroduinoENCTimeShow2.jpg|600px|center|thumb]]&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Pkj</name></author>	</entry>

	</feed>