以下是使用ESP8266获取当前时间并通过串口打印的示例代码:

#include <ESP8266WiFi.h>
#include <TimeLib.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

void setup() {
  Serial.begin(9600);
  while (!Serial); // wait for serial port to connect
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  configTime(0, 0, "pool.ntp.org"); // sync time with NTP server
}

void loop() {
  time_t now = time(nullptr);
  Serial.println(ctime(&now)); // print current time
  delay(1000);
}

在这个例子中,我们首先连接到WiFi网络,然后使用configTime()函数与NTP服务器同步时间。在主循环中,我们使用time()函数获取当前时间,并使用ctime()函数将其转换为字符串格式并通过串口打印。我们还添加了一个延迟以防止串口输出过快。

esp8266获取当前时间通过串口打印给我代码

原文地址: http://www.cveoy.top/t/topic/bfw9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录