基于 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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值