娇小w搡bbbb搡bbb,《第一次の人妻》,中国成熟妇女毛茸茸,边啃奶头边躁狠狠躁视频免费观看

ESP32學習筆記(41)——SNTP接口使用

發布者:EtherealBeauty最新更新時間:2025-02-26 來源: jianshu關鍵字:ESP32  網絡時間協議 手機看文章 掃描二維碼
隨時隨地手機看文章

一、SNTP簡介

簡單網絡時間協議(Simple Network Time Protocol),由 NTP 改編而來,主要用來同步因特網中的計算機時鐘

SNTP 協議是用來同步本地的時間到 unix 時間戳。通常嵌入式設備上電,連接 AP(access point),獲取 IP 地址后,就需要使用 SNTP 協議獲取全球時間。以便于下一步的應用交互和使用。
SNTP 工作原理比較簡單, 通俗來說,就是設備向 SNTP server 發送一包 SNTP 請求,服務器收到請求后回復一包 SNTP reply。其中 SNTP reply 中就含有 unix 時間戳。

ESP-IDF 編程指南——SNTP 時間同步

二、API說明

以下 SNTP 接口位于 lwip/include/apps/esp_sntp.h。

2.1 sntp_setoperatingmode

2.2 sntp_setservername

2.3 sntp_set_time_sync_notification_cb

2.4 sntp_init

2.5 sntp_get_sync_status

三、示例代碼

根據 examplesprotocolssntp 中的例程修改

在 menuconfig 中配置 SSID 和密碼

核心部分:

//設置單播模式

sntp_setoperatingmode(SNTP_OPMODE_POLL);

//設置訪問服務器

sntp_setservername(0, 'pool.ntp.org');

//初始化SNTP模塊

sntp_init();


完整代碼:

/* LwIP SNTP example


   This example code is in the Public Domain (or CC0 licensed, at your option.)


   Unless required by applicable law or agreed to in writing, this

   software is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR

   CONDITIONS OF ANY KIND, either express or implied.

*/

#include

#include

#include

#include 'freertos/FreeRTOS.h'

#include 'freertos/task.h'

#include 'freertos/event_groups.h'

#include 'esp_system.h'

#include 'esp_event.h'

#include 'esp_log.h'

#include 'esp_attr.h'

#include 'esp_sleep.h'

#include 'nvs_flash.h'

#include 'protocol_examples_common.h'

#include 'esp_sntp.h'


static const char *TAG = 'example';


static void obtain_time(void);

static void initialize_sntp(void);


void time_sync_notification_cb(struct timeval *tv)

{

    ESP_LOGI(TAG, 'Notification of a time synchronization event');

}


void app_main(void)

{

    time_t now;

    struct tm timeinfo;

    time(&now);

    localtime_r(&now, &timeinfo);

    // Is time set? If not, tm_year will be (1970 - 1900).

    if (timeinfo.tm_year < (2021 - 1900)) {

        ESP_LOGI(TAG, 'Time is not set yet. Connecting to WiFi and getting time over NTP.');

        obtain_time();

        // update 'now' variable with current time

        time(&now);

    }


    char strftime_buf[64];


    while (1)

    {

        // update 'now' variable with current time

        time(&now);


        // Set timezone to China Standard Time

        setenv('TZ', 'CST-8', 1);

        tzset();

        localtime_r(&now, &timeinfo);

        strftime(strftime_buf, sizeof(strftime_buf), '%c', &timeinfo);

        ESP_LOGI(TAG, 'The current date/time in Shanghai is: %s', strftime_buf);


        vTaskDelay(10000 / portTICK_PERIOD_MS);

    }

}


static void obtain_time(void)

{

    ESP_ERROR_CHECK( nvs_flash_init() );

    ESP_ERROR_CHECK(esp_netif_init());

    ESP_ERROR_CHECK( esp_event_loop_create_default() );


    /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.

     * Read 'Establishing Wi-Fi or Ethernet Connection' section in

     * examples/protocols/README.md for more information about this function.

     */

    ESP_ERROR_CHECK(example_connect());


    initialize_sntp();


    // wait for time to be set

    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);


    ESP_ERROR_CHECK( example_disconnect() );

}


static void initialize_sntp(void)

{

    ESP_LOGI(TAG, 'Initializing SNTP');

    sntp_setoperatingmode(SNTP_OPMODE_POLL);

    sntp_setservername(0, 'pool.ntp.org');

    sntp_set_time_sync_notification_cb(time_sync_notification_cb);

    sntp_init();

}


查看打印:

四、注意事項

  1. sntp_setservername 除了可以設置域名, 也可以設置 IP 地址, 例如 sntp_setservername(0, '120.25.115.20');

  2. 如果有必要, 請多設置幾個 SNTP server,防止某個 SNTP server 暫時關閉服務而導致產品部分功能無法使用, 例如:

sntp_setservername(0, 'ntp1.aliyun.com');

sntp_setservername(1, '210.72.145.44');     // 國家授時中心服務器 IP 地址

sntp_setservername(2, '1.cn.pool.ntp.org');    


說明:
默認情況下, ESP8266/ESP32 只允許開啟一個 SNTP server(節省資源考慮), 如果用戶需開啟多個 SNTP server, 請配置:

  • ESP8266 請在 make menuconfig -> Component config -> LWIP -> DHCP -> Maximum bumber of NTP servers 修改為 3

  • ESP32 請在 make menuconfig -> Component config -> LWIP -> SNTP -> Maximum bumber of NTP servers 修改為 3

配置多個 SNTP server 時, 不是同時發送多個 SNTP 請求報文, 而是輪循方式. 第一個處理超時后, 進行和第二個 SNTP server 交互, 這樣依次進行到最后一個, 最后一個處理超時后, 會再和第一個 SNTP server 交互

  1. 更新時間請求間隔SNTP_UPDATE_DELAY,到下一次發起更新時間請求的間隔時間,單位是ms,最小不能小于15s,也是通過make menuconfig修改CONFIG_LWIP_SNTP_UPDATE_DELAY。
    在 make menuconfig -> Component config -> LWIP -> SNTP -> Request interval to update time (ms).

  1. 最好不在任何 callback 或中斷處理函數中調用 obtain_time(), callback/中斷中, 調用任何阻塞的 API, 理論上都有死鎖的可能.

  2. 任何有校驗服務器證書的 TLS 過程 (本地有 CA 證書), 請務必開啟 SNTP 功能, 否則會因為校驗服務器證書有效期失敗而導致 TLS 握手失敗

  3. NTP.ORG.cn中國NTP授時快速域名服務提供商

新加坡sgp.ntp.org.cn韓國kr.ntp.org.cn
中國cn.ntp.org.cn中國教育網edu.ntp.org.cn
中國香港hk.ntp.org.cn中國臺灣tw.ntp.org.cn
美國us.ntp.org.cn韓國kr.ntp.org.cn
日本jp.ntp.org.cn德國de.ntp.org.cn
印度尼西亞ina.ntp.org.cn

  1. 時區:

  • CST-8:這時區的表示有點混
    CST卻同時可以代表如下 4 個不同的時區:
    Central Standard Time (USA) UT-6:00
    Central Standard Time (Australia) UT+9:30
    China Standard Time UT+8:00
    Cuba Standard Time UT-4:00

setenv('TZ', 'CST-8', 1);
  • GMT:(Greenwich Mean Time)是格林尼治平時
    GMT+8正好是中國的標準時區

setenv('TZ', 'GMT+8', 1);
  1. 在成功獲取了網絡時間后,必須調用 sntp_stop(); 停止NTP請求,不然設備重啟后會造成獲取網絡時間失敗的現象,大概是服務器時根據心跳時間來刪除客戶端的,如果不是stop結束的客戶端,下次連接服務器時就會出錯


關鍵字:ESP32  網絡時間協議 引用地址:ESP32學習筆記(41)——SNTP接口使用

上一篇:ESP32學習筆記(42)——硬件定時器接口使用
下一篇:ESP32學習筆記(40)——Watchdog看門狗使用

小廣播
設計資源 培訓 開發板 精華推薦

最新單片機文章
何立民專欄 單片機及嵌入式寶典

北京航空航天大學教授,20余年來致力于單片機與嵌入式系統推廣工作。

 
EEWorld訂閱號

 
EEWorld服務號

 
汽車開發圈

 
機器人開發圈

電子工程世界版權所有 京ICP證060456號 京ICP備10001474號-1 電信業務審批[2006]字第258號函 京公網安備 11010802033920號 Copyright ? 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
主站蜘蛛池模板: 玉田县| 当阳市| 新沂市| 徐州市| 汉阴县| 左贡县| 磐石市| 托克逊县| 台山市| 荆州市| 涟源市| 托克逊县| 新余市| 文昌市| 蒙山县| 石泉县| 普宁市| 通道| 托里县| 裕民县| 雷山县| 荣昌县| 神农架林区| 紫金县| 郓城县| 安图县| 莎车县| 凌云县| 宜黄县| 武清区| 朔州市| 宜君县| 盐亭县| 明光市| 遵化市| 寻乌县| 黔南| 湘乡市| 台东市| 济南市| 丰宁|