1.修复usb升级的问题
authord15521469561 <1731678236@qq.com>
Wed, 17 Jun 2026 02:02:56 +0000 (10:02 +0800)
committerd15521469561 <1731678236@qq.com>
Wed, 17 Jun 2026 02:02:56 +0000 (10:02 +0800)
2.增加wifi信号 名称上传dpid

main/app_main.c
main/ble.c
main/https_ota.c
main/project_common.c
main/project_common.h
main/uart.c
main/uart.h
main/user_http.c
main/user_mqtt.c
main/user_mqtt.h

index acf0f2e..a152ef5 100644 (file)
@@ -30,31 +30,8 @@ static void onesec_timer_callback(void* arg)
 {
     #if ENABLE_MQTT_FUNC
     Mqtt_Send_Data();
+    Mqtt_Report_Wifi_Rssi_Check();
     #endif
-    #if 0
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-
-    struct tm tm_info;
-    localtime_r(&tv.tv_sec, &tm_info);
-
-    char buf[64];
-    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm_info);
-
-    ESP_LOGI("TIME", "当前时间:%s", buf);
-    #endif
-    // static uint8_t onesec_timer_count = 0;
-    // common_setting.tz_offset_min = 8*60;
-
-    // if(onesec_timer_count < 20){
-    //     onesec_timer_count++;
-    //     if(onesec_timer_count == 19)
-    //     {
-    //         Https_Ota_Task();
-    //     }
-        
-    //     // set_system_time_int_from_utc(1779742628,"-3"); //1,779,753,428=2026-05-26 07:57:08 //转换成东八区是 2026-05-26 15:57:08
-    // }
     
     // struct tm local_time = {0};
     // Get_Time(&local_time);
@@ -92,10 +69,11 @@ static void onesec_timer_callback(void* arg)
     //===========455  ota timeout=================//
     N455_Ota_Check_TimeOut();
     #endif
+
     //================mqtt is cnnect===========//
     // 在这里添加每秒执行的代码
     #if ENABLE_HTTPS_OTA_FUNC
-    ESP_LOGI(MAIN_TAG, "ble_state %d ota_cur_status %d %" PRIu32 " bytes",common_setting.ble_status.ble_state,g_ota_status,esp_get_free_heap_size());
+    ESP_LOGI(MAIN_TAG, "ble_state %d wifi rssi %d %" PRIu32 " bytes",common_setting.ble_status.ble_state,common_setting.wifi_status.rssi,esp_get_free_heap_size());
     #else
     ESP_LOGI(MAIN_TAG, "onesec_timer:ota %d %" PRIu32 " bytes",common_setting.https_ota_cfg.https_ota_start_flag,esp_get_free_heap_size());
     #endif
@@ -292,10 +270,7 @@ void app_main(void)
     ESP_LOGI(MAIN_TAG, "[APP] IDF version: %s", esp_get_idf_version());
     ESP_LOGI(MAIN_TAG, "[DEVICE] Version: %04x %04x", FW_VERSION, BUILD_VERSION);
     ESP_LOGI(MAIN_TAG, "[APP] Free memory start: %" PRIu32 " bytes", esp_get_free_heap_size());
-    // Uart_Initialize();
-    // uart_task_handler = xTaskCreateStatic(Uart_Task, "Uart_Task", UART_TASK_STACK, NULL, 7, uart_task_stack, &uart_task_tcb);
 
-    // return;
     //chip info
     esp_chip_info_t chip_info;
     esp_chip_info(&chip_info);
@@ -328,30 +303,28 @@ void app_main(void)
     common_event_group = xEventGroupCreate();
 
     User_Flash_Check(); 
-    // ESP_LOGI(MAIN_TAG, "[APP] Free memory after User_Flash_Check: %" PRIu32 " bytes", esp_get_free_heap_size());
     // UART
     Uart_Initialize();
     uart_task_handler = xTaskCreateStatic(Uart_Task, "Uart_Task", UART_TASK_STACK, NULL, 7, uart_task_stack, &uart_task_tcb);
 
+    uart_send_task_handler = xTaskCreateStatic(Uart_Tx_Task, "Uart_Tx_Task", 1*1024, NULL, 6, uart_send_task_stack, &uart_send_task_tcb);
+
 #if (1)//#if (IOT_ENABLE)
     // LWIP
     ESP_ERROR_CHECK(esp_netif_init());
     ESP_ERROR_CHECK(esp_event_loop_create_default());
-    // ESP_LOGI(MAIN_TAG, "[APP] Free memory after lwip: %" PRIu32 " bytes", esp_get_free_heap_size());
-
     
     // WIFI
     #if ENABLE_WIFI_FUNC
     Wifi_Sta_Init();
     vTaskDelay(500);
-
-    
     #endif
     
     #if ENABLE_HTTPS_OTA_FUNC
     Https_Ota_Handler_Init();
 
     #endif
+    Power_On_Default_Time();
 #endif
     main_loop_task_handler = xTaskCreateStatic(Main_Loop_Task, "Main_Loop_Task", MAIN_LOOP_TASK_STACK, NULL, 4, main_loop_task_stack, &main_loop_task_tcb);
 }
index 9abb08f..4639272 100644 (file)
@@ -142,7 +142,6 @@ void Ble_Start_Nimble_NetWork(void)
     
     if(common_setting.mcu_info_got_flag == 1) //flag=2uart发送的信息 并且没有连接wifi
     {
-        //无serial_number wifi info 并且蓝牙关闭状态开启蓝牙配网功能 
         ESP_LOGI(BLE_TAG, "Ble_Start_Nimble_NetWork on: %d %d %d\n",common_storage.net_info.serial_number_flag,common_storage.net_info.wifi_info_flag,\
         common_setting.ble_status.ble_state);
         //只要有一个不为1都需要重新配网
@@ -187,7 +186,7 @@ void Ble_Start_Nimble_NetWork(void)
             }
         }else{
             xEventGroupSetBits(ble_event_group, BLE_ON_BIT);//开启配网模式
-            ESP_LOGI(BLE_TAG, "start BLE_ON_BIT error  on: on: %" PRIu32 " bytes", esp_get_free_heap_size());
+            ESP_LOGI(BLE_TAG, "start ble");
         }
     }
 }
@@ -203,9 +202,8 @@ int Ble_Start(void)
         common_setting.net_pairing_info.http_url_flag = 0;
         common_setting.net_pairing_info.http_token_flag = 0;
         common_setting.net_pairing_info.user_id_flag = 0;
-        // ESP_LOGI(BLE_TAG, "[APP] Free memory ble on1: %" PRIu32 " bytes", esp_get_free_heap_size());
         Ble_Nimble_Init();
-        // ESP_LOGI(BLE_TAG, "[APP] Free memory ble on2: %" PRIu32 " bytes", esp_get_free_heap_size());
+        ESP_LOGI(BLE_TAG, "BLE start ok");
     }else{
         return 0;
     }
@@ -228,6 +226,7 @@ int Ble_Stop(void)
             common_setting.ble_status.ble_state = 0;
             common_setting.ble_status.ble_adv_flag = 0;
             ESP_LOGI(BLE_TAG, "BLE stopped ok");
+            return 1;
         }
         else
         {
index ceaa846..b5d95ec 100644 (file)
@@ -925,11 +925,12 @@ static void Https_Ota_Task_Cb(void *pvParameter)
 void Https_Ota_Clear_Upgrade_Flag(void)
 {
     g_ota_status = HTTPS_OTA_STATUS_IDLE;
-    s_ota_ctx.ota_timeout_count = 0;
-    s_ota_ctx.https_ota_init=0;
-    s_ota_ctx.ota_timeout_req=0;
-    s_ota_ctx.https_update_ota_flag = 0; //升级标识清除
-    s_ota_ctx.ota_auto = 0;
+    memset(&s_ota_ctx,0,sizeof(s_ota_ctx));
+    // s_ota_ctx.ota_timeout_count = 0;
+    // s_ota_ctx.https_ota_init=0;
+    // s_ota_ctx.ota_timeout_req=0;
+    // s_ota_ctx.https_update_ota_flag = 0; //升级标识清除
+    // s_ota_ctx.ota_auto = 0;
     // memset(https_ota_url,0,sizeof(https_ota_url));
 }
 
index 3d306be..9b1f2e1 100644 (file)
@@ -1240,7 +1240,7 @@ void int64_to_str(uint64_t ts, char *out,size_t out_len) {
 #define TIME_DEFAULT_ZONE  8
 #define TIME_DEFAULT_ZONE_STAMP  (8*3600)  
 int8_t Time_Zone = TIME_DEFAULT_ZONE*2;//默认是东八区 16这是455需要知道的时区
-int64_t Time_Stamp = 0;
+int64_t Time_Stamp = 946684800; //默认是2000-01-01 00:00:00
 /*
  * 函数功能:获取时区 默认是东八区 已经是*2了8*2=16
  * 函数返回值:int8_t 范围是-24~+24
@@ -1281,6 +1281,11 @@ void set_system_time_int_from_utc(int64_t utc_timestamp_sec,const char *zone)
 
 }
 
+void Power_On_Default_Time(void)
+{
+    //开机默认时间
+    set_system_time_int_from_utc(Time_Stamp,"+8");
+}
 /***********************************************
  * 函数功能:获取具体时间 年月日时分秒
  * time_out:输出具体时间结构体 年月日 时分秒
@@ -1394,7 +1399,7 @@ uint16_t version_str_to_hex(char *version)
     return version_total;
 }
 /*
- * 函数功能:清除配网信息
+ * 函数功能:重新配网 不清除配网信息
  * 无返回值
 */
 void Esp32_Factory_Reset(void)
@@ -1436,6 +1441,11 @@ void Esp32_Factory_Reset(void)
 }
 
 
+
+/*
+ *函数功能:绑定失败 清除配网信息
+ * 无返回值
+*/
 void Esp32_Bind_Fail_Reset(void)
 {
 
index 5d089cf..984bbcb 100644 (file)
@@ -224,7 +224,7 @@ typedef union
         uint8_t wifi_info_flag:1;
         uint8_t serial_number_flag:1;
         uint8_t device_id_flag:1;
-        uint8_t reserved:6;
+        uint8_t reserved:5;
 
         uint8_t wifi_ssid_len;
         uint8_t wifi_ssid[WIFI_SSID_MAX_LEN+1];
@@ -515,6 +515,7 @@ int8_t parse_utc_offset_2(const char *zone_str);
 int8_t Get_Time_Zone(void);
 int64_t Get_Time_Stamp(void);
 void Esp32_Bind_Fail_Reset(void);
+void Power_On_Default_Time(void);
 #if ENABLE_SYNC_SNTP_TIME
 void wait_for_sntp_sync(void);
 #endif
index 6bf6262..e4c5dcc 100644 (file)
@@ -23,9 +23,9 @@ TaskHandle_t uart_task_handler;
 StackType_t uart_task_stack[UART_TASK_STACK];
 StaticTask_t uart_task_tcb;
 
-// TaskHandle_t uart_send_task_handler;
-// StackType_t uart_send_task_stack[UART_SEND_TASK_STACK];
-// StaticTask_t uart_send_task_tcb; 
+TaskHandle_t uart_send_task_handler;
+StackType_t uart_send_task_stack[1*1024];
+StaticTask_t uart_send_task_tcb; 
 /*
  * 函数功能:uart初始化
 */
@@ -47,25 +47,40 @@ void Uart_Initialize(void)
     
     // uart_rx_queue_handle = xQueueCreateStatic(UART_RX_QUEUE_LENGTH, UART_RX_QUEUE_ITEM_SIZE, uart_rx_queue_storage, &uart_rx_queue_buffer);
 }
-
+static volatile uint8_t ble_connected_notify_flag = 0;
+static uint16_t ble_connected_notify_count= 0;
+#define UART_SEND_BLE_STATUS_RETRY_NUM       3
+#define UART_SEND_BLE_STATUS_RETRY_TIME       500 //单位ms
 void Uart_Tx_Queue_Process(void)
 {
+    if(ble_connected_notify_flag)
+    {
+        if(ble_connected_notify_count <= UART_SEND_BLE_STATUS_RETRY_TIME*UART_SEND_BLE_STATUS_RETRY_NUM)
+        {
+            if(ble_connected_notify_count == 0) //第一次发送
+            {
+                
+                Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART);
+            }
+            ble_connected_notify_count++;
+            if(ble_connected_notify_count % UART_SEND_BLE_STATUS_RETRY_TIME == 0)
+            {
+                Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART);
+            }
+        } 
+    }else{
+        ble_connected_notify_count = 0;
+    }
     static UART_RECV_DATA_T uart_tx_data = {0};
     if(xQueueReceive(uart_tx_queue_handle, &uart_tx_data, 0) == pdTRUE)
     {
-        // taskENTER_CRITICAL(1);
         uart_write_bytes(UART_NUM_1, uart_tx_data.buffer, uart_tx_data.size);
-        // taskEXIT_CRITICAL(1);
-
-        // ESP_LOGI(UART_TAG, "Send %d bytes: ", uart_tx_data.size);
-        // ESP_LOG_BUFFER_HEX(UART_TAG, uart_tx_data.buffer, uart_tx_data.size);
     }
 }
 #define  RX_CACHE_MAX_LEN  1024
 
 static uint8_t  rx_cache[RX_CACHE_MAX_LEN] = {0};
 static uint32_t rx_cache_len = 0;
-static uint8_t  rx_timeout_cnt = 0;
 // =====================  =====================
 #if 0
 
@@ -160,6 +175,7 @@ void Uart_Rx_Queue_Process(void)
 */
 void Uart_Recv_Handle(uint8_t *data, uint16_t len)
 {
+    // ESP_LOG_BUFFER_HEX(UART_RECV,data,len);
     uint16_t offset = 0;
     while (offset < len)
     {
@@ -187,10 +203,7 @@ void Uart_Recv_Handle(uint8_t *data, uint16_t len)
     }
 }
 #endif
-static volatile uint8_t ble_connected_notify_flag = 0;
-static uint16_t ble_connected_notify_count= 0;
-#define UART_SEND_BLE_STATUS_RETRY_NUM       3
-#define UART_SEND_BLE_STATUS_RETRY_TIME       500 //单位ms
+
 /*
  *函数功能:设置蓝牙连接成功标识
  *参数说明:无
@@ -216,44 +229,32 @@ void Uart_Clear_Ble_Status(void)
     ble_connected_notify_count =0 ;
 
 }
-
+void Uart_Tx_Task(void *arg)
+{
+    while (1)
+    {
+        Uart_Tx_Queue_Process();
+        vTaskDelay(5/ portTICK_PERIOD_MS);
+    }
+}
 /*
  * 函数功能: uart接收发送task
 */
 #define READ_BUF_MAX_LEN (256)
-static uint8_t read_buf[READ_BUF_MAX_LEN]={0};
+
 static uint32_t rx_last_recv_tick  = 0;
 void Uart_Task(void *arg)
 {
     esp_log_level_set(UART_TAG, ESP_LOG_ERROR);
-    // static UART_RECV_DATA_T uart_rx_data_struct = {0};
-    // static uint8_t tx_count = 0;
     static uint32_t now = 0 ;
-    static uint32_t i = 0;
+    static uint8_t read_buf[READ_BUF_MAX_LEN]={0};
     while (1)
     {
         
-        if(ble_connected_notify_flag)
-        {
-            if(ble_connected_notify_count <= UART_SEND_BLE_STATUS_RETRY_TIME*UART_SEND_BLE_STATUS_RETRY_NUM)
-            {
-                if(ble_connected_notify_count == 0) //第一次发送
-                {
-                    
-                    Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART);
-                }
-                ble_connected_notify_count++;
-                if(ble_connected_notify_count % UART_SEND_BLE_STATUS_RETRY_TIME == 0)
-                {
-                    Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART);
-                }
-            } 
-        }else{
-            ble_connected_notify_count = 0;
-        }
         const int uart_rx_size = uart_read_bytes(UART_NUM_1, read_buf, READ_BUF_MAX_LEN,0);//2/ portTICK_PERIOD_MS);
         if (uart_rx_size > 0)
         {
+            // Uart_Recv_Handle(read_buf, uart_rx_size);
             if (rx_cache_len + uart_rx_size < RX_CACHE_MAX_LEN)
             {
                 memcpy(&rx_cache[rx_cache_len], read_buf, uart_rx_size);
@@ -271,23 +272,6 @@ void Uart_Task(void *arg)
                 // memset(rx_cache, 0, sizeof(rx_cache));
             }
         }
-        
-        // if(rx_timeout_cnt > 0)
-        // {
-        //     rx_timeout_cnt--;
-        //     ESP_LOGI(UART_TAG, "rx_cache_len %d bytes: %d\n", rx_cache_len,rx_timeout_cnt);   
-        //     if(rx_timeout_cnt == 0 && rx_cache_len > 0)
-        //     {
-        //         ESP_LOGI(UART_TAG, "recv\n");   
-        //         Uart_Recv_Handle(rx_cache,rx_cache_len);
-        //         rx_cache_len=0;
-        //         memset(rx_cache,0x00,sizeof(rx_cache));
-        //     }
-        // }
-        
-        
-        Uart_Tx_Queue_Process();
-        // Uart_Rx_Queue_Process();
         vTaskDelay(1/ portTICK_PERIOD_MS);
     }
 }
index 94289fa..8117648 100644 (file)
@@ -119,9 +119,9 @@ extern StackType_t uart_task_stack[UART_TASK_STACK];
 extern StaticTask_t uart_task_tcb;
 
 // #define UART_SEND_TASK_STACK                 (1*1024)
-// extern TaskHandle_t uart_send_task_handler;
-// extern StackType_t uart_send_task_stack[UART_SEND_TASK_STACK];
-// extern StaticTask_t uart_send_task_tcb;
+extern TaskHandle_t uart_send_task_handler;
+extern StackType_t uart_send_task_stack[1*1024];
+extern StaticTask_t uart_send_task_tcb;
 
 
 #define UART_REPLY_MAX_PAYLOAD UART_BUF_SIZE // 4 = header(1) + cmd(1) + len(1) + crc(1)
@@ -130,7 +130,7 @@ void Uart_Initialize(void);
 
 void Uart_Tx_Queue_Process(void);
 void Uart_Rx_Queue_Process(void);
-
+void Uart_Tx_Task(void *arg);
 
 void Uart_Task(void *arg);
 int Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type);
index d79bf3d..7f55f6c 100644 (file)
@@ -22,20 +22,8 @@ static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
         ESP_LOGI(HTTP_TAG, "HTTP_EVENT_HEADER_SENT");
         break;
     case HTTP_EVENT_ON_HEADER:
-        // ESP_LOGI(HTTP_TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
         break;
     case HTTP_EVENT_ON_DATA:
-        // ESP_LOGI(HTTP_TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
-        // if (!esp_http_client_is_chunked_response(evt->client)) 
-        // {
-        //     printf("%.*s", evt->data_len, (char*)evt->data);
-        //     uint8_t *data = (uint8_t*)evt->data;
-        //     for(uint32_t i=0;i<evt->data_len;i++)
-        //     {
-        //         printf("%02x ", *(data++));
-        //     }
-        //     printf("\n");
-        // }
         break;
     case HTTP_EVENT_ON_FINISH:
         ESP_LOGI(HTTP_TAG, "HTTP_EVENT_ON_FINISH");
@@ -63,8 +51,6 @@ static char http_download_cache[MAX_HTTP_OUTPUT_BUFFER+1] = {0};
 
 
 HTTP_GET_CERT_TASK_PARAM_T http_get_cert_task_param;
-static const char *HTTP_DOWNLOAD_TASK_TAG = "http_download_task";
-const esp_partition_t *s_ota_partition = NULL;
 /*
 函数功能: https下载证书task回调函数
 */
@@ -97,7 +83,7 @@ static void Http_Download_Task_Cb(void *pvParameters)
             // .disable_auto_redirect = true,
         };
         // mbedtls_ssl_conf_authmode(&ssl_config, MBEDTLS_SSL_VERIFY_NONE);
-        ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "downloading url %s", (char *)http_get_cert_task_param.url[cert_type]);
+        ESP_LOGI(HTTP_TAG, "downloading url %s", (char *)http_get_cert_task_param.url[cert_type]);
         
         esp_err_t err = ESP_FAIL;
         esp_http_client_handle_t http_client = NULL; // 提前定义
@@ -108,18 +94,15 @@ static void Http_Download_Task_Cb(void *pvParameters)
                 err = ESP_FAIL;
                 break;
             }
-            ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "get cer on: %" PRIu32 " bytes", esp_get_free_heap_size());
-            // printf("剩余堆内存: %" PRIu32, esp_get_free_heap_size());
             http_client = esp_http_client_init(&http_config);
             if(http_client == NULL)
             {
-                ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "client init fail");
+                ESP_LOGE(HTTP_TAG, "client init fail");
                 vTaskDelay(50);
                 continue;
             }
-            // ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "esp_http_client_init");
             err = esp_http_client_open(http_client, 0);
-            ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "esp_http_client_init err %d\n",err);
+            ESP_LOGI(HTTP_TAG, "esp_http_client_init err %d\n",err);
             if (err == ESP_OK)
             {
                 int header_ret = esp_http_client_fetch_headers(http_client);
@@ -127,8 +110,8 @@ static void Http_Download_Task_Cb(void *pvParameters)
                 if(code == 200 && header_ret >= 0)
                 {
                     int total_size =  esp_http_client_get_content_length(http_client);
-                    ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http req total_size:%d\n",total_size);
-                    // ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "esp_http_client_get_status_code");
+                    ESP_LOGE(HTTP_TAG, "http req total_size:%d\n",total_size);
+                    // ESP_LOGI(HTTP_TAG, "esp_http_client_get_status_code");
                     
                     if(total_size <= MAX_HTTP_OUTPUT_BUFFER)
                     {
@@ -136,10 +119,10 @@ static void Http_Download_Task_Cb(void *pvParameters)
                         int read_len = 0;
                         memset(http_download_cache, 0x00, sizeof(http_download_cache));
                         read_len = esp_http_client_read(http_client, http_download_cache, total_size);
-                        ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "\nread_len len%d: http_download_cache:%s\n", read_len, http_download_cache);
+                        ESP_LOGI(HTTP_TAG, "\nread_len len%d: http_download_cache:%s\n", read_len, http_download_cache);
                         if(read_len == total_size && read_len <= MAX_HTTP_OUTPUT_BUFFER)
                         {
-                            // ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "\nrecv data len%d: %s\n", total_size, http_download_cache);
+                            // ESP_LOGI(HTTP_TAG, "\nrecv data len%d: %s\n", total_size, http_download_cache);
                             if(common_storage.ssl_cert[cert_type].length == total_size && memcmp(http_download_cache, common_storage.ssl_cert[cert_type].content, total_size) == 0)
                             {
                                 ESP_LOGI(HTTP_TAG, "cert same: %d", cert_type);
@@ -152,38 +135,38 @@ static void Http_Download_Task_Cb(void *pvParameters)
                                 memset(common_storage.ssl_cert[cert_type].content, 0x00, sizeof(common_storage.ssl_cert[0].content));
                                 memcpy(common_storage.ssl_cert[cert_type].content, http_download_cache, total_size);
                                
-                                // ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write cer key server_crt success: %d\n", cert_type);
+                                // ESP_LOGE(HTTP_TAG, "flash write cer key server_crt success: %d\n", cert_type);
 
                                 if(User_Flash_Write_With_Flag(UF_OFFSET_CERT_CLIENT_CRT_ADDR+FLASH_SEC_SIZE*cert_type, UF_OFFSET_CERT_CERT_FLAG, &common_storage.ssl_cert[cert_type], sizeof(SSL_CERT_T)) == ESP_OK)
                                 {
                                     success_bit |= (1 << cert_type);
-                                    ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "server_crt success: %d\n", cert_type);
+                                    ESP_LOGE(HTTP_TAG, "server_crt success: %d\n", cert_type);
                                     err = ESP_OK;
                                     
                                 }
                                 else
                                 {
-                                    ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "server_crt error: %d\n", cert_type);
+                                    ESP_LOGE(HTTP_TAG, "server_crt error: %d\n", cert_type);
                                     err = ESP_FAIL;
                                 }
-                                // ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "flash write wifi pwd ssid success: \n");
+                                // ESP_LOGE(HTTP_TAG, "flash write wifi pwd ssid success: \n");
                             }
                         }
                         else
                         {
-                            ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http read size low = %d", read_len);
+                            ESP_LOGE(HTTP_TAG, "read size low = %d", read_len);
                             err = ESP_FAIL;
                         }
                         #endif
                     }
                     else{
                         err = ESP_FAIL;
-                        ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http filed large\n");
+                        ESP_LOGE(HTTP_TAG, "http filed large\n");
                     }
                 }
                 else
                 {
-                    ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "http req failed%d %d",code,header_ret);
+                    ESP_LOGE(HTTP_TAG, "http req failed%d %d",code,header_ret);
                     err = ESP_FAIL;
                 }
                 
@@ -207,7 +190,7 @@ static void Http_Download_Task_Cb(void *pvParameters)
         
         if(err != ESP_OK)
         {
-            ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "download fail %d", cert_type);
+            ESP_LOGI(HTTP_TAG, "download fail %d", cert_type);
             break;
         }
         vTaskDelay(50 / portTICK_PERIOD_MS);
@@ -239,15 +222,26 @@ static void Http_Download_Task_Cb(void *pvParameters)
         //保存mqtt 地址
         int result = 0;
         //UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info, sizeof(NET_INFO_T)) != ESP_OK
+        
+
+        common_setting.net_pairing_info.http_url_flag=0;
+        common_setting.net_pairing_info.http_token_flag = 0;
+        common_setting.net_pairing_info.user_id_flag = 0;
+        
+        vTaskDelay(10);
+        ESP_LOGI(HTTP_TAG, "download mqtt \n");
+        common_setting.ble_status.nimble_flag=1; //配网成功
+        common_setting.mqtt_user_cfg.mqtt_start_flag=1; //开始连接mqtt
+        common_setting.ble_status.is_bind = 1; //开始绑定
         if(User_Flash_Write_With_Flag(UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info, sizeof(NET_INFO_T)) == ESP_OK)
         {
-            ESP_LOGE(HTTP_DOWNLOAD_TASK_TAG, "network write\n");
+            ESP_LOGE(HTTP_TAG, "network write\n");
             result = 1;
         }else{
             //失败重新读
             if(User_Flash_Read_Check(UF_OFFSET_USER_CONFIG_INFO_ADDR, UF_OFFSET_USER_CONFIG_INFO_FLAG, &common_storage.net_info, sizeof(NET_INFO_T)) == ESP_OK)
             {
-                ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG,"common_storage ok\n");
+                ESP_LOGI(HTTP_TAG,"common_storage ok\n");
             }
             common_setting.net_pairing_info.http_url_flag=0;
             common_setting.net_pairing_info.http_token_flag = 0;
@@ -259,26 +253,12 @@ static void Http_Download_Task_Cb(void *pvParameters)
         }
         network_request_replay(NETWORK_HTTPS_DOWNLOAD_CERTIFICATE_OK);
         vTaskDelay(200);
-
-        common_setting.net_pairing_info.http_url_flag=0;
-        common_setting.net_pairing_info.http_token_flag = 0;
-        common_setting.net_pairing_info.user_id_flag = 0;
         int stop_ret = Ble_Stop();
         if(stop_ret < 0)
         {
-            ESP_LOGW(HTTP_DOWNLOAD_TASK_TAG, "Ble stop failed, continue MQTT");
-        }
-        else
-        {
-            ESP_LOGI(HTTP_DOWNLOAD_TASK_TAG, "Ble stop success");
+            ESP_LOGI(HTTP_TAG,"stop_ret faild\n");
         }
-        vTaskDelay(10);
-        ESP_LOGI(HTTP_TAG, "download mqtt \n");
-        common_setting.ble_status.nimble_flag=1; //配网成功
-        common_setting.mqtt_user_cfg.mqtt_start_flag=1; //开始连接mqtt
-        common_setting.ble_status.is_bind = 1; //开始绑定
         xEventGroupSetBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
-       
         #endif
        
     }else{
@@ -385,7 +365,7 @@ void Http_Get_Bind_Result(void *pvParameters)
     }
     if(result != 1) //绑定结果失败了 重新配网
     {
-        //已经又通过恢复出厂开始广播配网了 
+        //恢复出厂开始广播配网了 
        Esp32_Bind_Fail_Reset();
     }
     get_bind_flag=0;
@@ -395,7 +375,7 @@ void Http_Get_Bind_Result(void *pvParameters)
 /*
  * 函数功能: 获取绑定结果 task入口函数
  */
-void Http_Get_Bing_Result_Task(void){
+void Http_Get_Bind_Result_Task(void){
     ESP_LOGI(HTTP_TAG,"BIND TASK init\n");
     BaseType_t ret = xTaskCreate(Http_Get_Bind_Result, "http_get_bing_result_task", 2*1024, NULL, 2, NULL);
     if(ret != pdPASS)
@@ -453,10 +433,14 @@ static void Http_Get_Url_Task_Cb(void *pvParameters)
         const char path[] = "/iot/device/generateSerialNumberByPid?pid=";// "/iot/device/getDeviceByPid/";//;//;
         memset(url, 0x00, sizeof(url));
         
+        uint8_t mac[6] = {0};
+        char str_mac[32] = {0};
+        esp_read_mac(mac, ESP_MAC_BT);//获取mac地址
+        sprintf(str_mac, "%02X-%02X-%02X-%02X-%02X-%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
         
         // const char *path = "/iot/device/getDeviceByPid/";
         //注意http_url不能大于255-sizeof(path)-sizeof(common_setting_mcu_info.pid) //会编译出错
-        snprintf(url, sizeof(url)-1, "%s%s0x%04x",common_setting.net_pairing_info.http_url,path,common_setting.mcu_info.pid);
+        snprintf(url, sizeof(url)-1, "%s/iot/device/generateSerialNumberByPid?pid=0x%04X&mac=%s",common_setting.net_pairing_info.http_url,common_setting.mcu_info.pid,str_mac);
 
         http_config.url = url;
 
@@ -1028,7 +1012,7 @@ void Http_Download_Status_Check(void)
         xEventGroupClearBits(common_event_group, COMMON_HTTP_BIND_BIT);
         
         vTaskDelay(50 / portTICK_PERIOD_MS);
-        Http_Get_Bing_Result_Task();
+        Http_Get_Bind_Result_Task();
         
     }
 
index 32e1eb2..879305a 100644 (file)
@@ -139,6 +139,9 @@ const char *property_post = "thing.event.property.post";
 const char *service_invoke = "thing.service.calibration.invoke";
 const char *dev_invoke_replay = "thing.service.calibration.invoke.reply";
 
+const char *service_property_get_wifi = "thing.service.property.get.wifi";;
+const char *service_property_get_wifi_reply = "thing.service.property.get.wifi.reply";
+
 /*
  * 函数功能:获取方式请求mode
  * 
@@ -171,6 +174,12 @@ const char *get_methon(uint8_t methon_mode)
             return dev_invoke_replay;
         
 
+        //属性下发获取wifi信息
+        case METHON_SERVICE_PROPERTY_GET_WIFI:
+            return service_property_get_wifi;
+        case METHON_SERVICE_PROPERTY_GET_WIFI_REPLAY:
+            return service_property_get_wifi_reply;
+
         default:
             return "";
     }
@@ -181,9 +190,8 @@ const char *get_methon(uint8_t methon_mode)
 static void mqtt_event_on_connected(esp_mqtt_event_handle_t event)
 {
     esp_mqtt_client_handle_t mqtt_client = event->client;
-    int msg_id = 0;
     mqtt_ctx.mqtt_send_data_count=0;
-
+    int msg_id = 0;
     mqtt_ctx.connect_flag = 1;
     mqtt_ctx.sub_flag = 0;
     ESP_LOGI(MQTT_TAG, "set topic=%s",get_publish_topic(TOPIC_PROPERTY_SET));
@@ -714,6 +722,156 @@ uint8_t Mqtt_Send_Mcu_Info(void)
     mqtt_ctx.mqtt_send_mcu_info_flag=0;
     return 1;
 }
+static void Report_Wifi_Infor_Json(void)
+{
+    // "topic": "topic/PXN1LJ1QUR078E/0xA00B/property/report",
+       // "msgId": "dev_078E_1777518245123",
+       // "method": "thing.event.property.post",
+       // "time": 1777518245,
+       // "serialNumber": "PXN1LJ1QUR078E",
+       // "productId": "0xA00B",
+       // "params": [{"765":1},{"786":89}]
+    // 1. 创建根对象
+    cJSON *root = cJSON_CreateObject();
+    // char *msgId = get_devMsgId();
+    // 2. 一级字段
+    const char *methon = get_methon(METHON_SERVICE_PROPERTY_GET_WIFI_REPLAY);
+    char *topic = get_publish_topic(TOPIC_PROPERTY_ACK_POST);
+    cJSON_AddStringToObject(root, "topic", (const char *)topic);
+    cJSON_AddStringToObject(root, "msgId", (const char *)get_devMsgId());
+    cJSON_AddStringToObject(root, "method", (const char *)methon);
+    time_t Time_Sec = Get_Time_Stamp();
+    static char time_str[16] = {0};
+    int64_to_str((int64_t)Time_Sec,time_str,sizeof(time_str)-1);
+    cJSON_AddStringToObject(root, "time", (const char *)time_str);
+
+    cJSON_AddStringToObject(root, "serialNumber", (char *)common_storage.net_info.serial_number);
+    char pid[8] = {0};
+    snprintf(pid, sizeof(pid), "%04X", common_setting.mcu_info.pid);
+    cJSON_AddStringToObject(root, "productId",pid);
+
+    // 3. 创建 params 数组
+    cJSON *params_arr = cJSON_CreateArray();
+
+    // 数组第一个元素 {"765":1}
+    cJSON *item1 = cJSON_CreateObject();
+    cJSON_AddStringToObject(item1, "10", (char *)common_storage.net_info.wifi_ssid);
+    cJSON_AddItemToArray(params_arr, item1);
+
+    // 数组第二个元素 {"786":89}
+    cJSON *item2 = cJSON_CreateObject();
+    cJSON_AddNumberToObject(item2, "11", common_setting.wifi_status.rssi);
+    cJSON_AddItemToArray(params_arr, item2);
+
+    // 把数组挂到根对象
+    cJSON_AddItemToObject(root, "params", params_arr);
+
+    // 输出格式化JSON字符串
+    char *json_str = cJSON_Print(root);
+    
+    if(json_str == NULL)
+    {   
+        cJSON_Delete(root);
+        return;
+    }
+    if(strlen(json_str) == 0)
+    {
+        cJSON_Delete(root);
+        free(json_str);
+        return;
+    }
+    ESP_LOGI(MQTT_TAG,"wifi len=%d",strlen(json_str));
+    int ret = user_esp_mqtt_client_publish(json_str,strlen(json_str),get_publish_topic(TOPIC_PROPERTY_ACK_POST));
+    if(ret == -1)
+    {
+        ESP_LOGI(MQTT_TAG,"repott wifi failed!");
+    }
+    // 内存释放,必须做,否则内存泄漏
+    // 释放顺序:先释放打印字符串 → 销毁JSON树 → 释放动态topic
+    if(json_str)
+    {
+        free(json_str);
+        json_str = NULL;
+    }
+    cJSON_Delete(root);
+    root = NULL;
+}
+
+
+// WiFi RSSI上报控制参数结构体
+typedef struct {
+    int duration_second;    // 总持续上报时长 s
+    int interval_second;    // 单次上报间隔 s,最小2s
+    bool enable_flag;       // 上报总开关标识(原is_report_rssi_info_flag)
+    int duration_s;         // 当前已计时秒数
+    int32_t rssi_tmp;       // RSSI缓存,用于对比变化触发上报
+} __attribute__((packed)) MQTT_WIFI_REPORT_CTRL_T;
+
+// 全局统一控制实例,替代一堆零散static
+static MQTT_WIFI_REPORT_CTRL_T g_wifi_report_ctrl = {0};
+
+#define REPORT_INTERVAL_CNT  2
+/*
+ *函数功能:设置上报wifi信息参数
+ *参数:
+ *duration:上报持续的时间 s
+ *interval:上报的间隔时间 s 最小不低于2s
+*/
+void Mqtt_Set_Report_Params(int duration,int interval)
+{
+    if(duration > 0 && interval > 0)
+    {
+        if(duration >= interval)
+        {
+            g_wifi_report_ctrl.duration_second = duration;
+            g_wifi_report_ctrl.interval_second = (interval < REPORT_INTERVAL_CNT) ? REPORT_INTERVAL_CNT : interval;
+            g_wifi_report_ctrl.enable_flag=1;
+            g_wifi_report_ctrl.duration_s = 0;
+        }
+    }
+}
+void Mqtt_Report_Wifi_Rssi_Check(void)
+{
+    if(!Mqtt_Get_Connect_Flag()) 
+    {
+        g_wifi_report_ctrl.duration_s = 0;
+        g_wifi_report_ctrl.enable_flag=0;
+
+        return;
+    }
+    if(g_wifi_report_ctrl.enable_flag)
+    {
+        int dur_sec = g_wifi_report_ctrl.duration_second;
+        int int_sec = g_wifi_report_ctrl.interval_second;
+        if(g_wifi_report_ctrl.duration_s < dur_sec && dur_sec > 0)
+        {
+            
+            if(int_sec < 2)
+            {
+                g_wifi_report_ctrl.interval_second = 2;
+                int_sec = 2;
+            }
+            if(g_wifi_report_ctrl.duration_s % int_sec == 0 && g_wifi_report_ctrl.rssi_tmp != common_setting.wifi_status.rssi) //0 % 2 2 4 6 8 10 12 14 16 18 20
+            {
+                g_wifi_report_ctrl.rssi_tmp = common_setting.wifi_status.rssi;
+                //开始上报wifi信息
+                Report_Wifi_Infor_Json();
+            }
+            g_wifi_report_ctrl.duration_s++;
+            if(g_wifi_report_ctrl.duration_s >= dur_sec)
+            {
+                g_wifi_report_ctrl.duration_s=0;
+                g_wifi_report_ctrl.enable_flag=0;
+                g_wifi_report_ctrl.interval_second = 0;
+
+            }
+        }
+    }else{
+        g_wifi_report_ctrl.duration_s = 0;
+    }
+
+}
+
 void mqtt_get_dev_test(uint16_t *dpid,uint8_t num);
 /*
  * 函数功能:mcu信息
index 0e13243..bc8bc56 100644 (file)
@@ -8,6 +8,9 @@ typedef enum{
     METHON_SERVICE_PROPERTY_SET=0,//服务属性设置下发methon
     METHON_SERVICE_PROPERTY_SET_REPLAY, //设备属性设置应答
 
+    METHON_SERVICE_PROPERTY_GET_WIFI,//服务属性获取wifi信息
+    METHON_SERVICE_PROPERTY_GET_WIFI_REPLAY, //设备属性获取wifi信息应答
+
 
     METHON_SERVICE_PROPERTY_GET,//属性获取
     METHON_SERVICE_PROPERTY_GET_REPLAY, //设备属性获取应答
@@ -42,7 +45,7 @@ typedef enum {
 #define MQTT_DATA_BUF_SIZE  MQTT_SEND_RECV_DATA_BUF_SIZE
 #define MQTT_RECV_QUEUE_LENGTH 10
 
-#define MQTT_SEND_QUEUE_LENGTH  8
+#define MQTT_SEND_QUEUE_LENGTH  6
 #define MQTT_SEND_MSG_MAX_LEN   MQTT_SEND_RECV_DATA_BUF_SIZE
 
 
@@ -69,12 +72,6 @@ typedef struct {
 
 // 发送消息结构体
 
-
-typedef struct {
-    char topic[MQTT_TOPIC_LEN+1];
-    char data[MQTT_SEND_RECV_DATA_BUF_SIZE];
-} __attribute__((packed)) mqtt_send_msg_t;
-
 typedef struct {
     uint16_t len;
     uint8_t data[MQTT_SEND_RECV_DATA_BUF_SIZE];
@@ -119,5 +116,10 @@ void mqtt_check_isconnect(void);
 void mqtt_ota_data_parse(const cJSON *array);
 uint8_t get_mqtt_init_flag(void);
 uint8_t Mqtt_Send_Mcu_Info(void);
+
+void Mqtt_Report_Wifi_Rssi_Check(void);
+char *get_devMsgId(void);
+char* get_publish_topic(TOPIC_TYPE type);
+const char *get_methon(uint8_t methon_mode);
 #endif
 #endif
\ No newline at end of file