久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2724|回復: 1
打印 上一主題 下一主題
收起左側

【零知ESP8266教程】快速入門19 使用NTP服務器獲取網絡時間

[復制鏈接]
跳轉到指定樓層
樓主
ID:349555 發(fā)表于 2019-11-4 11:27 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最后由 roc2 于 2019-11-4 11:29 編輯

上次的分享,我們獲取了一枚天氣信息,但是呢,信息最重要的屬性之一就是時效性,如何去確認我們信息的時效性呢,簡單,保持自己時間的準確嘍。

Time is valuable thing!

時間對我們是很重要的。通常,我們需要進行時間校準(PS:在很多時候,你細心留意就發(fā)現時間是錯誤的),一般就可以用NTP服務器進行獲取時間信息,下面使用零知-ESP8266上進行NTP時間的獲取。

一、硬件
電腦,windows系統(tǒng)
零知ESP8266開發(fā)板
micro-usb線
二、
(1)打開零知開源開發(fā)軟件,選擇零知ESP8266開發(fā)板,界面如圖所示:


(2)電腦連接零知ESP8266開發(fā)板

(3)燒寫程序:

①可以直接選擇軟件中的示例程序


②也可以上傳以下代碼:

  1. <font color="rgb(77, 77, 77)"><font face="&quot;"><font style="font-size: 16px">/*
  2. * TimeNTP_ESP8266WiFi.ino
  3. * Example showing time sync to NTP time source
  4. *
  5. * This sketch uses the ESP8266WiFi library
  6. */
  7.   
  8. #include <TimeLib.h>
  9. #include <ESP8266WiFi.h>
  10. #include <WiFiUdp.h>
  11.   
  12. const char ssid[] = "xx";  //  your network SSID (name)
  13. const char pass[] = "xx";       // your network password
  14.   
  15. // NTP Servers:
  16. static const char ntpServerName[] = "cn.ntp.org.cn";
  17. //static const char ntpServerName[] = "time.nist.gov";
  18. //static const char ntpServerName[] = "time-a.timefreq.bldrdoc.gov";
  19. //static const char ntpServerName[] = "time-b.timefreq.bldrdoc.gov";
  20. //static const char ntpServerName[] = "time-c.timefreq.bldrdoc.gov";
  21.   
  22. const int timeZone = 8; //BeiJing in China
  23.   
  24. // const int timeZone = 1;     // Central European Time
  25. //const int timeZone = -5;  // Eastern Standard Time (USA)
  26. //const int timeZone = -4;  // Eastern Daylight Time (USA)
  27. //const int timeZone = -8;  // Pacific Standard Time (USA)
  28. //const int timeZone = -7;  // Pacific Daylight Time (USA)
  29.   
  30.   
  31. WiFiUDP Udp;
  32. unsigned int localPort = 8888;  // local port to listen for UDP packets
  33.   
  34. time_t getNtpTime();
  35. void digitalClockDisplay();
  36. void printDigits(int digits);
  37. void sendNTPpacket(IPAddress &address);
  38.   
  39. void setup()
  40. {
  41.   Serial.begin(9600);
  42.   while (!Serial) ; // Needed for Leonardo only
  43.   delay(250);
  44.   Serial.println("TimeNTP Example");
  45.   Serial.print("Connecting to ");
  46.   Serial.println(ssid);
  47.   WiFi.begin(ssid, pass);
  48.   
  49.   while (WiFi.status() != WL_CONNECTED) {
  50.     delay(500);
  51.     Serial.print(".");
  52.   }
  53.   
  54.   Serial.print("IP number assigned by DHCP is ");
  55.   Serial.println(WiFi.localIP());
  56.   Serial.println("Starting UDP");
  57.   Udp.begin(localPort);
  58.   Serial.print("Local port: ");
  59.   Serial.println(Udp.localPort());
  60.   Serial.println("waiting for sync");
  61.   setSyncProvider(getNtpTime);
  62.   setSyncInterval(300);
  63. }
  64.   
  65. time_t prevDisplay = 0; // when the digital clock was displayed
  66.   
  67. void loop()
  68. {
  69.   if (timeStatus() != timeNotSet) {
  70.     if (now() != prevDisplay) { //update the display only if time has changed
  71.       prevDisplay = now();
  72.       digitalClockDisplay();
  73.     }
  74.   }
  75. }
  76.   
  77. void digitalClockDisplay()
  78. {
  79.   // digital clock display of the time
  80.   Serial.print(hour());
  81.   printDigits(minute());
  82.   printDigits(second());
  83.   Serial.print(" ");
  84.   Serial.print(day());
  85.   Serial.print(".");
  86.   Serial.print(month());
  87.   Serial.print(".");
  88.   Serial.print(year());
  89.   Serial.println();
  90. }
  91.   
  92. void printDigits(int digits)
  93. {
  94.   // utility for digital clock display: prints preceding colon and leading 0
  95.   Serial.print(":");
  96.   if (digits < 10)
  97.     Serial.print('0');
  98.   Serial.print(digits);
  99. }
  100.   
  101. /*-------- NTP code ----------*/
  102.   
  103. const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
  104. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
  105.   
  106. time_t getNtpTime()
  107. {
  108.   IPAddress ntpServerIP; // NTP server's ip address
  109.   
  110.   while (Udp.parsePacket() > 0) ; // discard any previously received packets
  111.   Serial.println("Transmit NTP Request");
  112.   // get a random server from the pool
  113.   WiFi.hostByName(ntpServerName, ntpServerIP);
  114.   Serial.print(ntpServerName);
  115.   Serial.print(": ");
  116.   Serial.println(ntpServerIP);
  117.   sendNTPpacket(ntpServerIP);
  118.   uint32_t beginWait = millis();
  119.   while (millis() - beginWait < 1500) {
  120.     int size = Udp.parsePacket();
  121.     if (size >= NTP_PACKET_SIZE) {
  122.       Serial.println("Receive NTP Response");
  123.       Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
  124.       unsigned long secsSince1900;
  125.       // convert four bytes starting at location 40 to a long integer
  126.       secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
  127.       secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
  128.       secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
  129.       secsSince1900 |= (unsigned long)packetBuffer[43];
  130.       return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
  131.     }
  132.   }
  133.   Serial.println("No NTP Response :-(");
  134.   return 0; // return 0 if unable to get the time
  135. }
  136.   
  137. // send an NTP request to the time server at the given address
  138. void sendNTPpacket(IPAddress &address)
  139. {
  140.   // set all bytes in the buffer to 0
  141.   memset(packetBuffer, 0, NTP_PACKET_SIZE);
  142.   // Initialize values needed to form NTP request
  143.   // (see URL above for details on the packets)
  144.   packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  145.   packetBuffer[1] = 0;     // Stratum, or type of clock
  146.   packetBuffer[2] = 6;     // Polling Interval
  147.   packetBuffer[3] = 0xEC;  // Peer Clock Precision
  148.   // 8 bytes of zero for Root Delay & Root Dispersion
  149.   packetBuffer[12] = 49;
  150.   packetBuffer[13] = 0x4E;
  151.   packetBuffer[14] = 49;
  152.   packetBuffer[15] = 52;
  153.   // all NTP fields have been given values, now
  154.   // you can send a packet requesting a timestamp:
  155.   Udp.beginPacket(address, 123); //NTP requests are to port 123
  156.   Udp.write(packetBuffer, NTP_PACKET_SIZE);
  157.   Udp.endPacket();
  158. }
  159. </font></font></font>
復制代碼


然后驗證,并且上傳到開發(fā)板。

四、結果



然后對照自己的時間:



趕緊動手檢查看看自己的時間對不對呢?
我先校準自己的。

順便問你們一句:有沒有發(fā)現自己的時間變快了呢?(細思極恐)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:674270 發(fā)表于 2020-2-24 00:22 | 只看該作者
對啊 變快了 什么情況
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产精品1区2区 | 中文字幕免费视频 | 91影院在线观看 | 色综合国产 | 国产成人一区二区三区精 | 中文字幕亚洲精品 | 国产精品永久免费视频 | 在线日韩欧美 | 久久久女女女女999久久 | 国产色婷婷精品综合在线播放 | 欧美日韩在线观看一区 | 久久九精品 | 亚洲精品在线观看视频 | 羞羞的视频免费观看 | 日本h片在线观看 | 美国av毛片 | 九九视频在线观看 | 国产一区二区三区免费观看在线 | 久久精品久久精品久久精品 | 人人爱干 | 精品伊人久久 | 天堂av免费观看 | 日韩一区二区三区在线播放 | 国内精品久久精品 | 国产精品亚洲欧美日韩一区在线 | 欧美日韩免费一区二区三区 | 成人小视频在线观看 | 少妇淫片aaaaa毛片叫床爽 | 亚洲欧美一区二区三区情侣bbw | 午夜tv免费观看 | aaa综合国产 | 亚洲欧美在线视频 | 成人在线播放网站 | 粉嫩av久久一区二区三区 | 国产福利在线小视频 | 在线视频h | 午夜一级做a爰片久久毛片 精品综合 | 亚洲精品乱码8久久久久久日本 | 天堂视频一区 | 久久99精品久久久 | 色综合99|