實戰ARDUINO的RTC時鐘模組,教你怎麼進行網路校時

透過網路校時設計RTC 時鐘模組

整合上述程式,我們可以將RTC 時鐘模組的網路校時功能給予實作出來。
我們打開Arduino 開發板的開發工具:Sketch IDE整合開發軟體,攥寫一段程式,如下表所示之網路校時RTC 時鐘模組測試程式,我們就可以透過W5100 以太網路模組取得網路時間並校正TC 時鐘模組。

表 2網路校時RTC 時鐘模組測試程式

網路校時RTC 時鐘模組測試程式(SetTime_fromNet)
#include <Time.h>
#include <DS1307RTC.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

tmElements_t tm;

int Ntpyr,Ntpmon,Ntpday,Ntphr,Ntpmin,Ntpsec ;


// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
IPAddress ip(192, 168, 2, 200);     //fix ip
IPAddress dnServer(168, 95, 1, 1);    //dns ip
// the router's gateway address:
IPAddress gateway(192, 168, 2, 254);    //gateway ip
// the subnet:
IPAddress subnet(255, 255, 255, 0);     //sub mask


unsigned int localPort = 8888;       // local port to listen for UDP packets

char timeServer[] = "time.nist.gov"; // 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
EthernetUDP Udp;
// timeZoneOffset = (Time Zone) * 3600L eg. (+8) * 3600L = 28800L for Taipei, Taiwan
const long timeZoneOffset = 28800L;
// sync to NTP server every "ntpSyncTime" seconds, set to 1 hour or more to be reasonable
unsigned long ntpSyncTime = 21600;
// adjust the sync latency with computer NTP client in seconds
unsigned int syncLatency = 2;


const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

 


void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
  Ethernet.begin(mac, ip, dnServer, gateway, subnet);

  }
  Udp.begin(localPort);
  Serial.begin(9600);
    ntpSyncDS1307() ;
}

void loop() {
  tmElements_t ttm;

  if (RTC.read(ttm)) {

    Serial.print("Ok, Time = ");
     Serial.print(print2digits(ttm.Hour));
    Serial.print(':');
     Serial.print(print2digits(ttm.Minute));
    Serial.print(':');
     Serial.print(print2digits(ttm.Second));
    Serial.print(", Date (Y/M/D) = ");
    Serial.print(print4digits(tmYearToCalendar(ttm.Year)));
    Serial.print('/');
    Serial.print(print2digits(ttm.Month));
    Serial.print('/');
    Serial.print(print2digits(ttm.Day));
    Serial.println();
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the Set-Time");
      Serial.println("example to initialize the time and begin run-ning.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the cir-cuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);
}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(char* address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  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;

  // 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
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

String  print2digits(int number) {
  String ttt ;
  if (number >= 0 && number < 10)
  {
     ttt =String("0")+String(number);
  }
  else
  {
     ttt =String(number);
  }
   return ttt ;
}

String  print4digits(int number) {
  String ttt ;
     ttt =String(number);
   return ttt ;
}


void getInternetDateTime()
{
    sendNTPpacket(timeServer); // send an NTP packet to a time server

  // wait to see if a reply is available
  delay(1000);
  if ( Udp.parsePacket() ) {
    // We've received a packet, read the data from it
    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // combine the 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 the hour, minute and second:
    Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
    Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
    Serial.print(':');
    if ( ((epoch % 3600) / 60) < 10 ) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
    Serial.print(':');
    if ( (epoch % 60) < 10 ) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.println(epoch % 60); // print the second
  }
  // wait ten seconds before asking for the time again

}

// functions to be called when an alarm triggers:
void ntpSyncDS1307() {
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  // wait to see if a replay is available
  delay(1000);
  if (Udp.parsePacket()) {
    // We've received a packet, read the data from it
    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
    // the timstamp starts at byte 40 of the 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 the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900)
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    // now convert NTP time into everyday time:
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800L;
    // substract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears + timeZoneOffset + syncLatency;
    setTime(epoch);
    RTC.set(epoch);
   // RTC.Write
    // output time and "Sync OK" message every sync
    Serial.print(year());
    Serial.print('-');
    Serial.print(month());
    Serial.print('-');
    Serial.print(day());
    Serial.print(' ');
    Serial.print(hour());
    Serial.print(':');
    Serial.print(minute());
    Serial.print(':');
    Serial.print(second());
    Serial.print(' ');
    Serial.println("Sync OK");
  }
}

下載網址:https://github.com/brucetsao/techbang/tree/master/201602

如下圖所示,讀者可以看到本次實驗-網路校時RTC 時鐘模組測試程式結果畫面。

實戰ARDUINO的RTC時鐘模組,教你怎麼進行網路校時
圖 7網路校時RTC 時鐘模組測試程式結果畫面

本文主要介紹之Arduino開發板,透過W5100乙太網路擴充卡連接上網際網路,取得網路校正時間,並且成功校正RTC 時鐘模組,這樣的功能,非常的網路化、實務化的核心功能。
透過本文的解說,相信讀者更可以應用RTC 時鐘模組到商業化的產品,進而將本技術應用在物聯網的核心技術,後續筆者還會繼續發表智慧家庭相關的文章,讓我們在未來可以創造出更優質、智慧化的家庭。
敬請期待更多的文章。

筆者介紹

曹永忠 (Yung-Chung Tsao):目前為自由作家,專注於軟體工程、軟體開發與設計、物件導向程式設計、Arduino開發、嵌入式系統開發,商品攝影及人像攝影。長期投入資訊系統設計與開發、企業應用系統開發、軟體工程、新產品開發管理、商品及人像攝影等領域,並持續發表作品及相關專業著作。
Email:prgbruce@gmail.com
Line ID:dr.brucetsao  
Arduino部落格:http://taiwanarduino.blogspot.tw/
臉書社群(Arduino.Taiwan):https://www.facebook.com/groups/Arduino.Taiwan/
活動官網:http://arduino.kktix.cc/
Youtube:https://www.youtube.com/channel/UCcYG2yY_u0m1aotcA4hrRgQ

實戰ARDUINO的RTC時鐘模組,教你怎麼進行網路校時

 

參考文獻:

曹永忠, 許智誠, & 蔡英德. (2015a). Arduino投币定时器(网络篇):Using Arduino to Develop a Timing Controlling Device via Internet (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.
曹永忠, 許智誠, & 蔡英德. (2015b). Arduino投幣計時器(網路篇):Using Arduino to Develop a Timing Controlling Device via Internet (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.
曹永忠, 許智誠, & 蔡英德. (2015c). Arduino程式教學(常用模組篇):Arduino Programming (37 Sensor Modules) (初版 ed.). 台湾、彰化: 渥玛数位有限公司.
曹永忠, 許智誠, & 蔡英德. (2015d). Arduino程式教學(無線通訊篇):Arduino Programming (Wireless Communication) (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.
曹永忠, 許智誠, & 蔡英德. (2015e). Arduino编程教学(无线通讯篇):Arduino Programming (Wireless Communication) (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.
曹永忠, 許智誠, & 蔡英德. (2015f). Arduino编程教学(常用模块篇):Arduino Programming (37 Sensor Modules) (初版 ed.). 台湾、彰化: 渥玛数位有限公司.
曹永忠, 許碩芳, 許智誠, & 蔡英德. (2015a). Arduino程式教學(RFID模組篇):Arduino Programming (RFID Sensors Kit) (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.
曹永忠, 許碩芳, 許智誠, & 蔡英德. (2015b). Arduino編程教学(RFID模块篇):Arduino Programming (RFID Sensors Kit) (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.

曹永忠
作者

曹永忠,國立中央大學資訊管理學系博士,目前在暨南大學電機工程學系兼任助理教授與自由作家,專注於軟體工程、軟體開發與設計、物件導向程式設計......並持續發表作相關專業著作。

使用 Facebook 留言
發表回應
謹慎發言,尊重彼此。按此展開留言規則