基于 ESP32 - S3 同步 NTP(网络时间协议)并获取系统时间的 C 语言程序例程

以下是一个基于 ESP32 - S3 同步 NTP(网络时间协议)并获取系统时间的 C 语言程序例程。这个例程使用了 ESP-IDF(Espressif IoT Development Framework)来实现,你需要在 ESP-IDF 环境下编译和运行该程序。

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_event.h"
#include "protocol_examples_common.h"
#include "lwip/apps/sntp.h"

static const char *TAG = "ntp_example";

// 初始化 SNTP(简单网络时间协议)
static void initialize_sntp(void)
{
    ESP_LOGI(TAG, "Initializing SNTP");
    sntp_setoperatingmode(SNTP_OPMODE_POLL);
    sntp_setservername(0, "pool.ntp.org");
    sntp_init();
}

// 等待时间同步
static void obtain_time(void)
{
    // 初始化网络
    ESP_ERROR_CHECK(example_connect());

    // 初始化 SNTP
    initialize_sntp();

    // 等待时间同步
    time_t now = 0;
    struct tm timeinfo = { 0 };
    int retry = 0;
    const int retry_count = 10;
    while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
        ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }

    // 获取当前时间
    time(&now);
    localtime_r(&now, &timeinfo);
}

void app_main(void)
{
    // 初始化 NVS(非易失性存储)
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    // 初始化 TCP/IP 栈
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    // 同步时间
    obtain_time();

    // 打印当前时间
    time_t now;
    struct tm timeinfo;
    time(&now);
    localtime_r(&now, &timeinfo);
    char strftime_buf[64];
    strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
    ESP_LOGI(TAG, "Current time: %s", strftime_buf);

    // 可以在这里添加更多的逻辑,例如定时获取时间等
    while (1) {
        vTaskDelay(5000 / portTICK_PERIOD_MS);
        time(&now);
        localtime_r(&now, &timeinfo);
        strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
        ESP_LOGI(TAG, "Updated time: %s", strftime_buf);
    }
}


代码说明:

  1. 初始化 NVS:在 app_main 函数中,首先初始化非易失性存储(NVS),用于存储网络配置等信息。
  2. 初始化 TCP/IP 栈:使用 esp_netif_init 和 esp_event_loop_create_default 初始化 TCP/IP 栈和事件循环。
  3. 同步时间
    • obtain_time 函数负责初始化网络连接和 SNTP 客户端。
    • initialize_sntp 函数设置 SNTP 工作模式为轮询模式,并指定 NTP 服务器为 pool.ntp.org
    • 通过 sntp_get_sync_status 检查时间同步状态,最多重试 10 次,每次间隔 2 秒。
  4. 获取和打印时间:使用 time 函数获取当前时间,使用 localtime_r 函数将时间转换为本地时间,使用 strftime 函数将时间格式化为字符串并打印。
  5. 定时更新时间:在一个无限循环中,每隔 5 秒更新并打印一次时间。

编译和运行:

  1. 确保你已经安装了 ESP-IDF 开发环境。
  2. 将上述代码保存为 main.c 文件,并放置在 ESP-IDF 项目的 main 目录下。
  3. 在项目根目录下运行 idf.py menuconfig 配置网络连接信息(如 Wi-Fi SSID 和密码)。
  4. 运行 idf.py build 编译项目。
  5. 运行 idf.py -p /dev/ttyUSB0 flash monitor(根据实际情况修改串口设备名)烧录并监控程序运行。

这样,ESP32 - S3 就会连接到 Wi-Fi 网络,同步 NTP 时间,并定期打印系统时间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值