#include <WiFi.h>
#include <HTTPClient.h>
#include <time.h>
const char* ssid = "esp32";
const char* password = "guanzhuyixia!";
const char* ntpServer = "pool.ntp.org";
const long utcOffsetInSeconds = 28800; // Beijing: UTC +8 -- 获取东八区时间(默认以英国格林威治天文台所在地的本初子午线为基准线的)
void setup() {
Serial.begin(115200);
while (!Serial);
//连接wifi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
//获取时间
configTime(utcOffsetInSeconds, 0, ntpServer);
while (!time(nullptr)) {
delay(1000);
Serial.println("Waiting for time sync...");
}
Serial.println("Time synced successfully");
}
void loop() {
time_t now = time(nullptr);
Serial.print("Current time is: ");
Serial.println(ctime(&now)); //打印时间
// Convert current time to Unix timestamp
long unixTimestamp = static_cast<long>(now); //获取unix时间戳
Serial.print("Unix timestamp is: ");
Serial.println(unixTimestamp);
delay(1000);
}
————————————————
版权声明:本文为CSDN博主「予非鱼安知鱼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hongyun1221/article/details/130013819
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1