2016年7月18日 星期一

利用 NTP 伺服器來同步 Arduino 系統時鐘 (二)

晚上檢視前天寫的程式, 發現 Time 函式庫還有一個 weekday() 函數可用來取得今日是星期幾, 它會傳回 1~7 的整數, 星期天是一周的起始傳回 1, 星期六傳回 7, 我們可以用一個陣列將 weekday() 的傳回值映照成 Sun, Mon, ... Sat 等字串, 以便顯示在 1602 LCD 上時更具有可讀性. 參考 :

# 利用 NTP 伺服器來同步 Arduino 系統時鐘 (一)

我把前一篇中測試 5 的自訂函式做了一番更動, 從 NTP 取回時間訊息經運算後傳回台灣時間的函式改名為 getCST(); 取得日期與時間字串的函式也拆成 getDate() 與 getTime(), 這樣比較有彈性. 然後寫了一個新函數 getWeek() 來轉換 weekday() 函數所傳回的星期代碼. 完整程式如下 :

測試 6 : 

#include <SoftwareSerial.h>
#define DEBUG true
#include <Time.h>
#include <TimeAlarms.h>
#include <LiquidCrystal.h>
#define RS 2
#define E 3
#define D4 10
#define D5 11
#define D6 12
#define D7 13

SoftwareSerial esp8266(7,8); //(RX,TX)
byte packetBuffer[128]; //buffer : send & recv data to/from NTP
LiquidCrystal lcd(RS,E,D4,D5,D6,D7);  //create LCD object

void setup() {
  Serial.begin(9600);
  esp8266.begin(9600);
  sendData("AT+RST\r\n",2000,DEBUG); // reset ESP8266
  sendData("AT+GMR\r\n",1000,DEBUG);
  delay(3000); //wait for wifi connection to get local ip
  sendData("AT+CIFSR\r\n",1000,DEBUG); //get ip address
  Serial.println("Sending request to NTP server ...");
  sync_clock();
  Alarm.timerRepeat(60, sync_clock);
  lcd.begin(16,2); //define 2*16 LCD
  lcd.clear(); //clear screen
  }

void loop() {
  String d=getDate();
  String t=getTime();
  String w=getWeek();
  Serial.println(d + " " + t + " " + w);
  lcd.setCursor(0,0); //move to (x,y)
  lcd.print(d); //print date
  lcd.setCursor(11,0); //move to (x,y)
  lcd.print(w); //print date  
  lcd.setCursor(0,1); //move to (x,y)
  lcd.print(t); //print date  
  Alarm.delay(1000);
  }

void sync_clock() {
  setTime(getCST());
  }

String getDate() {
  String d=(String)year() + "-";
  byte M=month();
  if (M < 10) {d.concat('0');}
  d.concat(M);
  d.concat('-');
  byte D=day();
  if (D < 10) {d.concat('0');}
  d.concat(D);
  return d;
  }

String getTime() {
  String t="";
  byte h=hour();
  if (h < 10) {t.concat('0');}
  t.concat(h);
  t.concat(':');
  byte m=minute();
  if (m < 10) {t.concat('0');}
  t.concat(m);
  t.concat(':');
  byte s=second();
  if (s < 10) {t.concat('0');}
  t.concat(s);
  return t;
  }

String getWeek() {
  String w[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  return w[weekday()-1];
  }

long getCST() {
  //Start UDP Rrequest to NTP Server 91.226.136.136, 82.209.243.241,192.5.41.40
  //132.163.4.101, 132.163.4.102, 132.163.4.103
  sendData("AT+CIPSTART=\"UDP\",\"91.226.136.136\",123\r\n",5000,DEBUG);
  memset(packetBuffer,0,128); //clear buffer
  packetBuffer[0]=0xE3;       //LI, Version, Mode
  packetBuffer[1]=0x00;       //Stratum, or type of clock
  packetBuffer[2]=0x06;       //Polling Interval
  packetBuffer[3]=0xEC;       //Peer Clock Precision
  packetBuffer[12]=0x31;      //reference ID (4 bytes)
  packetBuffer[13]=0x4E;
  packetBuffer[14]=0x31;
  packetBuffer[15]=0x34;
  //send request to NTP Server
  sendData("AT+CIPSEND=48\r\n",1000,DEBUG); //send data length
  for (byte i=0; i < 48; i++) {
    esp8266.write(packetBuffer[i]);
    delay(5);
    }

  //deal with NTP response
  memset(packetBuffer,0,128); //clear buffer to store NTP response
  Serial.println();
  Serial.println("NTP server answered : ");
  int i=0; //packet byte counter
  while (esp8266.available() > 0) { //if receive NTP response : fall in loop
    byte ch=esp8266.read(); //got NTP response, read one byte for each loop
    if (i < 128) {packetBuffer[i]=ch;} //store received byte to packet buffer
    //show receving bytes in hex
    if (ch < 0x10) {Serial.print('0');} //prefix with '0' if byte value 0~9
    Serial.print(ch, HEX);
    Serial.print(' ');
    if ((((i+1) % 10) == 0)) {Serial.println();} //newline if exceeds 10 bytes
    delay(5); //wait 5ms for next incoming byte
    i++; //increment packet byte counter
    if ((i < 104) && (esp8266.available() == 0)) { //wainting if lags
      //Response packets not enough but no response : wait 1.5 seconds
      byte wcount=0; //waiting counter
      while (esp8266.available() == 0) { //loop until timeout (1.5 seconds)
        Serial.print("!"); //show ! means waiting for response packet
        delay(100);
        wcount += 1; //increment waiting counter
        if (wcount >= 15) {break;} //waiting timeout : quit loop
        }
      }
    }
  Serial.println();
  Serial.println();
  Serial.print(i+1);
  Serial.println(" bytes received"); // will be more than 48
  //Show time stamp (locates from byte 101~104 of the response packet)
  Serial.print("NTP time stamp packets (byte 101~104)=");
  Serial.print(packetBuffer[101],HEX);
  Serial.print(" ");
  Serial.print(packetBuffer[102],HEX);
  Serial.print(" ");
  Serial.print(packetBuffer[103],HEX);
  Serial.print(" ");
  Serial.print(packetBuffer[104],HEX);
  Serial.println();

  //handling time packets (4 bytes long) : combine them into words
  unsigned long highWord=word(packetBuffer[101],packetBuffer[102]);
  unsigned long lowWord=word(packetBuffer[103],packetBuffer[104]);
  //shift high word 16 bits left & OR with low word to form a double word
  //the result is a NTP time stamp (seconds since Jan 1 1900):
  unsigned long secsSince1900=highWord << 16 | lowWord;
  Serial.print("NTP time stamp (seconds since 1900-01-01)=");
  Serial.println(secsSince1900);

  //convert NTP time stamp to Unix time stamp :
  //Unix time starts on Jan 1 1970=2208988800 seconds since 1900-01-01
  //subtract seventy years to get Unix time stamp, plus 28800 (UTC+8)
  unsigned long epoch=secsSince1900 - 2208988800UL;
  Serial.print("Unix time stamp (seconds since 1970-01-01)=");
  Serial.println(epoch); //print Unix time
  sendData("AT+CIPCLOSE\r\n",1000,DEBUG); //close session
  return epoch + 28800;
  }

String sendData(String command, const int timeout, boolean debug) {
  String response="";
  esp8266.print(command); // send the read character to the esp8266
  long int time=millis();
  while ((time+timeout) > millis()) {
    while(esp8266.available()) {
      // The esp has data so display its output to the serial window
      char c=esp8266.read(); // read the next character.
      response += c;
      }
    }
  if (debug) {Serial.print(response);}
  return response;
  }

這裡除了新增的 getWeek() 函數外, 其餘只是函數名稱更改與功能拆分而已. 分別呼叫 getDate() 與 getTime(), 以及 getWeek() 再去組合想要的輸出字串, 我覺得這樣比較好.

串列埠監控視窗擷取訊息如下 :

AT+RST


OK
bB�鑭b禔S��"��侒��S��
[Vendor:www.ai-thinker.com Version:0.9.2.4]

ready
AT+GMR

0018000902-AI03

OK
AT+CIFSR

192.168.2.101

OK
Sending request to NTP server ...
AT+CIPSTART="UDP","91.226.136.136",123


OK
AT+CIPSEND=48

>
NTP server answered :
E3 00 06 EC 00 00 00 00 00 00
00 00 31 4E 31 34 00 00 00 00
00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 0D 0A
53 45 4E 44 20 4F 4B 0D 0A !!0D
0A 2B 49 50 44 2C 34 38 3A 24
02 06 ED 00 00 00 29 00 00 05
EF FA 3D D8 B4 DB 37 5B 76 94
DD 57 89 00 00 00 00 00 00 00
00 DB 37 5C 46 81 C8 A2 A7 DB
37 5C 46 81 F6 99 1F 0D 0A 4F
4B 0D

123 bytes received
NTP time stamp packets (byte 101~104)=DB 37 5C 46
NTP time stamp (seconds since 1900-01-01)=3677838406
Unix time stamp (seconds since 1970-01-01)=1468849606
AT+CIPCLOSE


OK
Unlink
2016-07-18 21:46:46 Mon
2016-07-18 21:46:47 Mon
2016-07-18 21:46:48 Mon
2016-07-18 21:46:49 Mon
2016-07-18 21:46:50 Mon
2016-07-18 21:46:51 Mon
2016-07-18 21:46:52 Mon
2016-07-18 21:46:53 Mon
2016-07-18 21:46:54 Mon
2016-07-18 21:46:55 Mon
2016-07-18 21:46:56 Mon
2016-07-18 21:46:57 Mon
2016-07-18 21:46:58 Mon
2016-07-18 21:46:59 Mon
2016-07-18 21:47:00 Mon
2016-07-18 21:47:01 Mon
2016-07-18 21:47:02 Mon
2016-07-18 21:47:03 Mon
2016-07-18 21:47:04 Mon
2016-07-18 21:47:05 Mon
2016-07-18 21:47:06 Mon
2016-07-18 21:47:07 Mon
2016-07-18 21:47:08 Mon
2016-07-18 21:47:09 Mon
2016-07-18 21:47:10 Mon
2016-07-18 21:47:11 Mon
2016-07-18 21:47:12 Mon
2016-07-18 21:47:13 Mon
2016-07-18 21:47:14 Mon
2016-07-18 21:47:15 Mon
2016-07-18 21:47:16 Mon
2016-07-18 21:47:17 Mon
2016-07-18 21:47:18 Mon
2016-07-18 21:47:19 Mon
2016-07-18 21:47:20 Mon
2016-07-18 21:47:21 Mon
2016-07-18 21:47:22 Mon
2016-07-18 21:47:23 Mon
2016-07-18 21:47:24 Mon
2016-07-18 21:47:25 Mon
2016-07-18 21:47:26 Mon
2016-07-18 21:47:27 Mon
2016-07-18 21:47:28 Mon
2016-07-18 21:47:29 Mon
2016-07-18 21:47:30 Mon
2016-07-18 21:47:31 Mon
2016-07-18 21:47:32 Mon
2016-07-18 21:47:33 Mon
2016-07-18 21:47:34 Mon
2016-07-18 21:47:35 Mon
2016-07-18 21:47:36 Mon
2016-07-18 21:47:37 Mon
2016-07-18 21:47:38 Mon
2016-07-18 21:47:39 Mon
2016-07-18 21:47:40 Mon
2016-07-18 21:47:41 Mon
2016-07-18 21:47:42 Mon
2016-07-18 21:47:43 Mon
2016-07-18 21:47:44 Mon
2016-07-18 21:47:45 Mon
AT+CIPSTART="UDP","91.226.136.136",123


OK
AT+CIPSEND=48

>
NTP server answered :
E3 00 06 EC 00 00 00 00 00 00
00 00 31 4E 31 34 00 00 00 00
00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 0D 0A
53 45 4E 44 20 4F 4B 0D 0A !!0D
0A 2B 49 50 44 2C 34 38 3A 24
02 06 ED 00 00 00 29 00 00 06
33 FA 3D D8 B4 DB 37 5B 76 94
DD 57 89 00 00 00 00 00 00 00
00 DB 37 5C 8A DA 03 81 56 DB
37 5C 8A DA 0F A2 81 0D 0A 4F
4B 0D 0A

124 bytes received
NTP time stamp packets (byte 101~104)=DB 37 5C 8A
NTP time stamp (seconds since 1900-01-01)=3677838474
Unix time stamp (seconds since 1970-01-01)=1468849674
AT+CIPCLOSE


OK
Unlink
2016-07-18 21:47:54 Mon
2016-07-18 21:47:55 Mon
2016-07-18 21:47:56 Mon
2016-07-18 21:47:57 Mon
......


這樣加上星期資訊後感覺比較完整了.

但是, 如果能加上 DHT11 感測模組取得的溫濕度資訊就更完美了. 剛好 1602 LCD 顯示時間的第二排後面還有 8 個空白可用來顯示攝氏溫度與濕度, 位置分配如下 :

22:00:44 30° 59%

這樣剛剛好填滿第二排. 關於 DHT11 用法, 參考最近一次的實驗 :

# 以 Arduino+ESP8266 物聯網模組重作 DHT11 溫溼度實驗

在此溫濕度實驗中, 使用了 D2 腳作為讀取 DHT11 資料輸出之用, 但因為 1602 使用了 D2 連接其 RS 腳, 因此 DHT11 輸入我另外找了 D6 來讀取. 注意, 下面的程式需要到 Github 下載 Adafruit 的 DHT11/22 函式庫到 Arduino 安裝目錄下的 library 資料夾下 (然後 Arduino IDE 必須重開才抓得到) :

adafruit/DHT-sensor-library

測試 7 :

#include <SoftwareSerial.h>
#define DEBUG true

#include <Time.h>
#include <TimeAlarms.h>
#include <LiquidCrystal.h>
#define RS 2
#define E 3
#define D4 10
#define D5 11
#define D6 12
#define D7 13

#include "DHT.h"
#define DHTPIN 6   
#define DHTTYPE DHT11

SoftwareSerial esp8266(7,8); //(RX,TX)
byte packetBuffer[128]; //buffer : send & recv data to/from NTP
LiquidCrystal lcd(RS,E,D4,D5,D6,D7);  //create LCD object
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor

void setup() {
  Serial.begin(9600);
  esp8266.begin(9600);
  sendData("AT+RST\r\n",2000,DEBUG); // reset ESP8266
  sendData("AT+GMR\r\n",1000,DEBUG);
  delay(3000); //wait for wifi connection to get local ip
  sendData("AT+CIFSR\r\n",1000,DEBUG); //get ip address
  Serial.println("Sending request to NTP server ...");
  sync_clock();
  Alarm.timerRepeat(60, sync_clock);
  lcd.begin(16,2); //define 2*16 LCD
  lcd.clear(); //clear screen
  }

void loop() {
  String d=getDate();
  String t=getTime();
  String w=getWeek();
  Serial.println(d + " " + t + " " + w);
  lcd.setCursor(0,0); //move to (x,y)
  lcd.print(d); //print date
  lcd.setCursor(11,0); //move to (x,y)
  lcd.print(w); //print date  
  lcd.setCursor(0,1); //move to (x,y)
  lcd.print(t); //print date  
  float h=dht.readHumidity();        //get humidity from DHT11
  float c=dht.readTemperature();     //get temperature (C) from DHT11 
  if (isnan(h) || isnan(c)) {h=0.0;c=0.0;}
  lcd.setCursor(9,1); //move to (x,y)
  lcd.print(int(c)); //print celcius  
  lcd.setCursor(11,1); //move to (x,y)
  lcd.print((char)223); //print degree symbol
  lcd.setCursor(12,1); //move to (x,y)
  lcd.print(' '); //print space   
  lcd.setCursor(13,1); //move to (x,y)
  lcd.print(int(h)); //print humidity 
  lcd.setCursor(15,1); //move to (x,y)
  lcd.print('%'); //print %      
  Alarm.delay(1000);
  }

void sync_clock() {
  setTime(getCST());
  }

String getDate() {
  String d=(String)year() + "-";
  byte M=month();
  if (M < 10) {d.concat('0');}
  d.concat(M);
  d.concat('-');
  byte D=day();
  if (D < 10) {d.concat('0');}
  d.concat(D);
  return d;
  }

String getTime() {
  String t="";
  byte h=hour();
  if (h < 10) {t.concat('0');}
  t.concat(h);
  t.concat(':');
  byte m=minute();
  if (m < 10) {t.concat('0');}
  t.concat(m);
  t.concat(':');
  byte s=second();
  if (s < 10) {t.concat('0');}
  t.concat(s);
  return t;
  }

String getWeek() {
  String w[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  return w[weekday()-1];
  }

long getCST() {
  //Start UDP Rrequest to NTP Server 91.226.136.136, 82.209.243.241,192.5.41.40
  //132.163.4.101, 132.163.4.102, 132.163.4.103
  sendData("AT+CIPSTART=\"UDP\",\"91.226.136.136\",123\r\n",5000,DEBUG);
  memset(packetBuffer,0,128); //clear buffer
  packetBuffer[0]=0xE3;       //LI, Version, Mode
  packetBuffer[1]=0x00;       //Stratum, or type of clock
  packetBuffer[2]=0x06;       //Polling Interval
  packetBuffer[3]=0xEC;       //Peer Clock Precision
  packetBuffer[12]=0x31;      //reference ID (4 bytes)
  packetBuffer[13]=0x4E;
  packetBuffer[14]=0x31;
  packetBuffer[15]=0x34;
  //send request to NTP Server
  sendData("AT+CIPSEND=48\r\n",1000,DEBUG); //send data length
  for (byte i=0; i < 48; i++) {
    esp8266.write(packetBuffer[i]);
    delay(5);
    }

  //deal with NTP response
  memset(packetBuffer,0,128); //clear buffer to store NTP response
  Serial.println();
  Serial.println("NTP server answered : ");
  int i=0; //packet byte counter
  while (esp8266.available() > 0) { //if receive NTP response : fall in loop
    byte ch=esp8266.read(); //got NTP response, read one byte for each loop
    if (i < 128) {packetBuffer[i]=ch;} //store received byte to packet buffer
    //show receving bytes in hex
    if (ch < 0x10) {Serial.print('0');} //prefix with '0' if byte value 0~9
    Serial.print(ch, HEX);
    Serial.print(' ');
    if ((((i+1) % 10) == 0)) {Serial.println();} //newline if exceeds 10 bytes
    delay(5); //wait 5ms for next incoming byte
    i++; //increment packet byte counter
    if ((i < 104) && (esp8266.available() == 0)) { //wainting if lags
      //Response packets not enough but no response : wait 1.5 seconds
      byte wcount=0; //waiting counter
      while (esp8266.available() == 0) { //loop until timeout (1.5 seconds)
        Serial.print("!"); //show ! means waiting for response packet
        delay(100);
        wcount += 1; //increment waiting counter
        if (wcount >= 15) {break;} //waiting timeout : quit loop
        }
      }
    }
  Serial.println();
  Serial.println();
  Serial.print(i+1);
  Serial.println(" bytes received"); // will be more than 48
  //Show time stamp (locates from byte 101~104 of the response packet)
  Serial.print("NTP time stamp packets (byte 101~104)=");
  Serial.print(packetBuffer[101],HEX);
  Serial.print(" ");
  Serial.print(packetBuffer[102],HEX);
  Serial.print(" ");
  Serial.print(packetBuffer[103],HEX);
  Serial.print(" ");
  Serial.print(packetBuffer[104],HEX);
  Serial.println();

  //handling time packets (4 bytes long) : combine them into words
  unsigned long highWord=word(packetBuffer[101],packetBuffer[102]);
  unsigned long lowWord=word(packetBuffer[103],packetBuffer[104]);
  //shift high word 16 bits left & OR with low word to form a double word
  //the result is a NTP time stamp (seconds since Jan 1 1900):
  unsigned long secsSince1900=highWord << 16 | lowWord;
  Serial.print("NTP time stamp (seconds since 1900-01-01)=");
  Serial.println(secsSince1900);

  //convert NTP time stamp to Unix time stamp :
  //Unix time starts on Jan 1 1970=2208988800 seconds since 1900-01-01
  //subtract seventy years to get Unix time stamp, plus 28800 (UTC+8)
  unsigned long epoch=secsSince1900 - 2208988800UL;
  Serial.print("Unix time stamp (seconds since 1970-01-01)=");
  Serial.println(epoch); //print Unix time
  sendData("AT+CIPCLOSE\r\n",1000,DEBUG); //close session
  return epoch + 28800;
  }

String sendData(String command, const int timeout, boolean debug) {
  String response="";
  esp8266.print(command); // send the read character to the esp8266
  long int time=millis();
  while ((time+timeout) > millis()) {
    while(esp8266.available()) {
      // The esp has data so display its output to the serial window
      char c=esp8266.read(); // read the next character.
      response += c;
      }
    }
  if (debug) {Serial.print(response);}
  return response;
  }

新加上的程式碼以藍色表示, 這裡要注意的是 1602 可以顯示如攝氏度數符號 ° 等等的特殊符號, 其內碼 (pattern code) 可參考 1602 內的 HD44780 晶片規格書之 Page 17 :

https://www.sparkfun.com/datasheets/LCD/HD44780.pdf

可知攝氏度數符號之內碼為 B11011111 或 0xDF, 化成十進制即 223 也. 但在使用 lcd.print() 輸出前必須使用 char() 予以強制轉型為字元. 關於 1602 的特殊符號也可參考  "Arduino Cookbook 錦囊妙計" 這本書的 11.5 節. 不過我照其範例直接輸出 B11011111 或 0xDF 或 223 都無法顯示攝氏度數符號, 後來找到下列資訊才知道要用 char() 轉型 :

http://forum.arduino.cc/index.php?topic=19002.0


這樣與網路同步的日期時間以及溫濕度資訊都完整呈現於 1602 顯示器, 這部分的實驗到這裡總算告一段落啦! TimeTimeAlarms 函式庫還有很多好用函式, 以後有用到或有空再來測試囉.

2016-08-05 :

NTP 回應訊息的擷取方式已改版, 參見 :

# 利用 NTP 伺服器來同步 Arduino 系統時鐘 (三)



9 則留言 :

梁淑媛 提到...

黃先生你好,

根據你的 NTP 伺服器來同步 Arduino 系統時鐘 (二),把你的程式燒入Arduino 一切都正常。但是時間日期都不對。而且只收到87 bytes. 不曉得是哪裡出錯。謝謝指點!

Sending request to NTP server ...
AT+CIPSTART="UDP","91.226.136.136",123

CONNECT

OK
AT+CIPSEND=48

OK
>
NTP server answered :
0D 0A 52 65 63 76 20 34 38 20
62 79 74 65 73 0D 0A 0D 0A 53
45 4E 44 20 4F 4B 0D 0A !!!0D 0A
2B 49 50 44 2C 34 38 3A 24 02
06 ED 00 00 00 4B 00 00 08 77
79 F9 80 85 DB 47 36 F0 95 9B
A1 86 00 00 00 00 00 00 00 00
DB 47 3C 42 E8 25 57 0D DB 47
3C 42 E8 2E 73 7E !!!!!!!!!!!!!!!

87 bytes received
NTP time stamp packets (byte 101~104)=0 0 0 0
NTP time stamp (seconds since 1900-01-01)=0
Unix time stamp (seconds since 1970-01-01)=2085978496
AT+CIPCLOSE

CLOSED

OK
2036-02-07 14:28:16 Thu
2036-02-07 14:28:17 Thu
2036-02-07 14:28:18 Thu
2036-02-07 14:28:19 Thu

小狐狸事務所 提到...

從 NTP time stamp packets (byte 101~104)=0 0 0 0 看有可能是 NTP 沒有回應正確時間, 建議您換另一個 NTP 伺服器 IP 看看. 另外序列埠緩衝器應該有從預設的 64 BYTES 改為 128 BYTES 吧? 參考 :
http://yhhuang1966.blogspot.tw/2016/06/arduino-esp8266-ntp.html

梁淑媛 提到...

謝謝回覆。已經有修改緩衝器為128 bytes.
修改的路徑如下:Contents->Java->Hardware->arduino->avr->libraries->SoftwareSerial->src->SoftwareSerial.h
或是我修改到錯誤的路徑。但是我用你的另外一個程式來跑,卻是可以顯示現在的時間。所以NTP server 應該有回應。只是對以NTP傳回來的東西。對我來說,還是很新。雖然有爬過文,還不是很了解。

Firmware version ...
Working mode ... OK
IP ... +CIFSR:STAIP,"192.168.1.31"
+CIFSR:STAMAC,"5c:cf:7f:16:77:4c"
register udp ok
The CCT time is 22:57:28
unregister udp ok
register udp ok
The CCT time is 22:57:39
unregister udp ok
register udp ok
The CCT time is 22:57:50
unregister udp ok
register udp ok



梁淑媛 提到...

謝謝回覆。有修改緩衝器為128 bytes. 可是沒有效。相同的問題。哪裡還需要注意的嗎?
有另外一個程式,他是可以抓到NTP的時間。所以NPT應該是沒有問題。
只是不曉得這個程式,為什麼沒有收到後續的資料。

梁淑媛 提到...

測試結果如下:

Firmware version ... Firmware version ...
Working mode ... OK
IP ... +CIFSR:STAIP,"192.168.1.30"
+CIFSR:STAMAC,"5c:cf:7f:16:7a:c8"
register udp ok
The CCT time is 00:51:24
unregister udp ok
register udp ok
The CCT time is 00:51:35
unregister udp ok
register udp ok
The CCT time is 00:51:46
unregister udp ok

小狐狸事務所 提到...

這樣看起來是正常囉!

梁淑媛 提到...

黃先生你好,這個程式是可以讀得到NTP的時間,可是前一個程式還是有問題。不曉得要怎麼找問題。日期時間和收到的bytes 都不對。因為對NTP送出來的資料並不是很了解。所以也不知道怎麼debug. 可以指點一下迷津。謝謝!

梁淑媛 提到...

發現我的留言會消失,好幾次發言都不見了。昨天有留言,今天也是不見了。
方便留下email 可以向你請教NTP的問題。
第一次試做的程式,還是有問題,不曉得要怎麼去debug.

小狐狸事務所 提到...

最近 blogger 確實怪怪的, 我已經針對 NTP 程式進行改版, 參考 :

http://yhhuang1966.blogspot.tw/2016/08/ntp.html