增加sntp同步网络时间
authord15521469561 <1731678236@qq.com>
Wed, 6 May 2026 01:43:18 +0000 (09:43 +0800)
committerd15521469561 <1731678236@qq.com>
Wed, 6 May 2026 01:43:18 +0000 (09:43 +0800)
build/config/sdkconfig.h
main/app_main.c
main/ble.h
main/project_common.c
main/project_common.h
main/uart.c
main/user_mqtt.c

index f7fe775..4763d64 100644 (file)
 #define CONFIG_HTTPD_SERVER_EVENT_POST_TIMEOUT 2000
 #define CONFIG_ESP_HTTPS_OTA_EVENT_POST_TIMEOUT 2000
 #define CONFIG_ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT 2000
-#define CONFIG_ESP32C2_REV_MIN_1 1
-#define CONFIG_ESP32C2_REV_MIN_FULL 100
-#define CONFIG_ESP_REV_MIN_FULL 100
+#define CONFIG_ESP32C2_REV_MIN_200 1
+#define CONFIG_ESP32C2_REV_MIN_FULL 200
+#define CONFIG_ESP_REV_MIN_FULL 200
 #define CONFIG_ESP32C2_REV_MAX_FULL 299
 #define CONFIG_ESP_REV_MAX_FULL 299
 #define CONFIG_ESP_EFUSE_BLOCK_REV_MIN_FULL 0
index 451a18d..2caed18 100644 (file)
@@ -29,6 +29,22 @@ static void onesec_timer_callback(void* arg)
     #if ENABLE_MQTT_FUNC
     Mqtt_Send_Data();
     #endif
+    #if ENABLE_SNTP_TIME
+    //测试打印系统时间
+    time_t now = 0;
+    time(&now);  
+
+    // 使用 localtime_r(线程安全,不会被覆盖)
+    struct tm time_info;
+    localtime_r(&now, &time_info);  
+
+    // tm_year + 1900
+    ESP_LOGI(MAIN_TAG, "时间: %d-%02d-%02d-%2d-%02d-%02d",
+        time_info.tm_year + 1900,   // 正确!
+        time_info.tm_mon + 1,
+        time_info.tm_mday,time_info.tm_hour,time_info.tm_min,time_info.tm_sec);
+    #endif
+    
     // 在这里添加每秒执行的代码
     ESP_LOGI(MAIN_TAG, "onesec_timer:ota %d ota_cur_status %d %" PRIu32 " bytes",common_setting.https_ota_cfg.https_ota_start_flag,g_ota_status,esp_get_free_heap_size());
 
@@ -138,6 +154,10 @@ void Main_Loop_Task(void *pvParameters)
         #if ENABLE_MQTT_FUNC
         Mqtt_Connect_Status_Check();
         #endif
+
+        #if ENABLE_SNTP_TIME
+        wait_for_sntp_sync();
+        #endif
         // heart beat
         if(common_setting.heart_beat_enable == 1 && common_setting.heart_beat_interval != 0)
         {
@@ -220,7 +240,8 @@ void app_main(void)
     esp_chip_info_t chip_info;
     esp_chip_info(&chip_info);
     ESP_LOGI(MAIN_TAG, "chip_name: %d chip_info.revision %d", chip_info.model,chip_info.revision);
-
+    ESP_LOGI(MAIN_TAG, "FW_VERSION: 0x%04x BUILD_VERSION 0x%04x\n", FW_VERSION,BUILD_VERSION);
+   
     /* Initialize NVS. */
     esp_err_t ret;
     ret = nvs_flash_init();
@@ -230,7 +251,6 @@ void app_main(void)
     }
     ESP_ERROR_CHECK( ret );
 
-    
     user_flash_mutex = xSemaphoreCreateBinary();
     xSemaphoreGive(user_flash_mutex);
 
@@ -245,9 +265,7 @@ void app_main(void)
     ble_write_queue_handle = xQueueCreateStatic(BLE_WRTIE_QUEUE_LENGTH, BLE_WRTIE_QUEUE_ITEM_SIZE, ble_write_queue_storage, &ble_write_queue_buffer);
     ble_notify_queue_handle = xQueueCreateStatic(BLE_NOTIFY_QUEUE_LENGTH, BLE_NOTIFY_QUEUE_ITEM_SIZE, ble_notify_queue_storage, &ble_notify_queue_buffer);
     #endif
-    
-    uart_tx_queue_handle = xQueueCreateStatic(UART_TX_QUEUE_LENGTH, UART_TX_QUEUE_ITEM_SIZE, uart_tx_queue_storage, &uart_tx_queue_buffer);
-    
+
     common_event_group = xEventGroupCreate();
 
     User_Flash_Check(); 
index aecbc2a..5131d7d 100644 (file)
@@ -27,8 +27,8 @@
 #include "nimble/nimble_port_freertos.h"
 
 /* Defines */
-#define DEVICE_NAME "PXN-W GTL"
-// #define DEVICE_NAME ""
+// #define DEVICE_NAME "PXN-W GTL"
+#define DEVICE_NAME ""
 
 #define GATT_UUID_COMM_0    0xFFD0
 #define GATT_UUID_COMM_1    0xFFD1
index c09c3c1..65699e2 100644 (file)
@@ -1080,4 +1080,73 @@ void print_chip_info(void)
         break;
     }
     ESP_LOGI(CHIP_LOG, "chip_name: %s chip_info.revision %d", chip_name,chip_info.revision);
-}
\ No newline at end of file
+
+   
+}
+
+#if ENABLE_SNTP_TIME
+#include <time.h>
+#include "sntp.h"
+
+// 初始化 NTP 时间同步
+void initialize_sntp(void)
+{
+    ESP_LOGI("NTP", "初始化时间同步...");
+    sntp_setoperatingmode(SNTP_OPMODE_POLL);
+    sntp_setservername(0, "ntp.aliyun.com");
+    sntp_init();
+}
+
+// 等待时间同步完成(官方可靠版)
+static uint8_t sntp_sync = 0;
+void wait_for_sntp_sync(void)
+{
+    if(common_setting.wifi_status.wifi_state == 0) //wifi未连接
+    {
+
+        return;
+    }
+    if(sntp_sync) //同步一次不在同步
+    {
+        return;
+    }
+    sntp_sync=1;
+    initialize_sntp();
+
+    // ==============================
+    // 
+    // ==============================
+    const int timeout_ms = 15000;
+    int waited_ms = 0;
+
+    while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET)
+    {
+        vTaskDelay(pdMS_TO_TICKS(200));
+        waited_ms += 200;
+
+        ESP_LOGI("NTP", "等待时间同步中...");
+
+        // 15秒超时才退出
+        if (waited_ms >= timeout_ms)
+        {
+            ESP_LOGW("NTP", "时间同步超时");
+            break;
+        }
+    }
+
+    time_t now = 0;
+    time(&now);
+
+    // 时区
+    setenv("TZ", "CST-8", 1);
+    tzset();
+
+    // 安全版本 localtime_r
+    struct tm time_info;
+    localtime_r(&now, &time_info);
+
+    char buf[64];
+    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &time_info);
+    ESP_LOGI("TIME", "time success: %s", buf);
+}
+#endif
\ No newline at end of file
index 3f52a3a..4d0a409 100644 (file)
 #endif
 
 
-
+//是否需要同步sntp网络时间
+#define ENABLE_SNTP_TIME    0
 
 #define MAIN_LOOP_INTERVAL  50
 //蓝牙协议指令
@@ -322,6 +323,7 @@ typedef struct
 //617086929577717760
 typedef struct
 {
+    
     uint8_t mqtt_connect_flag;//0 无连接 1已连接
     uint8_t mqtt_sub_flag; //1订阅成功 0订阅失败
     uint8_t mqtt_start_flag;//已经配网成功了开始连接mqtt表示
@@ -474,4 +476,7 @@ void network_request_replay(uint8_t status);
 
 void get_chip_info(char *chip_id,uint8_t *chip_len);
 void print_chip_info(void);
+#if ENABLE_SNTP_TIME
+void wait_for_sntp_sync(void);
+#endif
 #endif
index d0694fe..84295c3 100644 (file)
@@ -34,7 +34,9 @@ void Uart_Initialize(void)
     uart_param_config(UART_NUM_1, &uart_config);
     uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
     uart_driver_install(UART_NUM_1, UART_BUF_SIZE, 0, 0, NULL, 0);
-
+        
+    uart_tx_queue_handle = xQueueCreateStatic(UART_TX_QUEUE_LENGTH, UART_TX_QUEUE_ITEM_SIZE, uart_tx_queue_storage, &uart_tx_queue_buffer);
+    
     uart_rx_queue_handle = xQueueCreateStatic(UART_RX_QUEUE_LENGTH, UART_RX_QUEUE_ITEM_SIZE, uart_rx_queue_storage, &uart_rx_queue_buffer);
 }
 
@@ -135,7 +137,10 @@ void Uart_Task(void *arg)
             // ESP_LOGI(UART_TAG, "Received %d bytes: ", uart_rx_size);
             // ESP_LOG_BUFFER_HEX(UART_TAG, uart_rx_data, uart_rx_size);
             uart_rx_data_struct.size = uart_rx_size;
-            xQueueSend(uart_rx_queue_handle, &uart_rx_data_struct, 0);
+            if(uart_rx_queue_handle){
+                xQueueSend(uart_rx_queue_handle, &uart_rx_data_struct, 0);
+            }
+                
         }
         
         Uart_Tx_Queue_Process();
@@ -423,7 +428,11 @@ void Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type)
     uint8_t crc = Crc8_ROHC_Cal(&uart_pairing_reply.buffer[1], len + 2);
     uart_pairing_reply.buffer[len + 3] = crc;
     // ESP_LOGI(COMMON_TAG, "%s crc %02x len %d",__func__, crc, len);
-
+    if(uart_tx_queue_handle == NULL)
+    {
+        ESP_LOGI(COMMON_TAG, "uart_tx_queue_handle null");
+        return;
+    }
     xQueueSend(uart_tx_queue_handle, &uart_pairing_reply, 0);
 }
 
index 7f649d5..ff29a75 100644 (file)
@@ -889,23 +889,23 @@ static void mqtt_parse_recv_data(char *data, int data_len)
                 Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART);
                 total_data_len=0;
                 memset(send_data,0,UART_REPLY_MAX_PAYLOAD_MQTT);
-
+                vTaskDelay(5); 
             }
 
             // {
             //     "serialNumber": "PXN1LJ1QUR078E",
             //     "prot": [
-            //         { "335": "#ffffff"},
-            //         { "336": "#ff00ff"},
-            //         { "337": "#ff0000"},
-            //         { "338": "#00ff00"},
-            //         { "339": "#00ff00"},
-            //         { "340": "#00ff00"},
-            //         { "341": "#00ff00"},
-            //         { "342": "#00ff00"},
-            //         { "343": "#00ff00"},
-            //         { "344": "#00ff00"},
-            //         { "345": "#00ff00"},
+                        // { "335": "#ffffff"},
+                        // { "336": "#ff00ff"},
+                        // { "337": "#ff0000"},
+                        // { "338": "#00ff00"},
+                        // { "339": "#00ff00"},
+                        // { "340": "#00ff00"},
+                        // { "341": "#00ff00"},
+                        // { "342": "#00ff00"},
+                        // { "343": "#00ff00"},
+                        // { "344": "#00ff00"},
+                        // { "345": "#00ff00"},
                     
             //         { "271": "#ffffff"},
             //         { "272": "#00ff00"},
@@ -925,16 +925,34 @@ static void mqtt_parse_recv_data(char *data, int data_len)
             //         {"735":80},
             //         {"736":70},
             //         {"737":60},
-                    // {"399":"#ff0000"},
-                    // {"400":"#00ff00"},
-                    // {"401":"#0000ff"},
+            //         {"399":"#ff0000"},
+            //         {"400":"#00ff00"},
+            //         {"401":"#0000ff"},
 
-                    // {"412":"00ff00"},
-                    // {"413":"ffffff"},
-                    // {"414":"ff0000"}
+            //         {"412":"00ff00"},
+            //         {"413":"ffffff"},
+            //         {"414":"ff0000"}
 
 
             //     ]
+            // }
+            // 获取 UTC 时间戳(固定不变)
+            // time_t now = 0;
+            // time(&now);  
+
+            // // 转成 本地时间(自动时区计算)
+            // struct tm *time_info = localtime(&now);
+
+            // // 输出本地时间
+            // char buf[64];
+            // strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time_info);
+            // ESP_LOGI("TIME", "本地时间: %s", buf);
+            // ESP_LOGI("TIME", "UTC 时间戳: %lld", now); // 这个才是正确 Unix 时间戳
+            //property get
+            // {
+            //     "serialNumber": "PXN1LJ1QUR078E",
+            //     "proper_get":["dpid1","dpid2","dpid3",................]
+        
             // }
             // 737 10 738 50 739 70
             // total_data_len = 0 0 1 2 3 4 5
@@ -953,25 +971,18 @@ static void mqtt_parse_recv_data(char *data, int data_len)
                 memcpy(&send_data[total_data_len+5],&value,type_len);
             }else{ //copy字符串
                 strncpy((char *)&send_data[total_data_len+5], item->valuestring, type_len);
-                // snprintf((char *)&send_data[total_data_len+5],type_len+1,"%s",item->valuestring);
             }
             
             total_data_len=total_data_len+5+type_len;   //12+1+5=18 18+6=24 24+6=30 30+6=36
-            
-            if(i == prot_array_size - 1)//到最后一个发送
-            {
-                
-                vTaskDelay(2); // 必须加!让UART发送完成
-                ESP_LOGI(MQTT_TAG,"i:%d total_data_len:%d dpid:%u value:%u type:%d type_len:%u\n",i,total_data_len,dpid,value,type,type_len);
-                Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART);
-                type_len = 0;
-                total_data_len=0;
-                memset(send_data,0,UART_REPLY_MAX_PAYLOAD_MQTT);
-                ESP_LOGI(MQTT_TAG,"send end\n");
-            }
+        }
+        if(total_data_len > 0)//发送最后一包
+        {
+            ESP_LOGI(MQTT_TAG,"total_data_len:%d dpid:%u value:%u type:%d type_len:%u\n",total_data_len,dpid,value,type,type_len);
+            Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART);
+            ESP_LOGI(MQTT_TAG,"send end\n");
         }
     }
-    // 释放!!!
+    // 释放
     if(root != NULL)
     {
         cJSON_Delete(root);