NTP 服务器没有得到正确的日期 (ESP8266)
Posted
技术标签:
【中文标题】NTP 服务器没有得到正确的日期 (ESP8266)【英文标题】:NTP server doesn’t get the right date (ESP8266) 【发布时间】:2022-01-23 19:46:05 【问题描述】:我正在开发一个基于 ESP8266 模块的小项目,通过 NTP 服务器获取日期和时间,因为 RTC 模块不适合我。我已经测试了一个代码,我在电路中使用 NTPClient 库获取日期和时间。
我的系统可以很好地打印时间,它是准确的,并且与我所在国家/地区的时区同步。问题在于日期,因为它获取的数据不一致(并且可能是随机的)。
我的意思是,每次我将代码加载到我的板上时,我都会得到不同的月、日和年,就像它是随机的一样,所以我不能只使用“偏移量”来设置日期,因为我不'不知道下次我加载它是否会是相同的数字(它可以打印当前月份是一月,九月,可能......)。
来自 Seril Monitor 的一些示例如下:
06:15:16.430 -> Epoch Time: 1640153824
06:15:16.430 -> Formatted Time: 06:17:04
06:15:16.430 -> Hour: 6
06:15:16.430 -> Minutes: 17
06:15:16.430 -> Seconds: 4
06:15:16.430 -> Week Day: Wednesday
06:15:16.430 -> Month day: 17
06:15:16.430 -> Month: 12
06:15:16.430 -> Month name: December
06:15:16.430 -> Year: 1339088
06:15:16.430 -> Current date: 1339088-12-17
顺便说一句,我尝试将 NTP 服务器“链接”更改为“pool.ntp.org”以外的其他内容,更改为“us.pool.ntp.org”、“fr.pool.ntp.org”等其他很多,但结果是一样的......
所以,我想知道,关于日期的代码有什么问题,我可以做些什么来改进它?或者如果我可以更改 NTP 服务器...
这是我当前的代码:
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// Replace with your network credentials
const char *ssid = "mySSID";
const char *password = "mypass";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
//Week Days
String weekDays[7]="Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday";
//Month names
String months[12]="January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December";
void setup()
// Initialize Serial Monitor
Serial.begin(115200);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
delay(500);
Serial.print(".");
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(-14400);
void loop()
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
Serial.print("Epoch Time: ");
Serial.println(epochTime);
String formattedTime = timeClient.getFormattedTime();
Serial.print("Formatted Time: ");
Serial.println(formattedTime);
int currentHour = timeClient.getHours();
Serial.print("Hour: ");
Serial.println(currentHour);
int currentMinute = timeClient.getMinutes();
Serial.print("Minutes: ");
Serial.println(currentMinute);
int currentSecond = timeClient.getSeconds();
Serial.print("Seconds: ");
Serial.println(currentSecond);
String weekDay = weekDays[timeClient.getDay()];
Serial.print("Week Day: ");
Serial.println(weekDay);
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
Serial.print("Month day: ");
Serial.println(monthDay);
int currentMonth = ptm->tm_mon+1;
Serial.print("Month: ");
Serial.println(currentMonth);
String currentMonthName = months[currentMonth-1];
Serial.print("Month name: ");
Serial.println(currentMonthName);
int currentYear = ptm->tm_year+1900;
Serial.print("Year: ");
Serial.println(currentYear);
//Print complete date:
String currentDate = (String) currentYear + "-" + (String) currentMonth + "-" + (String) monthDay;
Serial.print("Current date: ");
Serial.println(currentDate);
Serial.println();
delay(1000);
【问题讨论】:
【参考方案1】:这是 esp8266 上最新版本的 NTPClient 库中的一个错误。
降级到 NTPClient 的 2.7.4 显示为 resolve the issue for some users。
见https://github.com/arduino-libraries/NTPClient/issues/149
【讨论】:
以上是关于NTP 服务器没有得到正确的日期 (ESP8266)的主要内容,如果未能解决你的问题,请参考以下文章
ESP8266(ESP12F)学习笔记2 -- NTP网络时间获取
ESP8266/ESP32 通过TimeLib库获取NTP时间方法