NTP
#include <NTPClient.h>
// change next line to use with another board/shield
//#include <ESP8266WiFi.h>
#include <WiFi.h> // ESP32开发板使用
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>
const char *ssid = "a1";
const char *password = "12345678";
WiFiUDP ntpUDP;
// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
//翻译://您可以指定时间服务器池和偏移量(以秒为单位,稍后可以使用setTimeOffset()进行更改)。
//此外,还可以指定更新间隔(以毫秒为单位,可以使用setUpdateInterval()进行更改)。
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 28800, 300000);
//北京时区 GMT +8 = 28800
void setup(){
Serial.begin(9600);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
//timeClient.setTimeOffset(28800); //60min*60s*8=28800,偏移量,北京时区+8
//setUpdateInterval(300000); //5min*60s*1000ms=300000,更新间隔设置为5分钟
timeClient.update();
}
void loop() {
timeClient.update(); //联网更新时间
Serial.println(timeClient.getFormattedTime());
delay(1000);
}