1.修改配网模式 逻辑
authord15521469561 <1731678236@qq.com>
Mon, 27 Apr 2026 00:58:21 +0000 (08:58 +0800)
committerd15521469561 <1731678236@qq.com>
Mon, 27 Apr 2026 00:58:21 +0000 (08:58 +0800)
2.修改用户id变成字符串不是uint16类型
3.修改led颜色值获取

main/app_main.c
main/gatt_svc.c
main/project_common.c
main/project_common.h
main/uart.c
main/user_flash.c
main/user_mqtt.c
main/user_mqtt.h

index c86ed45..18da8c3 100644 (file)
@@ -40,7 +40,7 @@ static void onesec_timer_callback(void* arg)
 const char* device_name = "PXN-W GTL";
 const char* http_url = "http://192.168.66.85:8080";//"http://107.23.202.137:8080";//"http://192.168.66.80:8080";//http://107.23.202.137:8080
 const char* http_token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImJhZWRiZjUyLTUzMDgtNDRlOS04MWI3LTE1MmU4NGYwYzk5ZCJ9.fyxIGmmYD-pKd7VQ02DrL6FwBKVkqXoIu4iKSVMOUj8WubDvuSdbX6j8YzQw8K_t6UTRVZsRDTlO5me2KKhs7A";//"eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjRhNjBlZDdiLTExMDUtNDU5Ny1iNDBhLTg5ZDRiMzMzNTIyYSJ9.CUxkgU2F6BoxzE3HHVdXAin2OEDD1-D_iI0zh7VWTnlNsjsHHulvZVMq2hQLXF4cAQIJpLWwiMZJQO6Bvy9vwg";
-const uint32_t user_id = 1;
+const char* user_id = "617086929577717760";
 // const char* wifi_ssid = "PXN";
 // const char* wifi_pwd = "PXN998877";
 const char* wifi_ssid = "PXN";
@@ -54,7 +54,6 @@ const uint16_t mcu_pid = 0xA00B;
 void Main_Loop_Task(void *pvParameters)
 {
     common_setting.heart_beat_interval = 1000;
-    common_setting.mcu_info_got_flag = 1;
     common_setting.heart_beat_enable = 1;
     common_setting.ble_status.ble_state=0;
     // // 创建1秒周期性定时器
@@ -101,6 +100,8 @@ void Main_Loop_Task(void *pvParameters)
     common_setting.net_pairing_info.http_token_length = strlen(http_token);
 
     common_setting.net_pairing_info.user_id = user_id;
+    memcpy(common_setting.net_pairing_info.user_id,user_id,sizeof(net_pairing_info.user_id))
+
 
     common_setting.net_pairing_info.http_url_flag = 1;
     common_setting.net_pairing_info.http_token_flag = 1;
@@ -144,7 +145,7 @@ void Main_Loop_Task(void *pvParameters)
             if(heart_beat_tick >= common_setting.heart_beat_interval)
             {
                 heart_beat_tick = 0;
-                ESP_LOGI(MAIN_TAG, "send heart\n");
+                
                 #if ENABLE_MQTT_FUNC
                 uint8_t heart_beat_data[2] = {0};//数据
                 heart_beat_data[0]=COMMON_MQTT_STATUS;//状态类型
@@ -160,12 +161,26 @@ void Main_Loop_Task(void *pvParameters)
                 #endif
                 // Check_Host_Type(0xFF, heart_beat_data, sizeof(heart
                 
-               
-                if(common_setting.mcu_info_got_flag == 0)
+                ESP_LOGI(MAIN_TAG, "send heart :%d\n",common_setting.mcu_info_got_flag);
+                if(common_setting.mcu_info_got_flag == 0)//没有收到主控发过来的mcu信息 一开机获取一次通过0xff 主控收到0xff就发送0x01 0x0A 0X0C对应的指令
+                {
+                    ESP_LOGI(MAIN_TAG, "send ff :%d\n",common_setting.mcu_info_got_flag);
+                    Uart_Reply_Send(0xff, NULL, 0, HOST_TYPE_UART);//发送查询主控信息 这个指令由主控主动发起
+                }
+            }
+        }else{ //防止没有心跳通过0xff获取mcu的信息 需要主控发送mcu信息
+            static uint32_t poweron_count = 0;
+            
+            poweron_count += MAIN_LOOP_INTERVAL;
+            if(poweron_count >= 5000)
+            {
+                poweron_count=0;
+                if(common_setting.mcu_info_got_flag == 0)//没有收到主控发过来的mcu信息 一开机获取一次通过0xff 主控收到0xff就发送0x01 0x0A 0X0C对应的指令
                 {
-                    // Uart_Reply_Send(0x01, NULL, 0, HOST_TYPE_UART);//发送查询主控信息 这个指令由主控主动发起
+                    Uart_Reply_Send(0xff, NULL, 0, HOST_TYPE_UART);//发送查询主控信息 这个指令由主控主动发起
                 }
             }
+            
         }
 
         if((xEventGroupWaitBits(common_event_group, COMMON_RESET_BIT, pdFALSE, pdFALSE, 0) & COMMON_RESET_BIT) != 0)
index e346a5e..d399665 100644 (file)
@@ -102,12 +102,12 @@ static int gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
             // ble_gatts_chr_updated(attr_handle);
             // MODLOG_DFLT(INFO, "Notification/Indication scheduled for "
             //             "all subscribed peers.\n");
-            ESP_LOGI(GATT_TAG,"write %d, %d, %d:\n", ctxt->om->om_flags, ctxt->om->om_pkthdr_len, ctxt->om->om_len);
+            printf("write %d, %d, %d:\n", ctxt->om->om_flags, ctxt->om->om_pkthdr_len, ctxt->om->om_len);
             for(size_t i=0;i<ctxt->om->om_len;i++)
             {
-                ESP_LOGI(GATT_TAG,"%02x ", ctxt->om->om_data[i]);
+                printf("%02x ", ctxt->om->om_data[i]);
             }
-            
+            printf("\n");
 
             // memset(&ble_notify,0x00,sizeof(BLE_MESSAGE_T));
             // if(ctxt->om->om_len <= sizeof(ble_notify.data))
@@ -123,7 +123,6 @@ static int gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
             {
                 ble_message_temp.length = ctxt->om->om_len;
                 memcpy(ble_message_temp.data, ctxt->om->om_data, ctxt->om->om_len);
-                // xQueueSend(ble_notify_queue_handle, &ble_message_temp, 0); // 回传测试
                 xQueueSend(ble_write_queue_handle, &ble_message_temp, 0);
             }
             return rc;
index debf0b9..6b5cc7c 100644 (file)
@@ -191,7 +191,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                             memcpy(common_setting.mcu_info.product_name, &data[12], name_len);
                             common_setting.mcu_info.product_name[name_len] = '\0'; // 确保字符串结尾
                             common_setting.mcu_info.product_name_len = name_len;
-                            common_setting.mcu_info_got_flag = 1;
+                            // common_setting.mcu_info_got_flag = 1;
                             net_pairing_reply.content[0]=0x01;
                             
                         }
@@ -389,8 +389,9 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                 uint8_t parse_done = 0;
                 char wifi_ssid[32+1] = {0};
                 char wifi_pwd[64+1] = {0};
-
-                if (len < 106)
+                
+                uint16_t data_total_len = *(uint16_t*)&data[2];
+                if (len < 106 || data_total_len != 102)
                 {
                     ESP_LOGW(COMMON_TAG, "wifi conn data too short (%u)\n",len);
                     net_pairing_reply.send_flag = 1;
@@ -400,7 +401,6 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                     net_pairing_reply.content[2] = data[5];
                     break;
                 }
-
                 size_t max_ssid_src = (len > 6) ? len - 6 : 0;
                 size_t max_pwd_src = (len > 38) ? len - 38 : 0;
                 size_t wifi_ssid_len = strnlen((const char*)&data[6], max_ssid_src);
@@ -542,6 +542,11 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                         else
                         {
                             ESP_LOGI(COMMON_TAG, "wifi pair wrong\n");
+                            net_pairing_reply.send_flag = 1;
+                            net_pairing_reply.len = 3;
+                            net_pairing_reply.content[0] = 0x08;
+                            net_pairing_reply.content[1] = data[4];
+                            net_pairing_reply.content[2] = data[5];
                         }
                     }
                     else
@@ -592,7 +597,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                     ESP_LOGW(COMMON_TAG, "net pairing 0x16 len too small (%u)", len);
                     net_pairing_reply.send_flag = 1;
                     net_pairing_reply.len = 1;
-                    net_pairing_reply.content[0] = 0x10;
+                    net_pairing_reply.content[0] |=0x10;
                     break;
                 }
 
@@ -610,10 +615,11 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                     ESP_LOGW(COMMON_TAG, "net pairing 0x16 too big (%u > %u)\n", packet_bytes, len - 13);
                     net_pairing_reply.send_flag = 1;
                     net_pairing_reply.len = 1;
-                    net_pairing_reply.content[0] = 0x10;
+                    net_pairing_reply.content[0]|=0x10;
                     break;
                 }
-
+                // 2*16+2 =34-9=25 16*1+9 = 25
+                //ff 16 22 00 19 00 c4 b2 01 00 00 19 68 74 74 70 3a 2f 2f 31 39 32 2e 31 36 38 2e 36 36 2e 38 35 3a 38 30 38 39 59
                 net_pairing_reply.send_flag = 1;
                 net_pairing_reply.len = 9;
                 memcpy(net_pairing_reply.content, &data[4], 9);
@@ -660,7 +666,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                         {
                             // CRC 
                             uint16_t calc_crc = Crc16_Modbus_Cal((uint8_t*)common_setting.net_pairing_info.http_url, total_len);
-                            uint16_t packet_crc = (data[7] << 8) | data[8];
+                            uint16_t packet_crc = (data[8] << 8) | data[7];//高在后 低在前
 
                             ESP_LOGI(COMMON_TAG, "http url: %s (len=%u)\n", common_setting.net_pairing_info.http_url, total_len);
                             
@@ -670,7 +676,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                                 //上一次已经接收完成了一个完整的http url包了,但校验失败了
                                 ESP_LOGI(COMMON_TAG, "http data error\n");
                                 http_end=0;
-                                memset(common_setting.net_pairing_info.http_url, 0x00, HTTP_URL_MAX_LEN);
+                                memset(common_setting.net_pairing_info.http_url, 0x00, HTTP_URL_MAX_LEN+1);
                                 net_pairing_reply.content[5] |= 0x20;
                                 break;//提前退出,等待APP重新发送一个新的http url包过来
                             }
@@ -679,17 +685,18 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                             {
                                 
                                 http_end=0;
+                                net_pairing_reply.content[5] |= 0x00;
                                 common_setting.net_pairing_info.http_url_length = total_len;
                                 common_setting.net_pairing_info.http_url_flag = 1;
-                                ESP_LOGI(COMMON_TAG, "http crc success: %s\n", common_setting.net_pairing_info.http_url);
+                                ESP_LOGI(COMMON_TAG, "http crc success: %s net_pairing_reply.content[5]:%02x\n", common_setting.net_pairing_info.http_url,net_pairing_reply.content[5]);
                             }
                             else
                             {
                                 http_end=1;
-                                memset(common_setting.net_pairing_info.http_url, 0x00, HTTP_URL_MAX_LEN);
+                                memset(common_setting.net_pairing_info.http_url, 0x00, HTTP_URL_MAX_LEN+1);
                                 net_pairing_reply.content[5] |= 0x20;
                                 common_setting.net_pairing_info.http_url_flag = 0;
-                                ESP_LOGI(COMMON_TAG, "http CRC: =%04x recv=%04x\n", calc_crc, packet_crc);
+                                ESP_LOGI(COMMON_TAG, "http CRC: =%04x recv=%04x 0x%02x\n", calc_crc, packet_crc,net_pairing_reply.content[5]);
                             }
                         }
                     }
@@ -732,7 +739,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                         {
                             // 
                             uint16_t calc_crc = Crc16_Modbus_Cal((uint8_t*)common_setting.net_pairing_info.http_token, total_len);
-                            uint16_t packet_crc = (data[7] << 8) | data[8];
+                            uint16_t packet_crc = (data[8] << 8) | data[7];
 
                             ESP_LOGI(COMMON_TAG, "correct token: %s (len=%u)\n", common_setting.net_pairing_info.http_token, total_len);
                             //本次结束后校验出错了,然后又下发最后一包不完整的数据
@@ -748,6 +755,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                             if(packet_crc == calc_crc)
                             {
                                 token_end=0;
+                                 net_pairing_reply.content[5] |= 0x00;
                                 common_setting.net_pairing_info.http_token_length = total_len;
                                 common_setting.net_pairing_info.http_token_flag = 1;
                                 ESP_LOGI(COMMON_TAG, "tokencrc success  %s",common_setting.net_pairing_info.http_token);
@@ -765,7 +773,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
 
                     else if(data[4] == 2) // user_id
                     {
-                        //  ESP_LOGI(COMMON_TAG, "net pairing 0x16 user_id overflow: start=%u packet=%u", start_b);
+                        //  ESP_LOGI(COMMON_TAG, "net pairing 0x16  overflow: start=%u packet=%u", start_b);
                         // if(start_byte >= sizeof(int) || packet_bytes > sizeof(int) || (size_t)start_byte + packet_bytes > sizeof(int))
                         // {
                         //     ESP_LOGI(COMMON_TAG, "net pairing 0x16 user_id overflow: start=%u packet=%u", start_byte, packet_bytes);
@@ -774,31 +782,44 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                         // }
                         if(start_byte == 0)
                         {
-                            common_setting.net_pairing_info.user_id=0;
+                            // common_setting.net_pairing_info.user_id=0;
+                            memset(common_setting.net_pairing_info.user_id,0x00,sizeof(common_setting.net_pairing_info.user_id));
+                        }
+                        uint16_t copy_len = (packet_bytes < (USER_ID_MAX_LEN - start_byte)) ? packet_bytes : (USER_ID_MAX_LEN - start_byte);
+                        if(total_len > USER_ID_MAX_LEN)
+                        {
+                            total_len=USER_ID_MAX_LEN;
+                            // 类型错误
+                            net_pairing_reply.content[5] |= 0x10;
+                            ESP_LOGI(COMMON_TAG, "net pair info type err\n");
+                            break;
                         }
-                        uint16_t copy_len = (packet_bytes < (sizeof(int) - start_byte)) ? packet_bytes : (sizeof(int) - start_byte);
-                        memcpy(&common_setting.net_pairing_info.user_id, &data[13], copy_len);
+                        
+                        memcpy(common_setting.net_pairing_info.user_id, &data[13], total_len);
+                        common_setting.net_pairing_info.user_id[total_len]='\0';
                     
                         if(data[9] == 1)
                         {
-                            if(total_len > sizeof(int))
+                            if(total_len > USER_ID_MAX_LEN)
                             {
-                                total_len = sizeof(int);
+                                total_len = USER_ID_MAX_LEN;
                             }
-                            uint16_t calc_crc = Crc16_Modbus_Cal((uint8_t*)&common_setting.net_pairing_info.user_id, total_len);
-                            uint16_t packet_crc = (data[7] << 8) | data[8];
+                            uint16_t calc_crc = Crc16_Modbus_Cal((uint8_t*)common_setting.net_pairing_info.user_id, total_len);
+                            uint16_t packet_crc = (data[8] << 8) | data[7];
 
-                            ESP_LOGI(COMMON_TAG, "user_id: %08x\n", (unsigned int)common_setting.net_pairing_info.user_id);
+                            ESP_LOGI(COMMON_TAG, "user_id: %s %d total_len:%d\n",common_setting.net_pairing_info.user_id,copy_len,total_len);
                             
                              //本次结束后校验出错了,然后又下发最后一包不完整的数据
                             if(packet_crc == calc_crc)
                             {
                                 common_setting.net_pairing_info.user_id_flag = 1;
-                                ESP_LOGI(COMMON_TAG, "user_id: %08x\n", (unsigned int)common_setting.net_pairing_info.user_id);
+                                net_pairing_reply.content[5] |= 0x00;
+                                ESP_LOGI(COMMON_TAG, "user_id: %s\n",common_setting.net_pairing_info.user_id);
                             }
                             else
                             {
-                                common_setting.net_pairing_info.user_id=0;
+                                // common_setting.net_pairing_info.user_id=0;
+                                memset(common_setting.net_pairing_info.user_id,0,sizeof(common_setting.net_pairing_info.user_id));
                                 common_setting.net_pairing_info.user_id_flag = 0;
                                 // 校验失败
                                 net_pairing_reply.content[5] |= 0x20;
@@ -817,7 +838,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                 {
                     if(data[4] == 0) // http配网服务器地址
                     {
-                        memset(common_setting.net_pairing_info.http_url, 0x00, NET_PAIRING_INFO_MAX_LEN);
+                        memset(common_setting.net_pairing_info.http_url, 0x00, HTTP_URL_MAX_LEN+1);
                     }
                     else if(data[4] == 1) // token
                     {
@@ -825,7 +846,8 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                     }
                     else if(data[4] == 2) // user_id
                     {
-                        common_setting.net_pairing_info.user_id = 0;
+                        // common_setting.net_pairing_info.user_id = 0;
+                        memset(common_setting.net_pairing_info.user_id, 0x00, sizeof(common_setting.net_pairing_info.user_id));
                     }
                     // 失败
                     net_pairing_reply.content[5] |= 0x10;
index bba936c..8deb0f5 100644 (file)
@@ -62,7 +62,7 @@
 
 // 最终对外版本
 #define FW_VERSION                             (FW_VERSION_B)          // 固件版本 //10.22         // 固件版本 //10.22         // 固件版本 //10.22         // 固件版本 //0A00 10.00 10.21
-#define BUILD_VERSION                  (0x0007)        // 编译测试版本
+#define BUILD_VERSION                  (0x0008)        // 编译测试版本
 #define DEVICE_VID                  (0x36E6)        // 设备厂商代码
 #define DEVICE_PID                  (0xEC01)        // 设备型号代码
 #define LWIP_LOCAL_HOSTNAME         "PXN-Device"    // WIFI本地主机名称
@@ -288,6 +288,7 @@ typedef struct
 
 #define   HTTP_URL_MAX_LEN     128
 #define   HTTP_TOKEN_MAX_LEN     255
+#define   USER_ID_MAX_LEN      20
 typedef struct
 {
     uint8_t serial_number_flag:1;
@@ -309,7 +310,7 @@ typedef struct
     int device_id;
     char http_url[HTTP_URL_MAX_LEN+1];
     char http_token[NET_PAIRING_INFO_MAX_LEN+1];
-    uint32_t user_id;
+    char user_id[USER_ID_MAX_LEN+1];
     char mqtt_sub_url[MQTT_SUB_URL_MAX_LEN+1];
 }__attribute__((packed)) NET_PAIR_INFO_T;
 typedef struct
@@ -318,7 +319,7 @@ typedef struct
     
     
 }__attribute__((packed)) HTTPS_OTA_CONFIG_INFO_T;
-
+//617086929577717760
 typedef struct
 {
     uint8_t mqtt_connect_flag;//0 无连接 1已连接
index 29afbda..0d612f2 100644 (file)
@@ -215,7 +215,7 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                         }
                         else
                         {
-                            ESP_LOGI(COMMON_TAG, "%s  data len %d, want %d\n",__func__, len, (12 + name_len));
+                            ESP_LOGI(COMMON_TAG, "data len %d, want %d\n", len, (12 + name_len));
                             name_len = 0;
                             uart_pairing_reply.content[0]=0x01;
                         }
@@ -343,7 +343,8 @@ void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
             {
                 //通过mqtt发送给上位机
                 #if ENABLE_MQTT_FUNC
-                // Mqtt_Get_Key_Led_Color(data+3,data[2]);//从第三个字节开始 不包含crc和之前的协议字段
+                ESP_LOG_BUFFER_HEX(COMMON_TAG, data, len);
+                Mqtt_Get_Key_Led_Color(data+3,data[2]-1);//从第三个字节开始 不包含crc和之前的协议字段
                 #endif
             }
             break;
index 75679c9..301f1ba 100644 (file)
@@ -15,7 +15,7 @@ void User_Flash_Check(void)
        //寻找分区表
        partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, PARA_FLASH_NAME);
        assert(partition != NULL);
-       User_Erase_Nimble_NetWork_Infor();
+       // User_Erase_Nimble_NetWork_Infor();
        //读取flash数据到common_storage
        
        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)
index 4c8d7b8..f22ee59 100644 (file)
@@ -43,8 +43,8 @@ static void log_error_if_nonzero(const char *message, int error_code)
 static uint8_t mqtt_init_task_flag = 0;
 static uint8_t mqtt_send_data_count = 0;
 static uint8_t mqtt_init_flag = 0;
-static char topic_str[64]={"/property/post"};
-#define MQTT_USER_DEFAULT_TOPIC     1
+static char topic_str[64]={0};
+#define MQTT_USER_DEFAULT_TOPIC     0
 static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
 {
     ESP_LOGD(MQTT_TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id);
@@ -63,7 +63,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
         snprintf(topic_str,sizeof(topic_str),"%s",MQTT_TOPIC);
         msg_id = esp_mqtt_client_subscribe(mqtt_client, topic_str, 0);
         #else
-        snprintf(topic_str,sizeof(topic_str),"%04X/%s/property/post",common_setting.mcu_info.pid,(char *)common_storage.net_info.serial_number);
+        snprintf(topic_str,sizeof(topic_str),"topic/%s/%04X/property/post",(char *)common_storage.net_info.serial_number,common_setting.mcu_info.pid);
         msg_id = esp_mqtt_client_subscribe(mqtt_client, topic_str, 0);
         #endif
         ESP_LOGI(MQTT_TAG, "send subscribe msg_id=%d topic_str=%s", msg_id,topic_str);
@@ -152,7 +152,7 @@ static esp_err_t Mqtt_App_Init(void)
         return ESP_FAIL;
     }
     
-    if(common_storage.net_info.mqtt_url_len==0)
+    if(common_storage.net_info.mqtt_url_len==0 && common_setting.mcu_info.pid == 0x0000)
     {
         ESP_LOGI(MQTT_TAG, "mqtt_sub_url == 0");
         return ESP_FAIL;
@@ -177,12 +177,14 @@ static esp_err_t Mqtt_App_Init(void)
     common_setting.mqtt_cfg.broker.verification.certificate = (const char *)common_storage.ssl_cert[SSL_SERVER_CRT].content;
     common_setting.mqtt_cfg.credentials.authentication.certificate = (const char *)common_storage.ssl_cert[SSL_CLIENT_CRT].content;
     common_setting.mqtt_cfg.credentials.authentication.key = (const char *)common_storage.ssl_cert[SSL_CLIENT_KEY].content;
+    char pid_str[10]={0};
+    sprintf(pid_str,"%04X",common_setting.mcu_info.pid);
     #if MQTT_USER_DEFAULT_TOPIC
     common_setting.mqtt_cfg.credentials.client_id = "basicPubSub";
     #else
-    common_setting.mqtt_cfg.credentials.client_id = (const char *)common_storage.net_info.serial_number;
+    common_setting.mqtt_cfg.credentials.client_id = (const char *)pid_str;
     #endif
-    ESP_LOGI(MQTT_TAG, "Mqtt init %s",(char *)common_storage.net_info.serial_number);
+    ESP_LOGI(MQTT_TAG, "Mqtt init %s",pid_str);
     common_setting.mqtt_client_handle = esp_mqtt_client_init(&common_setting.mqtt_cfg);
     if(common_setting.mqtt_client_handle == NULL){
         ESP_LOGI(MQTT_TAG, "client_handle null\n");
@@ -214,57 +216,200 @@ uint8_t Mqtt_Get_Connect_Flag(void)
     return 0;
 }
 // MQTT发送按键灯颜色
+static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
+{
+    switch (evt->event_id) {
+    case HTTP_EVENT_ERROR:
+        // ESP_LOGI(HTTP_TAG, "HTTP_EVENT_ERROR");
+        break;
+    case HTTP_EVENT_ON_CONNECTED:
+        // ESP_LOGI(HTTP_TAG, "HTTP_EVENT_ON_CONNECTED");
+        break;
+    case HTTP_EVENT_HEADER_SENT:
+        // 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");
+        break;
+    case HTTP_EVENT_DISCONNECTED:
+        // ESP_LOGI(HTTP_TAG, "HTTP_EVENT_DISCONNECTED");
+        break;
+    case HTTP_EVENT_REDIRECT:
+        // ESP_LOGI(HTTP_TAG, "HTTP_EVENT_REDIRECT");
+        break;
+    }
+    return ESP_OK;
+}
 void Mqtt_Get_Key_Led_Color(uint8_t *color_data,uint16_t color_len)
 {
-    return;
-    ESP_LOG_BUFFER_HEX(MQTT_TAG, color_data, color_len);
+    esp_http_client_config_t http_config = 
+    {
+        .event_handler = _http_event_handler,
+        .disable_auto_redirect = true,
+        .keep_alive_enable = true,
+    };
+    const char path[] = "/iot/device/";
+    memset(url, 0x00, sizeof(url));
+    //PATH 10 255-10
+    // if(common_setting.net_pairing_info.http_url_length < (sizeof(url) - sizeof(path))){
+    //     memcpy(url, common_setting.net_pairing_info.http_url, sizeof(url)-common_setting.net_pairing_info.http_url_length);
+    //     memcpy(&url[common_setting.net_pairing_info.http_url_length], path, strlen(path));
+    // }
+    snprintf(url,sizeof(url)-1,"%s",path);
+    esp_http_client_handle_t http_client = NULL;
+    http_config.url = url;
+    http_client = esp_http_client_init(&http_config);
+    if(http_client == NULL)
+    {
+
+        ESP_LOGI(MQTT_TAG, "%s http_config.url = %s",__func__, http_config.url);
+        
+        
+    }
+    esp_http_client_set_header(http_client, "Authorization", (const char*)common_setting.net_pairing_info.http_token);
+    esp_http_client_set_method(http_client, HTTP_METHOD_PUT);
+    esp_http_client_set_header(http_client, "Content-Type", "application/json");
+
+    esp_http_client_set_post_field(http_client, post_data, strlen(post_data));
+    esp_err_t err = esp_http_client_perform(http_client);
+    int result = 0;
+    if (err == ESP_OK)
+    {
+        int status_code = esp_http_client_get_status_code(http_client);
+        int read_len = esp_http_client_get_content_length(http_client);
+        if(status_code == 200)
+        {
+            result =  1;
+        }
+        else
+        {
+            result =  2;
+        }
+        ESP_LOGI(MQTT_TAG, "PUT Status = %d, content_length = %d", status_code, read_len);
+    }
+    else
+    {
+        result = 2;
+        
+    }
+
+    // f2 0d 84 7f 01 05 03 00 36 34 30 30 41 30 80 0105 03 00 34 36 43 33 44 43 81 01 05 03 00 46 41 35 35 30 30 
+    // 82 01 05 03 00 46 46 30 30 30 30 83 01 05 03 00 30 30 46 46 30 30 84 01 05 03 00 30 30 30 30 46 46 
+    // 85 01 05 03 00 46 30 35 41 36 3986 01 05 03 00 38 32 46 41 41 46 87 01 05 03 0041 46 30 30 38 43 
+    // 88 01 05 03 00 46 30 33 43 30 30 89 01 05 03 00 46 46 42 34 30 30 8a 01 05 03 00 39 42 46 46 35 30 
+    // 22
     uint8_t uart_reply_data[1]={0};
     //没有订阅成 没有连接wifi 没有连接mqtt 直接返回NULL
-    if(!Mqtt_Get_Connect_Flag() || color_data == NULL || color_len == 0 || color_len < 8) 
+    // if(!Mqtt_Get_Connect_Flag() || color_data == NULL || color_len == 0 || color_len < 8) 
+    // {
+    //     uart_reply_data[0] = REPLY_STATUS_FAIL;
+    //     Uart_Reply_Send(COMMON_UART_GET_KEY_COLOR_CMD,uart_reply_data, sizeof(uart_reply_data), HOST_TYPE_UART);
+    //     ESP_LOGI(MQTT_TAG, "Mqtt_Get_Key_Led_Color!!!%d\n",color_len);
+    //     // ESP_LOG_BUFFER_HEX(MQTT_TAG, color_data, color_len);
+    //     return;
+    // }
+    char pid_str[6] = {0};
+    char vid_str[6] = {0};
+    char wifi_soc[15] = {"Unknown"};
+
+    // PID、VID 大写十六进制格式
+    snprintf(pid_str, sizeof(pid_str), "%04X", common_setting.mcu_info.pid);
+    snprintf(vid_str, sizeof(vid_str), "%04X", common_setting.mcu_info.vid);
+
+    // 获取芯片型号
+    uint8_t chip_len = 0;
+    get_chip_info(wifi_soc, &chip_len);
+    if (chip_len == 0) {
+        strcpy(wifi_soc, "Unknown");
+    }
+
+    // 版本号格式化 0x0015 → V0021
+    char fw_ver[8] = {0}, wifi_ver[8] = {0};
+    snprintf(fw_ver, sizeof(fw_ver), "%04u", common_setting.mcu_info.fw_version);
+    snprintf(wifi_ver, sizeof(wifi_ver), "%04u", (unsigned int)FW_VERSION);
+    if(FW_VERSION > 9999)
     {
-        uart_reply_data[0] = REPLY_STATUS_FAIL;
-        Uart_Reply_Send(COMMON_UART_GET_KEY_COLOR_CMD,uart_reply_data, sizeof(uart_reply_data), HOST_TYPE_UART);
-        ESP_LOGI(MQTT_TAG, "Mqtt_Get_Key_Led_Color!!!%d\n",color_len);
-        // ESP_LOG_BUFFER_HEX(MQTT_TAG, color_data, color_len);
-        return;
+
+       snprintf(wifi_ver, sizeof(wifi_ver), "%s","9999");
     }
-    
-    //key==dpid value==color 12*3=36
-    // "383":"FF0000",
-    
-    char key[8]={0};//65535
-    char value[10]={0};
+    // ===================== 构建正确的 JSON =====================
     cJSON *root = cJSON_CreateObject();
-    if(root==NULL)
+    if (root == NULL) return;
+    
+
+    // 把设备信息加到里面
+    cJSON_AddStringToObject(root, "pid", pid_str);
+    cJSON_AddStringToObject(root, "vid", vid_str);
+    cJSON_AddStringToObject(root, "wifi_soc", wifi_soc);
+    cJSON_AddStringToObject(root, "fw_version", fw_ver);
+    cJSON_AddStringToObject(root, "wifi_version", wifi_ver);
+    cJSON_AddStringToObject(root, "serial_id", (char *)common_storage.net_info.serial_number);
+    cJSON_AddNumberToObject(root, "connected_platform", 2);
+    cJSON_AddNumberToObject(root, "power_on", 0);
+    cJSON *pRoot = cJSON_CreateObject();
+    // cJSON_AddItemToObject(root,""thingsModelValue": "string",");
+    if(pRoot == NULL)
     {
-        ESP_LOGI(MQTT_TAG, "is not storage\n");
-        uart_reply_data[0] = REPLY_STATUS_FAIL;
-        Uart_Reply_Send(COMMON_UART_GET_KEY_COLOR_CMD,uart_reply_data, sizeof(uart_reply_data), HOST_TYPE_UART);
+        if(root != NULL)
+        {
+            cJSON_Delete(root);
+        }
         
         return;
     }
-    for(int i=0;i<color_len/8;i++)
+    cJSON_AddItemToObject(root,"thingsModelValue",pRoot);
+
+    
+    char key[8]={0};//65535
+    uint16_t len = 0;
+    
+    char value[32]={0};//8个字节也就是16字符 ff ff ff ff ff ff ff ff
+
+    for(int i=0;i<color_len/11;i++)
     {
-        uint8_t dpid_h = color_data[i*8 + 0];   // 高8位 0x01
-        uint8_t dpid_l = color_data[i*8 + 1];   // 低8位 0x7F
+        uint8_t dpid_h = color_data[i*11 + 0];   // 高8位 0x01
+        uint8_t dpid_l = color_data[i*11 + 1];   // 低8位 0x7F
         uint16_t dpid = (dpid_h << 8) | dpid_l; // 0x017F = 383
-
+        //17
         snprintf(key,sizeof(key),"%d", dpid);
         if(color_data[i*8+2] == 5) //数据类型
         {
             //
         }
-        uint8_t d_h = color_data[i*8 + 3];   // 高8位 0x7f
-        uint8_t d_l = color_data[i*8 + 4];   // 低8位 0x01
+        uint8_t d_h = color_data[i*11 + 3];   // 高8位 0x7f
+        uint8_t d_l = color_data[i*11 + 4];   // 低8位 0x01
 
-        uint16_t data_len = d_l << 8 |d_h;
+        uint16_t data_len = (d_h << 8) |d_l;
+        ESP_LOGI(MQTT_TAG, "Mqtt_Get_Key_Led_Color!!!%d\n",data_len);
         // ESP_LOGI(MQTT_TAG,"COLOR LEN:%d",data_len);
-        // uint8_t r = color_data[i*8 + 5];
+        // uint8_t r = color_data[i*8 + 5]; 5 6 7 8 9 10
         // uint8_t g = color_data[i*8 + 6];
         // uint8_t b = color_data[i*8 + 7];
-        // memcpy(value,color_data[]);
-        // snprintf(value,sizeof(value),"%02X%02X%02X",r,g,b);
-        cJSON_AddStringToObject(root, key, value);
+        if(data_len > 32-1)
+        {
+            data_len=32-1;
+        }
+        memset(value,0,sizeof(value));
+        memcpy(value,&color_data[i*11+5],data_len);
+        value[data_len]='\0';
+        snprintf(key,sizeof(key),"%d",dpid);
+        cJSON_AddStringToObject(pRoot, key, value);
     }
     char *json_str = cJSON_PrintUnformatted(root);
     cJSON_Delete(root);
@@ -286,6 +431,59 @@ void Mqtt_Get_Key_Led_Color(uint8_t *color_data,uint16_t color_len)
     mqtt_send_msg_t send_msg={0};
     memcpy(send_msg.data,json_str,json_str_len);
     ESP_LOGI(MQTT_TAG, "key led:%s\n", json_str);
+
+
+    esp_http_client_config_t http_config = 
+    {
+        .event_handler = _http_event_handler,
+        .disable_auto_redirect = true,
+        .keep_alive_enable = true,
+    };
+    const char path[] = "/iot/device/";
+    char url[128] = {0};
+    memset(url, 0x00, sizeof(url));
+    //PATH 10 255-10
+    // if(common_setting.net_pairing_info.http_url_length < (sizeof(url) - sizeof(path))){
+    //     memcpy(url, common_setting.net_pairing_info.http_url, sizeof(url)-common_setting.net_pairing_info.http_url_length);
+    //     memcpy(&url[common_setting.net_pairing_info.http_url_length], path, strlen(path));
+    // }
+    snprintf(url,sizeof(url)-1,"%s",path);
+    esp_http_client_handle_t http_client = NULL;
+    http_config.url = url;
+    http_client = esp_http_client_init(&http_config);
+    if(http_client == NULL)
+    {
+
+        ESP_LOGI(MQTT_TAG, "%s http_config.url = %s",__func__, http_config.url);
+        
+        
+    }
+    esp_http_client_set_header(http_client, "Authorization", (const char*)common_setting.net_pairing_info.http_token);
+    esp_http_client_set_method(http_client, HTTP_METHOD_PUT);
+    esp_http_client_set_header(http_client, "Content-Type", "application/json");
+
+    esp_http_client_set_post_field(http_client, json_str, strlen(json_str));
+    esp_err_t err = esp_http_client_perform(http_client);
+    int result = 0;
+    if (err == ESP_OK)
+    {
+        int status_code = esp_http_client_get_status_code(http_client);
+        int read_len = esp_http_client_get_content_length(http_client);
+        if(status_code == 200)
+        {
+            result =  1;
+        }
+        else
+        {
+            result =  2;
+        }
+        ESP_LOGI(MQTT_TAG, "PUT Status = %d, content_length = %d", status_code, read_len);
+    }
+    else
+    {
+        result = 2;
+        
+    }
     free(json_str);
     json_str = NULL;
     memcpy(send_msg.topic, topic_str, strlen(topic_str));
@@ -359,6 +557,16 @@ void Mqtt_Send_Mcu_Info(void)
     cJSON_AddStringToObject(root, "serial_id", (char *)common_storage.net_info.serial_number);
     cJSON_AddNumberToObject(root, "connected_platform", 2);
     cJSON_AddNumberToObject(root, "power_on", 0);
+    cJSON *pRoot = cJSON_CreateObject();
+    // cJSON_AddItemToObject(root,""thingsModelValue": "string",");
+    if(pRoot == NULL)
+    {
+        cJSON_Delete(root);
+        return;
+    }
+    cJSON_AddItemToObject(root,"thingsModelValue",pRoot);
+
+
     // 生成压缩格式JSON(不带换行,HTTP专用)
     char *post_data = cJSON_Print(root);
     // char *post_data = cJSON_PrintUnformatted(root);
index 74d1c76..0cf22b0 100644 (file)
@@ -25,7 +25,7 @@ typedef struct {
 } mqtt_recv_msg_t;
 
 // 发送消息结构体
-#define MQTT_SEND_DATA_BUF_SIZE 512
+#define MQTT_SEND_DATA_BUF_SIZE 1024
 
 typedef struct {
     char topic[MQTT_TOPIC_LEN+1];