To Obtain Network Time

From Microduino Wiki
Revision as of 09:35, 13 September 2016 by Fengfeng (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Objective

The course will teach you how to use Processing to display the network time acquired by Microduino.

Equipment


  • Other Hardware Equipment
    • A USB cable

Schematic

Just stack the four Microduino modules mentioned above.

Program

https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Processing/MicroduinnoNetTime

https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Processing/ProcessingNetTime

Debugging

Step 1: Build the hardware environment according to the schematic, as follows:

MicroduinoENCShow.jpg


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


Step 2: Here is the code needed:

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:

  • ntpRequest(ntpServer, srcPort),sending a request to the fixed server;
  • ntpProcessAnswer(&timeStamp, srcPort), extracting time stamp(Only the first 32 bits) and acquiring response.

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:

  • Calculate the corresponding number of years of the time stamp;
  • Calculate how many months left in the rest of the time;
  • Work out date, hour and minute with the same way;
  • The last remaining is the number of seconds.

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 the serial data, intercept the time information and display it

 //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.

Result

There will appear network time on the screen

ProcessingNetTimeResult.jpg


Video