Difference between revisions of "To Obtain Network Time"
(Created page with "{| style="width: 800px;" |- | ==Objective== The course will teach you how to use Processing to display the network time acquired by Microduino. ==Equipment== *'''Microdui...") |
|||
Line 27: | Line 27: | ||
==Debugging== | ==Debugging== | ||
− | Step | + | Step 1: Build the hardware environment according to the schematic, as follows: |
[[File:MicroduinoENCShow.jpg|600px|center|thumb]] | [[File:MicroduinoENCShow.jpg|600px|center|thumb]] | ||
The hardware buildup can refer to: | The hardware buildup can refer to: | ||
− | http://www.microduino.cc/wiki/index.php?title=Microduino_ENC%E7%BD%91%E7%BB%9C%EF%BC%88%E5%8D%81%E4%BA%8C%EF%BC%89%E2%80%94%E2%80%94%E2%80%94%E2%80%94%E7%94%A8NTP%E8%8E%B7%E5%8F%96Internet%E6%97%B6%E9%97%B4 | + | http://www.microduino.cc/wiki/index.php?title=Microduino_ENC%E7%BD%91%E7%BB%9C%EF%BC%88%E5%8D%81%E4%BA%8C%EF%BC%89%E2%80%94%E2%80%94%E2%80%94%E2%80%94%E7%94%A8NTP%E8%8E%B7%E5%8F%96Internet%E6%97%B6%E9%97%B4 |
− | Step | + | Step 2: Here is the code needed: |
The code of the two ends(Processing and Microduino) | The code of the two ends(Processing and Microduino) | ||
Line 41: | Line 41: | ||
Microduino: | Microduino: | ||
− | There are many time servers on the internet such as the time server offered by | + | There are many time servers on the internet such as the time server offered by American's NIST(National Institute of Standards and Technology) and the time server provided by Italy's INRiM(Istituto Nazionale di Ricerca Metereologica): |
Line 112: | Line 112: | ||
} | } | ||
− | Step | + | Step 3: Download the code and get it compiled successfully. |
− | Step | + | Step 4: See what happens in Processing. |
==Result== | ==Result== |
Latest revision as of 09:35, 13 September 2016
ObjectiveThe course will teach you how to use Processing to display the network time acquired by Microduino. Equipment
SchematicJust stack the four Microduino modules mentioned above. ProgramDebuggingStep 1: Build the hardware environment according to the schematic, as follows:
The code of the two ends(Processing and Microduino) Microduino: There are many time servers on the internet such as the time server offered by American's NIST(National Institute of Standards and Technology) and the time server provided by Italy's INRiM(Istituto Nazionale di Ricerca Metereologica):
static byte ntpServer[] = {193,204,114,232}; There are two methods for NTP request:
The srcPort parameter is used to find a data packets containing NTP server response among many receiving packets of the ENC28J60 module. You can choose the value according to individual needs, but the parameters in the ntpRequest and ntpProcessAnswer methods should remain the same. After acquiring the value of the time stamp, you have to convert it into data-time format. As follows:
Mistakes easily to be made: Leap Year probably: If it is Leap Year, 365*86500 seconds must be replaced by 366*86500 seconds. Here is the way to judge whether it is Leap Year: boolean isLeapYear(unsigned int year) { return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); } Since the number of days in each month changes, you should put these values in an array. Here is the format: static int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; If it is Leap Year, there are 29 days in February: if(isLeapYear(year) && month == 1) seconds = SECONDS_IN_DAY * 29; The time stamp refers to GMT(Greenwich Mean Time). If you live in a different time zone, you must correct the value: #define TIME_ZONE +1 [...] printDate(timeStamp + 3600 * TIME_ZONE);
Processing: //Acquire the data of the first serial port and define it. Or cache it if there is a new line println(Serial.list()); // is always my Arduino, so I open Serial.list()[0]. // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[0], 9600); myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line
//read port data String val = myPort.readStringUntil('\n'); if (val != null) { val = trim(val); println(val); if (val.startsWith("time:")) { //if some exception happend, initial time is 0 colock try { timeShow=val.substring(5,13); } catch (NumberFormatException e) { println("error:"+val); } //Display Text text ( timeShow, 90, 120); } } Step 3: Download the code and get it compiled successfully. Step 4: See what happens in Processing. ResultThere will appear network time on the screen
Video |