ESP32编程


NTP

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

local 2021年11月12日 17:53 收藏文档