第一次提交:完成了网关的单路485数据采集,还有以太网链接和MQTT配置,实现数据上报和命令下发,差一个断网储存
This commit is contained in:
3
components/SNTP_ESP/CMakeLists.txt
Normal file
3
components/SNTP_ESP/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "SNTP_ESP.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES lwip)
|
||||
133
components/SNTP_ESP/SNTP_ESP.c
Normal file
133
components/SNTP_ESP/SNTP_ESP.c
Normal file
@@ -0,0 +1,133 @@
|
||||
#include "SNTP_ESP.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_sntp.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include <sys/time.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TAG "SNTP_ESP"
|
||||
|
||||
static bool time_synced = false;
|
||||
|
||||
// 时间同步回调函数
|
||||
static void sntp_sync_time_callback(struct timeval *tv)
|
||||
{
|
||||
time_t now = tv->tv_sec;
|
||||
struct tm timeinfo;
|
||||
localtime_r(&now, &timeinfo);
|
||||
|
||||
char time_str[64];
|
||||
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", &timeinfo);
|
||||
|
||||
ESP_LOGI(TAG, "时间同步成功: %s", time_str);
|
||||
time_synced = true;
|
||||
}
|
||||
|
||||
esp_err_t sntp_esp_set_timezone(void)
|
||||
{
|
||||
// 设置中国标准时间(北京时间)
|
||||
setenv("TZ", "CST-8", 1);
|
||||
tzset();
|
||||
ESP_LOGI(TAG, "时区设置为北京时间 (CST-8)");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
time_t sntp_esp_get_current_time(void)
|
||||
{
|
||||
// 使用POSIX函数获取时间
|
||||
return time(NULL);
|
||||
}
|
||||
|
||||
void sntp_esp_print_current_time(void)
|
||||
{
|
||||
time_t now = sntp_esp_get_current_time();
|
||||
struct tm timeinfo;
|
||||
char buffer[64];
|
||||
|
||||
localtime_r(&now, &timeinfo);
|
||||
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S %A", &timeinfo);
|
||||
|
||||
ESP_LOGI(TAG, "当前时间: %s", buffer);
|
||||
}
|
||||
|
||||
esp_err_t sntp_esp_get_formatted_time(char *buffer, size_t size, const char *format)
|
||||
{
|
||||
if (buffer == NULL || format == NULL) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
time_t now = sntp_esp_get_current_time();
|
||||
struct tm timeinfo;
|
||||
localtime_r(&now, &timeinfo);
|
||||
|
||||
strftime(buffer, size, format, &timeinfo);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
bool sntp_esp_is_synced(void)
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
// 检查时间是否已初始化(从1970年到现在)
|
||||
return (now > 1000000000); // 2001年9月9日之后的任何时间都认为是有效时间
|
||||
}
|
||||
|
||||
bool sntp_esp_wait_sync(uint32_t max_wait_ms)
|
||||
{
|
||||
ESP_LOGI(TAG, "等待时间同步(最长等待 %lu ms)...", (unsigned long)max_wait_ms);
|
||||
|
||||
uint32_t start_time = xTaskGetTickCount();
|
||||
uint32_t max_wait_ticks = pdMS_TO_TICKS(max_wait_ms);
|
||||
|
||||
while ((xTaskGetTickCount() - start_time) < max_wait_ticks) {
|
||||
if (sntp_esp_is_synced()) {
|
||||
ESP_LOGI(TAG, "时间同步完成");
|
||||
return true;
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(100)); // 每100毫秒检查一次
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "时间同步超时");
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_err_t sntp_esp_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "初始化SNTP服务");
|
||||
|
||||
// 重置同步标志
|
||||
time_synced = false;
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
// 设置时间服务器(默认使用 pool.ntp.org)
|
||||
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
|
||||
// 添加 NTP 服务器
|
||||
// esp_sntp_setservername(0, "pool.ntp.org"); // 默认服务器
|
||||
esp_sntp_setservername(0, "cn.pool.ntp.org"); // 中国 NTP 服务器
|
||||
esp_sntp_setservername(1, "ntp1.aliyun.com"); // 阿里云 NTP 服务器
|
||||
esp_sntp_setservername(2, "ntp.tencent.com"); // 腾讯云 NTP 服务器
|
||||
|
||||
// 设置时间同步回调
|
||||
esp_sntp_set_time_sync_notification_cb(sntp_sync_time_callback);
|
||||
|
||||
// 初始化 SNTP
|
||||
esp_sntp_init();
|
||||
#else
|
||||
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
|
||||
// esp_sntp_setservername(0, "pool.ntp.org"); // 默认服务器
|
||||
sntp_setservername(0, "cn.pool.ntp.org"); // 中国 NTP 服务器
|
||||
sntp_setservername(1, "ntp1.aliyun.com"); // 阿里云 NTP 服务器
|
||||
sntp_setservername(2, "ntp.tencent.com"); // 腾讯云 NTP 服务器
|
||||
|
||||
sntp_set_time_sync_notification_cb(sntp_sync_time_callback);
|
||||
sntp_init(); // 初始化 SNTP
|
||||
#endif
|
||||
|
||||
sntp_esp_set_timezone(); // 设置时区
|
||||
sntp_esp_print_current_time(); // 打印时间
|
||||
|
||||
ESP_LOGI(TAG, "SNTP服务初始化完成");
|
||||
return ESP_OK;
|
||||
}
|
||||
72
components/SNTP_ESP/include/SNTP_ESP.h
Normal file
72
components/SNTP_ESP/include/SNTP_ESP.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef SNTP_ESP_H
|
||||
#define SNTP_ESP_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include "esp_err.h"
|
||||
|
||||
/**
|
||||
* @brief 初始化SNTP服务
|
||||
*
|
||||
* 初始化SNTP客户端,配置NTP服务器和时区
|
||||
* 必须在获得网络连接后调用
|
||||
*
|
||||
* @return ESP_OK 成功
|
||||
* ESP_FAIL 失败
|
||||
*/
|
||||
esp_err_t sntp_esp_init(void);
|
||||
|
||||
/**
|
||||
* @brief 设置时区
|
||||
*
|
||||
* 设置本地时区,默认为北京时间(CST-8)
|
||||
*
|
||||
* @return ESP_OK 成功
|
||||
*/
|
||||
esp_err_t sntp_esp_set_timezone(void);
|
||||
|
||||
/**
|
||||
* @brief 获取当前时间
|
||||
*
|
||||
* @return time_t 当前时间戳
|
||||
*/
|
||||
time_t sntp_esp_get_current_time(void);
|
||||
|
||||
/**
|
||||
* @brief 打印当前时间
|
||||
*
|
||||
* 以格式化的方式打印当前时间到日志
|
||||
*/
|
||||
void sntp_esp_print_current_time(void);
|
||||
|
||||
/**
|
||||
* @brief 获取格式化的时间字符串
|
||||
*
|
||||
* @param buffer 存储时间字符串的缓冲区
|
||||
* @param size 缓冲区大小
|
||||
* @param format 时间格式(如 "%Y-%m-%d %H:%M:%S")
|
||||
* @return ESP_OK 成功
|
||||
* ESP_FAIL 失败
|
||||
*/
|
||||
esp_err_t sntp_esp_get_formatted_time(char *buffer, size_t size, const char *format);
|
||||
|
||||
/**
|
||||
* @brief 等待时间同步完成
|
||||
*
|
||||
* 等待SNTP时间同步完成
|
||||
*
|
||||
* @param max_wait_ms 最大等待时间(毫秒)
|
||||
* @return true 同步成功
|
||||
* false 超时
|
||||
*/
|
||||
bool sntp_esp_wait_sync(uint32_t max_wait_ms);
|
||||
|
||||
/**
|
||||
* @brief 检查时间是否已同步
|
||||
*
|
||||
* @return true 已同步
|
||||
* false 未同步
|
||||
*/
|
||||
bool sntp_esp_is_synced(void);
|
||||
|
||||
#endif // SNTP_ESP_H
|
||||
Reference in New Issue
Block a user