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
+ // }
- time_t Time_Sec=Get_Time_Stamp();
-
-
- char time_str[24]={0};
- int64_to_str((int64_t)Time_Sec, time_str,sizeof(time_str)-1);
- ESP_LOGI("TIME","time: %s",time_str);
+ struct tm local_time = {0};
+ Get_Time(&local_time);
+ // uint8_t send[9]={0};
+ // int64_t time = Get_Time_Stamp();
+ // memcpy(send,&time,8);
+ // int8_t time_zone = Get_Time_Zone();
+ // memcpy(&send[8],&time_zone,1);
+ // int8_t t = send[8];
+ // ESP_LOGI("TIME","time:%x-%x-%x-%x-%x-%x-%x-%x zone=%x,%d t=%d\n",send[0],send[1],send[2],send[3],send[4],send[5],send[6],send[7],send[8],(int8_t)send[8],t);
+ // // char time_str[24]={0};
+ // // int64_to_str((int64_t)Time_Sec, time_str,sizeof(time_str)-1);
+ // // ESP_LOGI("TIME","time: %4d-%02d-%02d %02d:%02d:%02d",local_time.tm_year+1900,local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,local_time.tm_min,local_time.tm_sec);
+ // // ESP_LOGI("TIME", "putenv TZ ret: %d", putenv("TZ"));
#if ENABLE_HTTP_N455_OTA_FUNC
static uint8_t onesec_timer_count = 0;
if(onesec_timer_count < 65)
mqtt_check_isconnect();
// 在这里添加每秒执行的代码
#if ENABLE_HTTPS_OTA_FUNC
- // 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());
+ ESP_LOGI(MAIN_TAG, "common_setting.ble_status.ble_state %d ota_cur_status %d %" PRIu32 " bytes",common_setting.ble_status.ble_state,g_ota_status,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
// common_setting.mcu_info.product_name_len = strlen(device_name);
// common_setting.mcu_info_got_flag = 1;
-#if (ENABLE_HTTP_N455_OTA_FUNC)
+#if (0)
// BLE Test
common_setting.mcu_info.vid = mcu_vid;
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();
// 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, 2, uart_task_stack, &uart_task_tcb);
+ 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_Send_Task, "Uart_Send_Task", UART_SEND_TASK_STACK, NULL, 5, uart_send_task_stack, &uart_send_task_tcb);
+
// ESP_LOGI(MAIN_TAG, "[APP] Free memory after Uart_Initialize: %" PRIu32 " bytes", esp_get_free_heap_size());
#if (1)//#if (IOT_ENABLE)
// LWIP
* gap_event_handler is a callback function registered when calling
* ble_gap_adv_start API and called when a GAP event arrives
*/
+static bool ble_connected_executed = false;
static int gap_event_handler(struct ble_gap_event *event, void *arg) {
/* Local variables */
int rc = 0;
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
if (rc != 0)
{
- ESP_LOGE(GAP_TAG,
+ ESP_LOGI(GAP_TAG,
"failed to find connection by handle, error code: %d",
rc);
return rc;
}
-
+ if(!ble_connected_executed){
+ ble_connected_executed=1;
+ common_setting.ble_enable_tick = 0;
+ common_setting.ble_status.ble_state = 2;
+ Uart_Set_Ble_Status();
+ }
/* Print connection descriptor */
print_conn_desc(&desc);
-
- /* Try to update connection parameters */
- struct ble_gap_upd_params params = {.itvl_min = desc.conn_itvl,
- .itvl_max = desc.conn_itvl,
- .latency = 3,
- .supervision_timeout =
- desc.supervision_timeout};
- rc = ble_gap_update_params(event->connect.conn_handle, ¶ms);
- if (rc != 0)
- {
- ESP_LOGE(
- GAP_TAG,
- "failed to update connection parameters, error code: %d",
- rc);
- return rc;
- }
- common_setting.ble_enable_tick = 0;
- common_setting.ble_status.ble_state = 2;
- Uart_Set_Ble_Status();
+
+ ESP_LOGI(GAP_TAG,"updata param %d",rc);
+
+
+ ESP_LOGI(GAP_TAG,"update connection parameters, OK code: %d",rc);
}
/* Connection failed, restart advertising if allowed */
else
event->disconnect.reason);
common_setting.ble_status.ble_state=0;
/* Restart advertising if allowed */
+ ble_connected_executed=0;
if (ble_advertising_enabled) {
start_advertising();
}
/* The central has updated the connection parameters. */
ESP_LOGI(GAP_TAG, "connection updated; status=%d",
event->conn_update.status);
-
+ if(!ble_connected_executed){
+ ble_connected_executed=1;
+ common_setting.ble_enable_tick = 0;
+ common_setting.ble_status.ble_state = 2;
+ Uart_Set_Ble_Status();
+ }
/* Print connection descriptor */
rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
if (rc != 0) {
return;
}
https_ota_init=1;
- BaseType_t ret = xTaskCreate(&Https_Ota_Task_Cb, "https_ota_task", HTTPS_OTA_STACK_SIZE, NULL, 4, NULL);
+ BaseType_t ret = xTaskCreate(Https_Ota_Task_Cb, "https_ota_task", HTTPS_OTA_STACK_SIZE, NULL, 4, NULL);
if(ret !=pdPASS )
{
Https_Ota_Clear_Upgrade_Flag();
}else{
ota_timeout_count=0;
}
- // if(ota_faile_count > 0)
- // {
- // ota_faile_count--;
- // if(ota_faile_count == 0)
- // {
- // Https_Ota_Start();
+ if(ota_faile_count > 0)
+ {
+ ota_faile_count--;
+ if(ota_faile_count == 0)
+ {
+ Https_Ota_Start();
- // }
+ }
- // }
+ }
}
// OTA升级回调事件处理函数
continue;
}
cJSON_AddStringToObject(pRoot, "serialNumber", (const char*)common_storage.net_info.serial_number);
- if(ota_status == OTA_STATUS_SUCCESS
- #if ENABLE_HTTP_N455_OTA_FUNC
- || ota_status == N455_OTA_STATUS_SUCCESS
- )
+ #if (!ENABLE_HTTP_N455_OTA_FUNC)
+ if(ota_status == OTA_STATUS_SUCCESS)
#else
- )
+ if(ota_status == OTA_STATUS_SUCCESS || ota_status == N455_OTA_STATUS_SUCCESS)
#endif
{
cJSON_AddNumberToObject(pRoot,"upgradeStatus", 3);
cJSON_AddStringToObject(pRoot, "detailMsg", "升级成功");
- }else if(ota_status == OTA_STATUS_UPGRADING
- #if ENABLE_HTTP_N455_OTA_FUNC
- || ota_status == OTA_STATUS_UPGRADING
- )
+ }
+ #if (!ENABLE_HTTP_N455_OTA_FUNC)
+ else if(ota_status == OTA_STATUS_UPGRADING)
#else
- )
+ else if(ota_status == OTA_STATUS_UPGRADING || ota_status == OTA_STATUS_UPGRADING)
#endif
{
cJSON_AddStringToObject(pRoot, "detailMsg", "升级中");
vTaskDelay(50);
}
- if(ota_status == OTA_STATUS_SUCCESS
- #if ENABLE_HTTP_N455_OTA_FUNC
- || ota_status == OTA_STATUS_UPGRADING
- )
+ #if (!ENABLE_HTTP_N455_OTA_FUNC)
+ if(ota_status == OTA_STATUS_SUCCESS)
#else
- )
+ if(ota_status == OTA_STATUS_SUCCESS || ota_status == OTA_STATUS_UPGRADING)
#endif
{
Wifi_Stop();//断开wifi
return 2;
}
n455_ota_init=1;
- BaseType_t ret = xTaskCreate(&N455_Ota_Task_Cb, "https_n455_ota_task", HTTPS_OTA_STACK_SIZE, NULL, 6, NULL);
+ BaseType_t ret = xTaskCreate(N455_Ota_Task_Cb, "https_n455_ota_task", HTTPS_OTA_STACK_SIZE, NULL, 6, NULL);
if(ret !=pdPASS )
{
ESP_LOGI(N455_OTA, "task err\n");
#ifndef _HTTP_N455_OTA_H_
#define _HTTP_N455_OTA_H_
-#define ENABLE_HTTP_N455_OTA_FUNC 1 //是否开启HTTPS_OTA升级
+#define ENABLE_HTTP_N455_OTA_FUNC 0 //是否开启HTTPS_OTA升级
#if ENABLE_HTTP_N455_OTA_FUNC
#include <stdint.h>
#include <stdbool.h>
#include "ble.h"
#include "user_mqtt.h"
#include "user_flash.h"
-static const char *COMMON_TAG = "COMMON";
+static const char *COMMON_TAG = "COMMON";
+typedef struct {
+ uint16_t fw_version;
+ uint16_t pid;
+ uint16_t vid;
+} __attribute__((packed)) user_custom_version_header_t;
+
+const __attribute__((section(".rodata_desc"), used, retain))
+user_custom_version_header_t esp_user_app_custom_desc = {
+ .fw_version = FW_VERSION,
+ .pid = DEVICE_PID,
+ .vid = DEVICE_VID,
+};
const FW_FILE_HEADER_T firmware_header =
{
.header_magic = 0xBAD0C0FFEE00CAFE,
#endif
/*
-将时间戳字符串转换成int64数字
+ * 函数功能:将时间戳字符串转换成int64数字
+ * ts:时间戳数字
+ * out:输出时间戳字符串
+ * out_len:输出时间戳字符串长度
+ * 无返回值:void
*/
void int64_to_str(uint64_t ts, char *out,size_t out_len) {
char buf[24] = {0};
}
out[idx] = 0;
}
-//获取本地的时间戳INT64
-time_t Get_Time_Stamp(void)
-{
- time_t Time_Stamp = 0;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- Time_Stamp = tv.tv_sec;
+#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;
+/*
+ * 函数功能:获取时区 默认是东八区 已经是*2了8*2=16
+ * 函数返回值:int8_t 范围是-24~+24
+*/
+int8_t Get_Time_Zone(void)
+{
+ return Time_Zone;
+}
+/*
+ * 函数功能:获取世界时间戳不带时区的时间戳
+ * 函数返回值:int64_t 默认是2000-01-01 00:00:00
+*/
+int64_t Get_Time_Stamp(void)
+{
return Time_Stamp;
}
-//是不是有效的时区格式 UTC开头
-static bool is_valid_timezone(const char *tz)
+
+
+/*
+ * 函数功能:设置本地时间
+ * 参数:
+ * utc_timestamp_sec:服务器下发的时间戳带时区
+ * zone:是时区字符串 考虑到有UTC+3.5这种需要字符串
+ * 无返回值:void
+*/
+
+void set_system_time_int_from_utc(int64_t utc_timestamp_sec,const char *zone)
{
- // 不能为空
- if (!tz || strlen(tz) < 4 || strlen(tz) > 8)
- return false;
-
- // 前3个字符必须是 UTC
- if (strncmp(tz, "UTC", 3) != 0)
- return false;
-
- // 第4位必须是 + 或 -
- char sign = tz[3];
- if (sign != '+' && sign != '-')
- return false;
-
- // 后面必须是数字
- for (int i = 4; i < strlen(tz); i++) {
- if (!isdigit((unsigned char)tz[i]))
- return false;
- }
+ int32_t time_s = parse_utc_offset_min(zone)*60;
+ Time_Zone = parse_utc_offset_2(zone);
+ Time_Stamp = utc_timestamp_sec-time_s;//utc_timestamp_sec加了时区的时间戳 需要减去相对于的时区分钟数
+ ESP_LOGI("TIME","zone:%s utc_timestamp_sec:%ld TIME_ZONE=%ld Time_Zone:%d Time_Stamp=%ld",zone,(uint32_t)utc_timestamp_sec,time_s,Time_Zone,(uint32_t)Time_Stamp);
+ struct timeval tv;
+ tv.tv_sec = utc_timestamp_sec;
+ tv.tv_usec = 0;
+
+ settimeofday(&tv, NULL);
- return true;
}
-/*
-utc_timestamp_sec :时间戳单位s
-*timeZone :字符串 UTC+-8
+/***********************************************
+ * 函数功能:获取具体时间 年月日时分秒
+ * time_out:输出具体时间结构体 年月日 时分秒
*/
-void set_system_time_str_from_utc(int64_t utc_timestamp_sec, const char *timeZone)
+void Get_Time(struct tm *time_out)
{
- if(!is_valid_timezone((const char *)timeZone))
- {
- return;
+ time_t now = 0;
+ time(&now);
+
+ time_t local = now + TIME_DEFAULT_ZONE_STAMP;
+ localtime_r(&local,time_out);
+ #if 1
+ ESP_LOGI("TIME", "time_out:%04d-%02d-%02d %02d:%02d:%02d",
+ time_out->tm_year+1900, \
+ time_out->tm_mon+1,\
+ time_out->tm_mday,\
+ time_out->tm_hour,\
+ time_out->tm_min,\
+ time_out->tm_sec);
+ #endif
+}
+
+
+/*
+ * 函数功能:时区字符串转换成分钟数
+ * zone_str:时区字符串
+ * 返回值:int
+*/
+int parse_utc_offset_min(const char *zone_str)
+{
+ if (!zone_str || strlen(zone_str) < 2) {
+ return TIME_DEFAULT_ZONE_STAMP;
}
- struct timeval tv;
- struct timezone tz = {0};
- // 1. 直接设置 UTC 时间戳(系统永远存 UTC)
- tv.tv_sec = utc_timestamp_sec;
- tv.tv_usec = 0;
- settimeofday(&tv, &tz);
+ if (zone_str[0] != '+' && zone_str[0] != '-') {
+ return TIME_DEFAULT_ZONE_STAMP;
+ }
- // 2. 设置时区(告诉系统怎么转本地时间)
-
- setenv("TZ", timeZone, 1);
- tzset(); // 生效
+ // 2. 直接转浮点数 +8 → 8.0 -3.5 → -3.5
+ float hours = atof(zone_str);
+
+ // 3. 转成分钟
+ return (int)(hours * 60.0f);
}
-//根据时时间戳和时区设置本地时间
/*
-utc_timestamp_sec:时间戳64
-timezone : 时区int 8 - 8
+ * 函数功能:时区字符串转换成int8_t *2=时区倍数
+ * zone_str:时区字符串
+ * 返回值:int8_t -24~+24
*/
-void set_system_time_int_from_utc(int64_t utc_timestamp_sec, int timeZone)
+int8_t parse_utc_offset_2(const char *zone_str)
{
- struct timeval tv;
- struct timezone tz = {0};
+ if (!zone_str || strlen(zone_str) < 2) {
+ return 16;
+ }
- // 1. 直接设置 UTC 时间戳(系统永远存 UTC)
- tv.tv_sec = utc_timestamp_sec;
- tv.tv_usec = 0;
- settimeofday(&tv, &tz);
-
- // 2. 设置时区(告诉系统怎么转本地时间)
- char tz_buf[16];
- if (timeZone >= 0) {
- // 东时区:UTC-8
- snprintf(tz_buf, sizeof(tz_buf), "UTC+%d", timeZone);
- } else {
- // 西时区:UTC+5
- snprintf(tz_buf, sizeof(tz_buf), "UTC-%d", timeZone);
+ if (zone_str[0] != '+' && zone_str[0] != '-') {
+ return 16;
+ }
+
+ // 2. 直接转浮点数 +8 → 8.0 -3.5 → -3.5
+ float hours = atof(zone_str);
+
+ // 4. 转成 半小时倍数(×2)
+ int8_t offset_half_hour = (int8_t)(hours * 2.0f);
+
+ // 5. 合法范围校验(全球时区:-12 ~ +14 → -24 ~ +28)
+ if (offset_half_hour < -24 || offset_half_hour > 24) {
+ return 16; //
}
- setenv("TZ", tz_buf, 1);
- tzset(); // 生效
+
+ return offset_half_hour;
}
/*
-func:版本转换
-云端 version 转 16 进制版本号(例如 1.03 → 0x0103)
+ * 函数功能:版本字符串转换hex
+ * version:版本号字符串
+ * 返回值:uint16_t
*/
uint16_t version_str_to_hex(char *version)
{
// 合成 0x0103
return (major << 8) | minor;
}
-
-//恢复出厂重新配网 不需要重启设备
+/*
+ * 函数功能:清除配网信息
+ * 无返回值
+*/
void Esp32_Factory_Reset(void)
{
ESP_LOGI(COMMON_TAG,"Esp32_Factory_Reset!!%d %d",common_storage.net_info.serial_number_flag,common_storage.net_info.wifi_info_flag);
#include "iap.h"
#include "esp_ota_ops.h"
-#define PID_A00D 0
+#define PID_A00D 1
// 定义原始版本
#define FW_VERSION_B 0x0100
uint8_t is_connect_usb:1;
uint8_t reserved:4;
+ int tz_offset_min;
// UNIVERSAL
uint32_t heart_beat_interval; // ms
MCU_INFO_T mcu_info;
//获取时间戳单位是s
void int64_to_str(uint64_t ts, char *out,size_t out_len);
-time_t Get_Time_Stamp(void);
-void set_system_time_int_from_utc(int64_t utc_timestamp_sec, int timeZone);
+void Get_Time(struct tm *time_out);
+void set_system_time_int_from_utc(int64_t utc_timestamp_sec,const char *zone);
void set_system_time_str_from_utc(int64_t utc_timestamp_sec, const char *timeZone);
void network_request_replay(uint8_t status);
void print_chip_info(void);
uint16_t version_str_to_hex(char *version);
void Esp32_Factory_Reset(void);
+int parse_utc_offset_min(const char *zone_str);
+int8_t parse_utc_offset_2(const char *zone_str);
+int8_t Get_Time_Zone(void);
+int64_t Get_Time_Stamp(void);
#if ENABLE_SYNC_SNTP_TIME
void wait_for_sntp_sync(void);
#endif
#include "ble.h"
#include "n455_ota.h"
static const char *UART_TAG = "UART_TASK";
-static const char *COMMON_TAG = "UART_RECV";
+static const char *UART_SEND = "UART_SEND";
+static const char *UART_RECV = "UART_RECV";
// static const int uart_rx_max = UART_BUF_SIZE;
-static uint8_t uart_rx_data[UART_BUF_SIZE] = {0};
+
uint8_t uart_tx_queue_storage[UART_TX_QUEUE_LENGTH*UART_TX_QUEUE_ITEM_SIZE];
QueueHandle_t uart_tx_queue_handle;
StaticQueue_t uart_tx_queue_buffer;
// //
-uint8_t uart_rx_queue_storage[UART_RX_QUEUE_LENGTH*UART_RX_QUEUE_ITEM_SIZE];
-QueueHandle_t uart_rx_queue_handle;
-StaticQueue_t uart_rx_queue_buffer;
+// uint8_t uart_rx_queue_storage[UART_RX_QUEUE_LENGTH*UART_RX_QUEUE_ITEM_SIZE];
+// QueueHandle_t uart_rx_queue_handle;
+// StaticQueue_t uart_rx_queue_buffer;
// UART
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;
/*
func:uart初始化
*/
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);
+ // uart_rx_queue_handle = xQueueCreateStatic(UART_RX_QUEUE_LENGTH, UART_RX_QUEUE_ITEM_SIZE, uart_rx_queue_storage, &uart_rx_queue_buffer);
}
void Uart_Tx_Queue_Process(void)
// ESP_LOG_BUFFER_HEX(UART_TAG, uart_tx_data.buffer, uart_tx_data.size);
}
}
-#define RX_CACHE_MAX_LEN 256
+#define RX_CACHE_MAX_LEN 1024
//uart接收数据
static uint8_t rx_cache[RX_CACHE_MAX_LEN] = {0};
while(uart_buf_offset < rx_cache_len)
{
- // ESP_LOGI(COMMON_TAG, "uart_buf_offset recv2 %d %d",uart_buf_offset,uart_rx_size);
+ // ESP_LOGI(UART_RECV, "uart_buf_offset recv2 %d %d",uart_buf_offset,uart_rx_size);
if(rx_cache[uart_buf_offset] == FRAMEHEAD_PANEL_TO_IOT)
{
//uart_rx_data[uart_buf_offset+1] //data
{
break;
}
- // ESP_LOGI(COMMON_TAG, "uart_buf_offset recv3 %d %d",uart_buf_offset,uart_rx_size);
+ // ESP_LOGI(UART_RECV, "uart_buf_offset recv3 %d %d",uart_buf_offset,uart_rx_size);
//if(crcr8(data,len) == crc)
//f2 00 00 00 crc uart_buf_offset=0
//f2 00 01 0f c2
uint8_t crc_pkt = rx_cache[uart_buf_offset + full_len-1];//1+1+2=4
if(crc_cal == crc_pkt)
{
- // ESP_LOGI(COMMON_TAG, "uart_buf_offset recv5 %d %d",uart_buf_offset,uart_rx_size);
+ // ESP_LOGI(UART_RECV, "uart_buf_offset recv5 %d %d",uart_buf_offset,uart_rx_size);
Uart_Pairing_Handle(&rx_cache[uart_buf_offset],full_len, HOST_TYPE_UART);
}
}
-static volatile uint8_t ble_connected_notify_flag = 0;
-static uint8_t ble_connected_notify_count = 0;
-static uint8_t ble_connected_notify_retry = 0;
-void Uart_Set_Ble_Status(void)
-{
- ble_connected_notify_count=0;
- ble_connected_notify_retry=0;
- ble_connected_notify_flag = 1;
-}
// 直接解析完整帧
void Uart_Recv_Handle(uint8_t *data, uint16_t len)
{
}
}
#endif
+static volatile uint8_t ble_connected_notify_flag = 0;
+// static uint8_t ble_connected_first_flag = false;
+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_Set_Ble_Status(void)
+{
+ ESP_LOGI(UART_TAG,"Uart_Set_Ble_Status!!!!");
+ ble_connected_notify_count = 0;
+ ble_connected_notify_flag = 1;
+ // ble_connected_first_flag = 0;
+
+}
+void Uart_Clear_Ble_Status(void)
+{
+ // ble_connected_first_flag=0;
+ ble_connected_notify_flag = 0;
+ ble_connected_notify_count =0 ;
+}
+void Uart_Send_Task(void *arg)
+{
+ while (1)
+ {
+
+ Uart_Tx_Queue_Process();
+ vTaskDelay(5/ portTICK_PERIOD_MS);
+ }
+
+}
/*
func: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_INFO);
- memset(uart_rx_data, 0x00, sizeof(uart_rx_data));
// static UART_RECV_DATA_T uart_rx_data_struct = {0};
// static uint8_t tx_count = 0;
+ static uint32_t now = 0 ;
while (1)
{
- static uint8_t read_buf[256]={0};
- const int uart_rx_size = uart_read_bytes(UART_NUM_1, read_buf, UART_BUF_SIZE,0);//2/ portTICK_PERIOD_MS);
+ 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) //第一次发送
+ {
+ ESP_LOGI(UART_TAG,"first ble_connected_notify_flag!!!!");
+ 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);
+ }
+ }
+ }
+ // ESP_LOGI(UART_TAG, "uart %d", rx_cache_len);
+ const int uart_rx_size = uart_read_bytes(UART_NUM_1, read_buf, READ_BUF_MAX_LEN,2/ portTICK_PERIOD_MS);//2/ portTICK_PERIOD_MS);
if (uart_rx_size > 0)
{
// ESP_LOGI(UART_TAG, "Received %d bytes: %d\n", uart_rx_size,rx_timeout_cnt);
{
memcpy(&rx_cache[rx_cache_len], read_buf, uart_rx_size);
rx_cache_len += uart_rx_size;
- rx_timeout_cnt = 2;
-
}
-
+ rx_last_recv_tick = xTaskGetTickCount();
+ // ESP_LOGI(UART_TAG, "rx_last_recv_tick: %ld now:%ld", rx_last_recv_tick,now);
}
- if(rx_timeout_cnt > 0)
+ if (rx_cache_len > 0)
{
- rx_timeout_cnt--;
- if(rx_timeout_cnt == 0 && rx_cache_len > 0)
+ now = xTaskGetTickCount();
+ // ESP_LOGI(UART_TAG, "rx_last_recv_tick %ld bytes: %ld %ld\n", rx_last_recv_tick,now,now - rx_last_recv_tick);
+ if (now - rx_last_recv_tick >= 2)
{
+ // 超时 → 解析
+ // ESP_LOGI(UART_TAG, "Frame timeout, parse len: %d", rx_cache_len);
- Uart_Recv_Handle(rx_cache,rx_cache_len);
- rx_cache_len=0;
- memset(rx_cache,0x00,sizeof(rx_cache));
+ Uart_Recv_Handle(rx_cache, rx_cache_len);
+ rx_cache_len = 0;
+ memset(rx_cache, 0, sizeof(rx_cache));
}
}
- Uart_Tx_Queue_Process();
- if(ble_connected_notify_flag)
- {
- ble_connected_notify_count++;
- if(ble_connected_notify_count > 5)
- {
- ble_connected_notify_count=0;
- if(ble_connected_notify_retry < 3)
- {
- ble_connected_notify_retry++;
- Uart_Reply_Send(COMMON_UART_BLE_CONNECT_STATUS,NULL,0,HOST_TYPE_UART);
- }
- else{
- ble_connected_notify_retry=0;
- ble_connected_notify_flag = 0;
- }
- }
- }
+ // 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);
}
}
/*
-func:uart协议解析
-data:数据不包含crcr header
-len:纯数据长度
-host_type:预留
+ * 函数功能: uart协议解析
+ * data: 数据不包含crcr header
+ * len: 纯数据长度
+ * host_type:预留
*/
void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
{
{
if(data[1] != 0xb7)
{
- // ESP_LOG_BUFFER_HEX(COMMON_TAG,data,len);
+ // ESP_LOG_BUFFER_HEX(UART_RECV,data,len);
}
- // ESP_LOGI(COMMON_TAG, "Read %d bytes:", len);
- // ESP_LOG_BUFFER_HEX(COMMON_TAG, data, len);
+ // ESP_LOGI(UART_RECV, "Read %d bytes:", len);
+ // ESP_LOG_BUFFER_HEX(UART_RECV, data, len);
static UART_RECV_REPLY_T uart_pairing_reply = {0};
memset(&uart_pairing_reply, 0x00, sizeof(UART_RECV_REPLY_T));
if(data[2] >= 2)
{
common_setting.mcu_platform_mode = data[4];//mcu连接的pc的模式
- // ESP_LOGI(COMMON_TAG,"common_setting.mcu_platform_mode:%d\n",common_setting.mcu_platform_mode);
+ // ESP_LOGI(UART_RECV,"common_setting.mcu_platform_mode:%d\n",common_setting.mcu_platform_mode);
}
if(data[2] >= 3)
{
common_setting.is_connect_usb = data[5];//是否连接基座还是电脑
- // ESP_LOGI(COMMON_TAG,"common_setting.is_connect_usb:%d\n",common_setting.is_connect_usb);
+ // ESP_LOGI(UART_RECV,"common_setting.is_connect_usb:%d\n",common_setting.is_connect_usb);
}
if (len >= 12 && data[2] >= 9)
{
common_setting.mcu_info.vid = *(uint16_t*)&data[3];
- #if PID_A00D
+ #if 0
common_setting.mcu_info.pid = 0xA00D;//
#else
common_setting.mcu_info.pid = *(uint16_t*)&data[5];
common_setting.mcu_info.product_name_len = name_len;
common_setting.mcu_info_got_flag = 1;
uart_pairing_reply.content[0]=0x01;
- ESP_LOGI(COMMON_TAG, "start nimble 0x%04x 0x%04x 0x%04x 0x%04x %s\n",common_setting.mcu_info.vid,common_setting.mcu_info.pid,\
+ ESP_LOGI(UART_RECV, "start nimble 0x%04x 0x%04x 0x%04x 0x%04x %s\n",common_setting.mcu_info.vid,common_setting.mcu_info.pid,\
common_setting.mcu_info.fw_version,common_setting.mcu_info.build_version,common_setting.mcu_info.product_name);
#if !ENABLE_HTTP_N455_OTA_FUNC
Ble_Start_Nimble_NetWork();////开启蓝牙配网
}
else
{
- ESP_LOGI(COMMON_TAG, "data len %d, want %d\n", len, (12 + name_len));
+ ESP_LOGI(UART_RECV, "data len %d, want %d\n", len, (12 + name_len));
name_len = 0;
- uart_pairing_reply.content[0]=0x01;
+ uart_pairing_reply.content[0]=0x00;
}
}
}else{
- ESP_LOGI(COMMON_TAG, "mcu len %d",data[2]);
+ ESP_LOGI(UART_RECV, "mcu len %d",data[2]);
uart_pairing_reply.content[0]=0x00;
}
}
#endif
}
break;
-
+ case COMMON_UART_GET_TIMESTAMP_CMD:
+ {
+ uart_pairing_reply.send_flag = 1;
+ uart_pairing_reply.len = 9;
+ uart_pairing_reply.content[0] = 10;
+ uart_pairing_reply.content[1] = Get_Time_Zone();//时区
+ int64_t time = Get_Time_Stamp();
+ memcpy(&uart_pairing_reply.content[2],&time,8); //时间戳
+ }
+ break;
//
case COMMON_UART_FACTORY_RESET:
{
break;
case COMMON_UART_BLE_CONNECT_STATUS:
{
- ble_connected_notify_flag=0;
+ Uart_Clear_Ble_Status();
}
break;
// 其它命令
- case 0x20://APP检查到iot服务器地址不可达,通知iot模块停止配网,等待APP重新发起配网
+ case 0x20:
{
if(data[3] == 1) // 停止配网
{
break;
case COMMON_UART_MCU_FIRMWARE_UPDATE_CMD:
{
- // ESP_LOG_BUFFER_HEX(COMMON_TAG,data,len);
+ // ESP_LOG_BUFFER_HEX(UART_RECV,data,len);
#if ENABLE_HTTP_N455_OTA_FUNC
N455_Ota_Capture_Ack(data[1],data[4]);
#endif
}
break;
- case 0XB1:
+ case 0xB1: //这是USB通过串口给模组升级的指令 模组暂时不用 使用的是0xE0升级
{
- // ESP_LOG_BUFFER_HEX(COMMON_TAG,data,len);
+ // ESP_LOG_BUFFER_HEX(UART_RECV,data,len);
#if ENABLE_HTTP_N455_OTA_FUNC
N455_Ota_Capture_Ack(data[1],data[4]);
#endif
// Check_Host_Type(uart_pairing_reply.cmd, uart_pairing_reply.content, uart_pairing_reply.len, host_type);
}
- ESP_LOGI(COMMON_TAG, "uart recv %d bytes:cmd %0x", len, data[1]);
+ ESP_LOGI(UART_RECV, "uart recv %d bytes:cmd %0x", len, data[1]);
}
else if(data[0] == FRAMEHEAD_PANEL_TO_IOT)
{
- ESP_LOGI(COMMON_TAG, "net pairing crc8 %d bytes:cmd %0x", len, data[1]);
+ ESP_LOGI(UART_RECV, "net pairing crc8 %d bytes:cmd %0x", len, data[1]);
if(len < 4 || len > UART_BUF_SIZE)
{
- ESP_LOGE(COMMON_TAG, "net pairing len err:%d", len);
+ ESP_LOGE(UART_RECV, "net pairing len err:%d", len);
}
else if(Crc8_ROHC_Cal(&data[1], len-2) != data[len-1])
{
- ESP_LOGE(COMMON_TAG, "net pairing crc8 err:%02x %02x", Crc8_ROHC_Cal(&data[1], len-2), data[len-1]);
+ ESP_LOGE(UART_RECV, "net pairing crc8 err:%02x %02x", Crc8_ROHC_Cal(&data[1], len-2), data[len-1]);
}
}
}
host_type:预留
*/
//*pub不包含帧头 命令 crcr长度等协议字段,纯数据部分,len为数据部分长度
-void Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type)
+uint8_t Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type)
{
if (len > UART_REPLY_MAX_PAYLOAD-4) // 4 = header(1) + cmd(1) + len(1) + crc(1) 暂时len为1个字节
{
- ESP_LOGE(COMMON_TAG, "Uart too large: %d\n", len);
- return;
+ ESP_LOGE(UART_SEND, "Uart too large: %d\n", len);
+ return 0;
}
static UART_RECV_DATA_T uart_pairing_reply = { 0 };
uint8_t header[3] = {FRAMEHEAD_IOT_TO_PANEL, cmd, 0x00};
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);
+ // ESP_LOGI(UART_SEND, "%s crc %02x len %d",__func__, crc, len);
if(uart_tx_queue_handle == NULL)
{
- ESP_LOGI(COMMON_TAG, "uart_tx_queue_handle null");
- return;
+ ESP_LOGI(UART_SEND, "uart_tx_queue_handle null");
+ return 0;
+ }
+ ESP_LOGI(UART_SEND, "uart_pairing_reply:len %d cmd:%d",uart_pairing_reply.size,cmd);
+ if (xQueueSend(uart_tx_queue_handle, &uart_pairing_reply, 0) != pdPASS) {
+ ESP_LOGI(UART_SEND, "UART_SEND ERR\n");
+ return 0;
+ }else{
+ return 1;
}
- xQueueSend(uart_tx_queue_handle, &uart_pairing_reply, 0);
+
}
};
}__attribute__((packed)) UART_RECV_REPLY_T;
-#define UART_TX_QUEUE_LENGTH 4
+#define UART_TX_QUEUE_LENGTH 8
#define UART_TX_QUEUE_ITEM_SIZE (sizeof(UART_RECV_DATA_T))
#define UART_RX_QUEUE_LENGTH 4
extern StaticQueue_t uart_tx_queue_buffer;
-extern uint8_t uart_rx_queue_storage[UART_RX_QUEUE_LENGTH*UART_RX_QUEUE_ITEM_SIZE];
-extern QueueHandle_t uart_rx_queue_handle;
-extern StaticQueue_t uart_rx_queue_buffer;
+// extern uint8_t uart_rx_queue_storage[UART_RX_QUEUE_LENGTH*UART_RX_QUEUE_ITEM_SIZE];
+// extern QueueHandle_t uart_rx_queue_handle;
+// extern StaticQueue_t uart_rx_queue_buffer;
-#define UART_TASK_STACK (2*1024)
+#define UART_TASK_STACK (1024+256)
extern TaskHandle_t uart_task_handler;
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;
+
+
#define UART_REPLY_MAX_PAYLOAD UART_BUF_SIZE // 4 = header(1) + cmd(1) + len(1) + crc(1)
void Uart_Initialize(void);
void Uart_Tx_Queue_Process(void);
void Uart_Rx_Queue_Process(void);
+void Uart_Send_Task(void *arg);
void Uart_Task(void *arg);
-void Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type);
+uint8_t Uart_Reply_Send(uint8_t cmd, uint8_t* pbuf, uint8_t len, uint8_t host_type);
void Uart_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type);
-void Uart_Set_Ble_Status(void);
+void Uart_Set_Ble_Status(void); //设置蓝牙连接成功灯闪标识
+void Uart_Clear_Ble_Status(void);//清空蓝牙闪灯标识
#endif
//寻找分区表
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)
{
// "serialNumber": "PXN1LJ1QUR078E"
- cJSON* utc = cJSON_GetObjectItem(pData,"utc");
+ // cJSON* utc = cJSON_GetObjectItem(pData,"utc");
cJSON* utcOffset = cJSON_GetObjectItem(pData,"utcOffset");
cJSON* timestamp = cJSON_GetObjectItem(pData,"timestamp");
- if(utcOffset != NULL && cJSON_IsNumber(utcOffset) && timestamp != NULL && cJSON_IsString(timestamp))
+ if(utcOffset != NULL && cJSON_IsString(utcOffset) && timestamp != NULL && cJSON_IsString(timestamp))
{
- int64_t utc_timestamp = strtoll(timestamp->valuestring, NULL, 10); //这个是已经加了时区的时间戳
- int timezone = utcOffset->valueint;
-
- //服务器返回的timestamp已经是加了时区的时间戳了,如果直接用这个时间戳设置系统时间,系统时间就会比实际时间快一个时区的时间,所以需要把时区偏移去掉,得到真正的UTC时间戳,再设置系统时间
- int64_t real_utc = utc_timestamp - (timezone * 3600);
-
- set_system_time_int_from_utc(real_utc,timezone);
+ int64_t utc_timestamp = strtoll(timestamp->valuestring, NULL, 10); //不加时区的时间戳格林威治时间
+ //'+8' '-3.5'
+ set_system_time_int_from_utc(utc_timestamp,utcOffset->valuestring);
}
#include "project_common.h"
#include "n455_ota.h"
#if ENABLE_MQTT_FUNC
+
+#define ERR_CODE_OK 200
+#define ERR_PID_SERIALNUMBER_ERR_CODE 401
+#define ERR_JSON_CODE 402 //不是json字符串
+#define ERR_UART_SEND_ERR_CODE 600 //串口发送失败
+
+
+#define REPLY_STATUS_OK 1
+#define REPLY_STATUS_FAIL 0
+
static const char *MQTT_TAG = "MQTT";
static esp_mqtt_client_config_t mqtt_cfg = {0};
static esp_mqtt_client_handle_t mqtt_client_handle = NULL;
-#define REPLY_STATUS_OK 1
-#define REPLY_STATUS_FAIL 0
+
// 发送队列静态内存
uint8_t mqtt_dev_report_queue_storage[MQTT_SEND_QUEUE_LENGTH * sizeof(mqtt_dev_report_msg_t)];
QueueHandle_t mqtt_dev_report_queue_handle=NULL;
const char *suffix = NULL;
switch (type) {
case TOPIC_PROPERTY_POST: suffix = "/property/post" ; break; //云端
+ case TOPIC_PROPERTY_SET: suffix = "/property/set" ; break; //云端
+
case TOPIC_PROPERTY_REPLY: suffix = "/property/reply"; break; //设备
case TOPIC_PROPERTY_REPORT: suffix = "/property/report"; break; //设备
case TOPIC_SERVICE_POST: suffix = "/service/post"; break; //云端
const char *service_property_set="thing.service.property.set";
const char *property_set_replay="thing.service.property.set.reply";
+
+const char *service_property_get="thing.service.property.get";
+const char *service_property_get_replay="thing.service.property.get.reply";
+
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";
case METHON_SERVICE_PROPERTY_SET_REPLAY:
return property_set_replay;
+ case METHON_SERVICE_PROPERTY_GET:
+ return service_property_get;
+
+ case METHON_SERVICE_PROPERTY_GET_REPLAY:
+ return service_property_get_replay;
+
//设备事件主动上报methon
case METHON_EVENT_PROPERTY_POST:
return property_post;
#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);
+ ESP_LOGI(MQTT_TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id);
esp_mqtt_event_handle_t event = event_data;
esp_mqtt_client_handle_t mqtt_client = event->client;
int msg_id;
mqtt_send_data_count=0;
common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
common_setting.mqtt_user_cfg.mqtt_connect_flag = 1;
+ ESP_LOGI(MQTT_TAG, "send subscribe topic=%s common_setting.mqtt_user_cfg.mqtt_start_flag=%d",get_publish_topic(TOPIC_PROPERTY_SET),common_setting.mqtt_user_cfg.mqtt_start_flag);
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_CONNECTED");
// /{pid}/{serialNumber}/property/post
#if MQTT_USER_DEFAULT_TOPIC
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),"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, get_publish_topic(TOPIC_PROPERTY_REPLY), 1);
- // ESP_LOGI(MQTT_TAG, "订阅属性下发: msg_id=%d topic=%s", msg_id, sub_topic[0]);
-
-
+ ESP_LOGI(MQTT_TAG, "subscribe topic=set");
+ msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_PROPERTY_SET), 0);
+ ESP_LOGI(MQTT_TAG, "subscribe topic=post");
+ ESP_LOGI(MQTT_TAG, "send subscribe topic=%s common_setting.mqtt_user_cfg.mqtt_start_flag=%d",get_publish_topic(TOPIC_PROPERTY_POST),common_setting.mqtt_user_cfg.mqtt_start_flag);
msg_id = esp_mqtt_client_subscribe(mqtt_client,get_publish_topic(TOPIC_PROPERTY_POST), 0);
- ESP_LOGI(MQTT_TAG, "send subscribe msg_id=%d common_setting.mqtt_user_cfg.mqtt_start_flag =%s", msg_id,get_publish_topic(TOPIC_PROPERTY_POST),common_setting.mqtt_user_cfg.mqtt_start_flag);
+
if(common_setting.ble_status.is_bind == 1) // 连接mqtt请求一次http绑定接口做绑定 绑定失败配网失败 绑定成功配网成功
{
common_setting.ble_status.is_bind = 0;
#endif
break;
+
case MQTT_EVENT_DISCONNECTED:
common_setting.mqtt_user_cfg.mqtt_connect_flag = 0;
common_setting.mqtt_user_cfg.mqtt_sub_flag = 0;
mqtt_client_handle=NULL;
memset(&mqtt_cfg, 0, sizeof(mqtt_cfg));
retry_connect_cnt = 0;
+ mqtt_cfg.session.keepalive = 100; //50S
+ mqtt_cfg.session.disable_keepalive = 0;
// common_setting.mqtt_cfg.session.keepalive = 60;
mqtt_cfg.network.disable_auto_reconnect = false;
// common_setting.mqtt_cfg.broker.address.transport = MQTT_TRANSPORT_OVER_TCP;
#if MQTT_USER_DEFAULT_TOPIC
common_setting.mqtt_cfg.credentials.client_id = "basicPubSub";
#else
- mqtt_cfg.credentials.client_id = (const char *)pid_str;
+ // mqtt_cfg.credentials.client_id = (const char *)pid_str;
+ mqtt_cfg.credentials.client_id = (const char *)common_storage.net_info.serial_number;
#endif
ESP_LOGI(MQTT_TAG, "Mqtt init %s",pid_str);
+ mqtt_cfg.task.priority = 1;
mqtt_client_handle = esp_mqtt_client_init(&mqtt_cfg);
if(mqtt_client_handle == NULL){
ESP_LOGI(MQTT_TAG, "client_handle null\n");
cJSON_AddStringToObject(root, "serial_id", (char *)common_storage.net_info.serial_number);
cJSON_AddNumberToObject(root, "connected_platform", common_setting.mcu_platform_mode);
cJSON_AddNumberToObject(root, "power_on", 1);
- // cJSON *pRoot = cJSON_CreateObject();
- // // cJSON_AddItemToObject(root,""thingsModelValue": "string",");
- // if(pRoot == NULL)
- // {
- // cJSON_Delete(root);
- // return;
- // }
- // cJSON_AddItemToObject(root,"thingsModelValue",pRoot);
+ cJSON_AddNumberToObject(root, "9", common_setting.is_connect_usb);
// 生成压缩格式JSON(不带换行,HTTP专用)
{
// return;
// ESP_LOGI(MQTT_TAG,"HR!!!!!",mqtt_send_data_count);
- if(mqtt_send_data_count >= 60)
- {
-
- // ESP_LOGI(MQTT_TAG,"HR!!!!!");
- // Wifi_Stop();
- // Mqtt_Disconnect();
- // User_Erase_NotReset();
-
- Mqtt_Send_Mcu_Info();
- mqtt_send_data_count=0;
- }
- if(mqtt_send_data_count % 7 == 0)
+ // ESP_LOGI(MQTT_TAG,"HR!!!!!:%d",mqtt_send_data_count);
+ if(mqtt_send_data_count % 5 == 0)
{
- // uint16_t test_dpid[25]={335,336,336,337,338,339,340,341,400,401,745,746,747,271,272,273,274,275,745,746,275,276,277,278,279};
+ // ESP_LOGI(MQTT_TAG,"HR!!!11",mqtt_send_data_count);
+ // uint16_t test_dpid[81]= {745,777,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,271,272,273,274,275,276,277,
+ // 278,279,280,283,284,285,286,287,288,289,59,60,62,63,64,65,66,67,68,69,70,71,72,73,
+ // 745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786,745,777,779,780,781,782,783,784,785,786};
- // mqtt_get_dev_test(test_dpid,25);
- // mqtt_parse_recv_data(NULL,0);
- }
- else if(mqtt_send_data_count % 15 == 0)
- {
- // mqtt_parse_recv_data(NULL,1);
+ // mqtt_get_dev_test(test_dpid,80);
- }
+ // mqtt_get_dev_test(test_dpid,80);
+ // mqtt_get_dev_test(test_dpid,49);
+ }
+ if(mqtt_send_data_count >= 30)
+ {
+ mqtt_send_data_count = 0;
+ Mqtt_Send_Mcu_Info();
+ }
mqtt_send_data_count++;
}
/*
// 拷贝数据
memcpy(msg.data, data, data_len);
msg.data_len = data_len;
+ // ESP_LOGI(MQTT_TAG, "topic=%s data=%s\n",topic,data);
if(mqtt_recv_queue_handle){
+ UBaseType_t queue_size = uxQueueMessagesWaiting(mqtt_recv_queue_handle); // 当前已有几条消息
+ UBaseType_t queue_free = uxQueueSpacesAvailable(mqtt_recv_queue_handle); // 剩余空间
+
+ ESP_LOGW(MQTT_TAG, "queuremain = %d | remain=%d", queue_size, queue_free);
if (xQueueSend(mqtt_recv_queue_handle, &msg, 0) != pdPASS) {
- ESP_LOGW(MQTT_TAG, "mcuerr\n");
+ ESP_LOGE(MQTT_TAG, "mqtt recv ok\n");
+ }
+ else{
+ ESP_LOGI(MQTT_TAG,"mqtt recv queue is full");
}
}else{
- ESP_LOGI(MQTT_TAG,"mcusok");
+ ESP_LOGI(MQTT_TAG,"mqtt_recv_queue_handle is null");
}
}
void Mqtt_Connect_Status_Check(void)
{
static uint8_t mqtt_init = 0;
+ #if (!ENABLE_HTTP_N455_OTA_FUNC)
if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_CONNECT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_CONNECT_BIT) != 0 && common_setting.ble_status.ble_state == 0\
- &&(Https_Get_Ota_Status() == OTA_STATUS_IDLE
- #if ENABLE_HTTP_N455_OTA_FUNC
- || N455_Get_Ota_Status() == N455_OTA_STATUS_IDLE))
+ &&(Https_Get_Ota_Status() == OTA_STATUS_IDLE))
#else
- ))
+ if((xEventGroupWaitBits(common_event_group, COMMON_MQTT_CONNECT_BIT, pdFALSE, pdFALSE, 0) & COMMON_MQTT_CONNECT_BIT) != 0 && common_setting.ble_status.ble_state == 0\
+ &&(Https_Get_Ota_Status() == OTA_STATUS_IDLE || N455_Get_Ota_Status() == N455_OTA_STATUS_IDLE))
#endif
{
xEventGroupClearBits(common_event_group, COMMON_MQTT_CONNECT_BIT);
cJSON *params = cJSON_CreateArray();
cJSON_AddItemToObject(payload, "params", params);
- // 示例["345","346","347"]
- for(uint16_t i =17;i<28;i++)
- {
- char str[16];
- sprintf(str, "%d", i);
- cJSON *s = cJSON_CreateString(str);
- if (s) {
- cJSON_AddItemToArray(params, s);
- }
- }
-
// for(uint16_t i =843;i<878;i++)
// {
// char str[16];
- // // 示例:添加 { "736": "50" }
- // cJSON *p1 = cJSON_CreateObject();
- // cJSON_AddNumberToObject(p1, "736", 50);
- // cJSON_AddItemToArray(params, p1);
-
- // // 示例:添加 { "737": "89" }
- // cJSON *p2 = cJSON_CreateObject();
- // cJSON_AddNumberToObject(p2, "737", 89);
- // cJSON_AddItemToArray(params, p2);
-
+ // 示例:添加 { "736": "50" }
+ cJSON *p1 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p1, "59", 10);
+ cJSON_AddItemToArray(params, p1);
+
+ // 示例:添加 { "737": "89" }
+ cJSON *p2 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p2, "60", 20);
+ cJSON_AddItemToArray(params, p2);
+
+ // 示例:添加 { "737": "89" }
+ cJSON *p3 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p3, "61", 30);
+ cJSON_AddItemToArray(params, p3);
+ // 示例:添加 { "737": "89" }
+ cJSON *p4 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p4, "62", 40);
+ cJSON_AddItemToArray(params, p4);
+ // 示例:添加 { "737": "89" }
+ cJSON *p5 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p5, "63", 50);
+ cJSON_AddItemToArray(params, p5);
+ // 示例:添加 { "737": "89" }
+ cJSON *p6 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p6, "64", 60);
+ cJSON_AddItemToArray(params, p6);
+ // 示例:添加 { "737": "89" }
+ cJSON *p7 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p7, "65", 70);
+ cJSON_AddItemToArray(params, p7);
+ // 示例:添加 { "737": "89" }
+ cJSON *p8 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p8, "66", 80);
+ cJSON_AddItemToArray(params, p8);
+ // 示例:添加 { "737": "89" }
+ cJSON *p9 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p9, "67", 90);
+ cJSON_AddItemToArray(params, p9);
+ // 示例:添加 { "737": "89" }
+ cJSON *p10 = cJSON_CreateObject();
+ cJSON_AddNumberToObject(p10, "68", 100);
+ cJSON_AddItemToArray(params, p10);
+
+ mqtt_set_dev_property(params);
// ================== 打印看结果 ==================
char *out = cJSON_Print(root);
ESP_LOGI(MQTT_TAG,"%s\n", out);
-
+ free(out);
// 销毁
cJSON_Delete(root);
return out;
data:数据
data_len:数据长度
*/
-void mqtt_parse_recv_data(char *data, int data_len)
+void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len)
{
-
// // 安全判断
// if (buf == NULL || data_len <= 0 || data_len > MQTT_DATA_BUF_SIZE) {
// ESP_LOGW(MQTT_TAG, "parese too len:%d\n",data_len);
cJSON *payload_root = cJSON_GetObjectItem(root, "data");//
- if (payload_root == NULL && !cJSON_IsObject(payload_root )) {
-
+ if (payload_root == NULL && !cJSON_IsObject(payload_root )) {
+ ESP_LOGI(MQTT_TAG, "payload_root: ");
cJSON_Delete(root);
return;
}
-
-
-
- // 示例 2:
- //ota
- //{"firmwareId":25,"firmwareName":"wgtl","productId":"A00B","productName":"WGTL方向盘",
- //"tenantId":1,"tenantName":"13714325636","isLatest":1,"version":1.0,
- //"filePath":"https://pxn-iot.s3.us-east-2.amazonaws.com/iot01/2026/04/20/69e589eb7a67c1057c3d452e.bin"}
- //========OTA 升级文件key=====================//
- // {
+ // {
// "msg": "操作成功",
// "code": 200,
// "data": {
// "topic": "topic/PXN1LJ1QUR078E/A00B/property/post",
// "payload": {
- // "firmwareId": 25,
- // "firmwareName": "wgtl方向盘",
- // "productId": "A00B",
- // "productName": "WGTL方向盘",
- // "tenantId": 1,
- // "tenantName": "13714325636",
- // "isLatest": 1,
- // "version": 1.04,
- // "filePath": "https://pxn-iot.s3.us-east-2.amazonaws.com/iot01/2026/04/27/69ef076479113dba3ec8e375.bin"
+
+ // "msgId":"cloud_0196415f3b1f7e2ab7f72c4e8f91c2331",
+ // "productId": "A00D",
+ // "serialNumber": "PXN13BA1IJ7MB9" ,
+ // "time": 1779679394,
+ // "method": "thing.service.property.set",
+ // "params":[{"335":"#d9d40c"},{"336":"#d90c83"}]
// }
// }
// }
- // cJSON *payload = cJSON_GetObjectItem(root, "payload");//
-
//
//灯光
//{"productId":"A00B","productName":"WGTL方向盘","prot":[{"736":"50"},{"737":"89"},{"738":"90"}]}
// 示例 3:设备属性设置下行解析
//解析 Topic
- cJSON *topic = cJSON_GetObjectItem(payload_root, "topic");//
- if(topic == NULL || !cJSON_IsString(topic) || topic->valuestring == NULL)
- {
-
- cJSON_Delete(root);
- root = NULL;
- return;
- }
+
//解析payload
cJSON *payload = cJSON_GetObjectItem(payload_root, "payload");//
if(payload == NULL || !cJSON_IsObject(payload))
{
-
+ ESP_LOGI(MQTT_TAG, "payloa: ");
cJSON_Delete(root);
root = NULL;
return;
if (method == NULL || !cJSON_IsString(method) || method->valuestring == NULL) {
cJSON_Delete(root);
+ ESP_LOGI(MQTT_TAG, "method: ");
root = NULL;
return;
}
if (msgId == NULL || !cJSON_IsString(msgId) || msgId->valuestring == NULL) {
cJSON_Delete(root);
+ ESP_LOGI(MQTT_TAG, "msgId: ");
root = NULL;
return;
}
cJSON *productId = cJSON_GetObjectItem(payload, "productId");//
if (productId == NULL || !cJSON_IsString(productId) || productId->valuestring == NULL) {
- pid_serialNumber_replay_json(msgId->valuestring,NULL,method->valuestring,401);
+ pid_serialNumber_replay_json(msgId->valuestring,NULL,method->valuestring,ERR_PID_SERIALNUMBER_ERR_CODE);
cJSON_Delete(root);
root = NULL;
+ ESP_LOGI(MQTT_TAG, "productId: ");
return;
}
else{
snprintf(pid_str,sizeof(pid_str)-1,"%04X",common_setting.mcu_info.pid);
if(strcmp(pid_str,productId->valuestring) != 0)
{
- pid_serialNumber_replay_json(msgId->valuestring,NULL,method->valuestring,401);
+ pid_serialNumber_replay_json(msgId->valuestring,NULL,method->valuestring,ERR_PID_SERIALNUMBER_ERR_CODE);
cJSON_Delete(root);
root = NULL;
+ ESP_LOGI(MQTT_TAG, "productId 11: ");
return;
}
}
// "productId": "A00B",
// "code": 0
// }
- pid_serialNumber_replay_json(msgId->valuestring,NULL,method->valuestring,401);
+ pid_serialNumber_replay_json(msgId->valuestring,NULL,method->valuestring,ERR_PID_SERIALNUMBER_ERR_CODE);
cJSON_Delete(root);
+ ESP_LOGI(MQTT_TAG, "serialNumber: ");
root = NULL;
return;
}
else{
- char serialNumber_str[34] = {0};
- snprintf(serialNumber_str,sizeof(serialNumber_str)-1,"%s",(const char *)common_storage.net_info.serial_number);
- if(strcmp((const char *)serialNumber_str,serialNumber->valuestring) != 0)
+ // char serialNumber_str[SERIAL_NUMBER_MAX_LEN] = {0};
+ // snprintf(serialNumber_str,sizeof(common_storage.net_info.serial_number),"%s",(const char *)common_storage.net_info.serial_number);
+ if(strcmp((char *)common_storage.net_info.serial_number,serialNumber->valuestring) != 0)
{
- pid_serialNumber_replay_json(msgId->valuestring,NULL,method->valuestring,401);
+ pid_serialNumber_replay_json(msgId->valuestring,NULL,method->valuestring,ERR_PID_SERIALNUMBER_ERR_CODE);
cJSON_Delete(root);
root = NULL;
+ ESP_LOGI(MQTT_TAG, "serialNumber1111: ");
return;
}
}
//topic/PAXN1LJ1QUR0781/A00B/property/post
- // {
+ // data:{
// "msgId": "cloud_0196415f3b1f7e2ab7f72c4e8f91c0a1",
// "method": "thing.service.property.set",
// "time": 1777517820,
// }
// get_publish_topic(TOPIC_PROPERTY_POST,topic_buf,sizeof(topic_buf)); 属性设置
- if(strcmp(get_publish_topic(TOPIC_PROPERTY_POST),topic->valuestring) == 0)
+ if(strcmp(get_publish_topic(TOPIC_PROPERTY_SET),topic) == 0)
{
-
- strncpy(topic_compare,topic->valuestring,sizeof(topic_compare)-1);
+ ESP_LOGI(MQTT_TAG, "SET: ");
+ strncpy(topic_compare,topic,sizeof(topic_compare)-1);
if(strcmp(get_methon(METHON_SERVICE_PROPERTY_SET),method->valuestring) == 0) //"thing.service.property.set" 服务下发属性设备参数
{
// "message": "ok",
// }
- cJSON *replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY));
+
+ int ret = mqtt_set_dev_property(params);
+ int code = ERR_CODE_OK;
+ if(ret == 0)
+ {
+ code=ERR_UART_SEND_ERR_CODE;
+ }
+ cJSON *replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
if(replay_root)
{
+ mqtt_send_publish_json(TOPIC_PROPERTY_POST,replay_root);
cJSON_Delete(replay_root);
replay_root = NULL;
}
- mqtt_set_dev_property(params);
+ }
+ }
+ else if(strcmp(get_methon(METHON_SERVICE_PROPERTY_GET),method->valuestring) == 0) //"thing.service.property.set" 服务下发属性设备参数
+ {
+ ESP_LOGI(MQTT_TAG, "GET: ");
+ if(strlen(msgId->valuestring) != 0)
+ {
+
+ strncpy(publish_data.msgid, msgId->valuestring, sizeof(publish_data.msgid)-1);
+ cJSON *params = cJSON_GetObjectItem(payload, "params");//
+ if (params == NULL || !cJSON_IsArray(params)) {
+
+ cJSON_Delete(root);
+ root = NULL;
+ return;
+ }
+ // mqtt_get_dev_property(params);
+ // {
+ // "msgId": "cloud_0196415f3b1f7e2ab7f72c4e8f91c0a1",
+ // "method": "thing.service.property.set.reply",
+ // "time": 1777517821,
+ // "serialNumber": "PXN1LJ1QUR078E",
+ // "productId": "A00B",
+ // "code": 0,
+ // "message": "ok",
+
+ // }
+
+ int ret = mqtt_get_dev_property(params);
+ // int code = 0;
+ // if(ret == 0)
+ // {
+ // code=ERR_UART_SEND_ERR_CODE;
+ // }
+ // cJSON *replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_GET_REPLAY),code);
+ // if(replay_root)
+ // {
+ // mqtt_send_publish_json(TOPIC_PROPERTY_POST,replay_root);
+ // cJSON_Delete(replay_root);
+ // replay_root = NULL;
+ // }
}
}
}
// }
//"thing.service.calibration.invoke", //服务下发 设备应答
- else if(strcmp(get_publish_topic(TOPIC_SERVICE_POST),topic->valuestring) == 0)
+ else if(strcmp(get_publish_topic(TOPIC_SERVICE_POST),topic) == 0)
{
- strncpy(topic_compare,topic->valuestring,sizeof(topic_compare)-1);
+ strncpy(topic_compare,topic,sizeof(topic_compare)-1);
if(strcmp(get_methon(METHON_SERVICE_CALIBRATION_INVOKE),method->valuestring) == 0) //"thing.service.property.set" 服务下发属性设备参数
{
if(strlen(msgId->valuestring) != 0)
root = NULL;
return;
}
- mqtt_set_dev_property(input);
+ int ret = mqtt_set_dev_property(input);
+ int code = ERR_CODE_OK;
+ if(ret == 0) //串口发送失败
+ {
+ code = ERR_UART_SEND_ERR_CODE;
+ }
cJSON *replay_root = NULL;
cJSON *serviceId = cJSON_GetObjectItem(payload, "serviceId");//
if (serviceId == NULL || !cJSON_IsString(serviceId) || serviceId->valuestring == NULL) {
- replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY));
+ replay_root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
}else{
strncpy(publish_data.serviceId, msgId->valuestring, sizeof(publish_data.serviceId)-1);
- replay_root = create_publish_ack_json(publish_data.msgid,serviceId->valuestring,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY));
+ replay_root = create_publish_ack_json(publish_data.msgid,serviceId->valuestring,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY),code);
}
if(replay_root)
func:云端下发设置设备属性值
array:数组 [{"key":value},{"key":value},{"key":value}.............]
*/
-void mqtt_set_dev_property(const cJSON *array)
+uint8_t mqtt_set_dev_property(const cJSON *array)
{
- // {
+ // {
// "serialNumber": "PXN1LJ1QUR078E",
// "prot": [
// { "335": "#ffffff"},
if(array == NULL || !cJSON_IsArray(array))
{
- return;
+ return 0;
}
uint16_t array_size = cJSON_GetArraySize(array);
- if(array_size <= 0)return;
+ if(array_size <= 0)return 0;
- uint16_t total_data_len = 0;
+ uint8_t total_data_len = 0;
uint8_t send_data[UART_REPLY_MAX_PAYLOAD_MQTT];
memset(send_data,0,UART_REPLY_MAX_PAYLOAD_MQTT);
ESP_LOGI(MQTT_TAG,"array_size:%d",array_size);
+ uint8_t ret = 0;
+ uint8_t send_ok = 0;
for (uint16_t i = 0; i < array_size; i++) //0-65535
{
// 取数组第i个元素(是一个对象)
cJSON *pro_item = cJSON_GetArrayItem(array, i);
if (pro_item == NULL || !cJSON_IsObject(pro_item)) {
ESP_LOGI(MQTT_TAG,"%d not object\n", i);
+ ret = 1;
continue;
}
cJSON *item = pro_item->child;
//总的长度加上4+当前数据类型的长度
if(total_data_len + 5 + type_len > UART_REPLY_MAX_PAYLOAD_MQTT) //去掉header+cmd+len+crc
{
- ESP_LOGI(MQTT_TAG,"i:%d UART_REPLY_MAX_PAYLOAD_MQTT-5 %d len:%d",i,UART_REPLY_MAX_PAYLOAD_MQTT,total_data_len + 5 + type_len);
- Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART);
+
+ send_ok = Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART);
+ if(send_ok==0)
+ {
+ ret = 2;
+ break;
+ }
+ ESP_LOGI(MQTT_TAG,"i:%d UART_REPLY_MAX_PAYLOAD_MQTT-5 %d len:%d send_ok:%d",i,UART_REPLY_MAX_PAYLOAD_MQTT,total_data_len + 5 + type_len,send_ok);
total_data_len=0;
memset(send_data,0,UART_REPLY_MAX_PAYLOAD_MQTT);
- vTaskDelay(5);
+ vTaskDelay(10);
}
// 737 10 738 50 739 70
}
if(total_data_len > 0)//发送最后一包
{
+ if(ret != 1 )
+ {
+ if(ret == 2) //第一包发送失败
+ {
+ return 0;
+ }
+ send_ok = Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART);
+ ESP_LOGI(MQTT_TAG,"send ok:%d\n",send_ok);
+ return send_ok;
+ }else{
+ ESP_LOGI(MQTT_TAG,"send err\n");
+ return 0;
+ }
- Uart_Reply_Send(COMMON_UART_PROPERTY_SET_CMD,send_data, total_data_len,HOST_TYPE_UART);
- ESP_LOGI(MQTT_TAG,"send end\n");
+
}
+ return 1;
}
/*云端下发获取设备属性值
array:数组 ["key","key","key",.............]
*/
-void mqtt_get_dev_property(const cJSON *array)
+uint8_t mqtt_get_dev_property(const cJSON *array)
{
- if(array == NULL || !cJSON_IsArray(array)) return;
+ if(array == NULL || !cJSON_IsArray(array)) return 0;
uint16_t array_size = cJSON_GetArraySize(array);
- if (array_size <= 0) return;
+ if (array_size <= 0) return 0;
// 取数组第i个元素(是一字符串数字)
if(array_size > 0)
{
static uint8_t send_dpid_data[UART_REPLY_MAX_PAYLOAD_MQTT]={0};
memset(send_dpid_data,0,UART_REPLY_MAX_PAYLOAD_MQTT);
- uint16_t send_len = 0;
+ uint8_t send_len = 0;
// 字段 字节数 说明
// 帧头 1 模组到主控:0xF1
// 命令 1 0x09
// 数据长度 1 取决于数据字段长度
// 数据 N dpid 如:0x010203 表示查询3个dpid,分别为:0x01、0x02、0x03
// CRC校验 1 CRC8-ROHC
-
+ int ret = 0;
+ uint8_t send_ok = 1;
//["dpid1","dpid2","dpid3"...................]
for (int i = 0; i < array_size; i++)
{
// 取数组第i个元素(是一个对象)
cJSON *item = cJSON_GetArrayItem(array, i);
- ESP_LOGI(MQTT_TAG, " %d =%d | =%s",i,item ? item->type : -1,item ? item->valuestring : "null");
+ // ESP_LOGI(MQTT_TAG, " %d =%d | =%s",i,item ? item->type : -1,item ? item->valuestring : "null");
if (item == NULL || !cJSON_IsString(item) || item->valuestring == NULL) {
-
+ ESP_LOGI(MQTT_TAG, "%d",i);
continue;
}
if(!is_pure_number(item->valuestring)) continue;
}
if(send_len+sizeof(uint16_t) > UART_REPLY_MAX_PAYLOAD_MQTT)
{
- Uart_Reply_Send(COMMON_UART_PROPERTY_BATCH_REPORT_CMD,send_dpid_data, send_len,HOST_TYPE_UART);
+ vTaskDelay(10);
+ send_ok = Uart_Reply_Send(COMMON_UART_PROPERTY_BATCH_REPORT_CMD,send_dpid_data, send_len,HOST_TYPE_UART);
+ if(send_ok == 0)
+ {
+ ret=1;
+ break;
+ }
send_len=0;
memset(send_dpid_data,0,UART_REPLY_MAX_PAYLOAD_MQTT);
- vTaskDelay(10);
+
}
send_dpid_data[send_len++] = (dpid >> 8) & 0xFF;
send_dpid_data[send_len++] = dpid & 0xFF;
- ESP_LOGI(MQTT_TAG,"dpid:%d send_len %d\n",dpid,send_len);
+ // ESP_LOGI(MQTT_TAG,"dpid:%d send_len %d\n",dpid,send_len);
}
+ if(ret == 1)
+ {
+ return 0;
+ }
if(send_len > 0)
{
- Uart_Reply_Send(COMMON_UART_PROPERTY_BATCH_REPORT_CMD,send_dpid_data, send_len,HOST_TYPE_UART);
+ vTaskDelay(10);
+ send_ok = Uart_Reply_Send(COMMON_UART_PROPERTY_BATCH_REPORT_CMD,send_dpid_data, send_len,HOST_TYPE_UART);\
+
+ return send_ok;
send_len=0;
}
+ return 1;
+
}
+ return 1;
}
//
mqtt_send_msg_t send_msg={0};
memcpy(send_msg.data,json_str,strlen(json_str));
- ESP_LOGI(MQTT_TAG, "ack CJSON:%s %d", json_str,strlen(json_str));
+ ESP_LOGI(MQTT_TAG, "ack CJSON:%s %d topic=%s", json_str,strlen(json_str),topic);
free(json_str);
json_str = NULL;
{
return 0;
}
-
- if(user_esp_mqtt_client_publish(send_msg.data,strlen(send_msg.data),send_msg.topic) != pdTRUE)
+ int ret = user_esp_mqtt_client_publish(send_msg.data,strlen(send_msg.data),send_msg.topic);
+ if(ret == -1)
{
ESP_LOGI(MQTT_TAG, "ack faile \n");
return 0;
service_id: 传入服务ID 如 "calibration"
method: 传入方法名 如 "thing.service.calibration.invoke.reply"
*/
-cJSON* create_publish_ack_json(const char* msgId,const char* service_id, const char* method)
+cJSON* create_publish_ack_json(const char* msgId,const char* service_id, const char* method,uint16_t code)
{
cJSON *root = cJSON_CreateObject();
if (root == NULL)
}
- cJSON_AddNumberToObject(root, "code", 0); // 注意:code 是数字!
+ cJSON_AddNumberToObject(root, "code", code); // 注意:code 是数字!
cJSON_AddStringToObject(root, "message", "ok");
return root;
}
strncpy(publish_data.msgid,cloud_msgID,strlen(cloud_msgID));
uint8_t mode = 0; //0是使用云端生成下发的 属于应答 1 是使用设备端生成的 属于设备主动上报的
//应答
- if(strcmp(topic_compare,get_methon(METHON_SERVICE_PROPERTY_SET)) == 0) //属性设置需要应答
+
+ if(strcmp(topic_compare,get_publish_topic(TOPIC_PROPERTY_SET)) == 0) //属性设置需要应答
{
+ ESP_LOGI(MQTT_TAG,"TOPIC_PROPERTY_SET:");
mode=MODE_ACK;
}
{
mode=MODE_REPORT;
}
-
+ ESP_LOGI(MQTT_TAG,"mode:%d %s",mode,topic_compare);
// {
// "msgId": "dev_078E_12_1777518245123_0007",
// "method": "thing.event.property.post",
switch (mode)
{
case MODE_ACK:
- root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY));
+ root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_GET_REPLAY),ERR_CODE_OK);
/* code */
break;
case MODE_REPORT:
// ESP_LOGI(MQTT_TAG,"offset+5=%d, len=%d\n",offset+5,len);
break;
}
- // 1. 取 DPID (2个字节) //大端序
+ // 1. 取 DPID (2个字节) //
uint16_t dpid = data[offset + 0] | (data[offset + 1] << 8);
uint8_t data_type = data[offset+2];//type
switch (mode)
{
case MODE_ACK:
- mqtt_send_publish_json(TOPIC_PROPERTY_REPLY,root);
+ mqtt_send_publish_json(TOPIC_PROPERTY_POST,root);
break;
case MODE_REPORT:
break;
}
- vTaskDelay(1);
+ vTaskDelay(5);
// 销毁旧包,建新包
cJSON_Delete(root);
root = NULL;
switch (mode)
{
case MODE_ACK:
- root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_SET_REPLAY));
+ root = create_publish_ack_json(publish_data.msgid,NULL,get_methon(METHON_SERVICE_PROPERTY_GET_REPLAY),ERR_CODE_OK);
/* code */
break;
case MODE_REPORT:
switch (mode)
{
case MODE_ACK:
- mqtt_send_publish_json(TOPIC_PROPERTY_REPLY,root);
+ mqtt_send_publish_json(TOPIC_PROPERTY_POST,root);
/* code */
break;
case MODE_REPORT:
}
-
/*
func:uart发送queue给mqtt进行数据解析
cmd: 命令保留
return 1;
}
-
/*
func:用户封装mqtt发送函数
*/
}
int ret = esp_mqtt_client_publish(mqtt_client_handle,topic,\
data,data_len,0, 0 );
+ ESP_LOGI(MQTT_TAG, "user_esp_mqtt_client_publish ret :%d\n",ret);
return ret;
}
while (1)
{
if(mqtt_recv_queue_handle != NULL){
- if(xQueueReceive(mqtt_recv_queue_handle, &recv_msg, portMAX_DELAY) == pdTRUE)
+ if(xQueueReceive(mqtt_recv_queue_handle, &recv_msg, 0) == pdTRUE)
{
- ESP_LOGI(MQTT_TAG, "mqtt recv data");
- ESP_LOGI(MQTT_TAG, "主题:%d", recv_msg.topic_len);
- ESP_LOGI(MQTT_TAG, "数据:%d", recv_msg.data_len);
- mqtt_parse_recv_data(recv_msg.data,recv_msg.data_len);
+ // ESP_LOGI(MQTT_TAG, "mqtt recv data");
+ // ESP_LOGI(MQTT_TAG, "主题:%d", recv_msg.topic_len);
+ // ESP_LOGI(MQTT_TAG, "数据:%d", recv_msg.data_len);
+ mqtt_parse_recv_data(recv_msg.topic,recv_msg.topic_len,recv_msg.data,recv_msg.data_len);
// =============================================
// 1. JSON 解析
}
//设备上报
if(mqtt_dev_report_queue_handle != NULL){
- if (xQueueReceive(mqtt_dev_report_queue_handle, &send_msg, portMAX_DELAY) == pdTRUE) {
+ if (xQueueReceive(mqtt_dev_report_queue_handle, &send_msg, 0) == pdTRUE) {
if (Mqtt_Get_Connect_Flag())
{
switch (send_msg.cmd)
}
}
}
- vTaskDelay(10 / portTICK_PERIOD_MS);
+ vTaskDelay(2/ portTICK_PERIOD_MS);
}
}
#include "project_common.h"
#define ENABLE_MQTT_FUNC 1
#if ENABLE_MQTT_FUNC
-#define UART_REPLY_MAX_PAYLOAD_MQTT UART_REPLY_MAX_PAYLOAD-4
+#define UART_REPLY_MAX_PAYLOAD_MQTT UART_REPLY_MAX_PAYLOAD-8
typedef enum{
METHON_SERVICE_PROPERTY_SET=0,//服务属性设置下发methon
METHON_SERVICE_PROPERTY_SET_REPLAY, //设备属性设置应答
+
+
+ METHON_SERVICE_PROPERTY_GET,//属性获取
+ METHON_SERVICE_PROPERTY_GET_REPLAY, //设备属性获取应答
METHON_EVENT_PROPERTY_POST, //设备属性事件主动上报
TOPIC_PROPERTY_POST=0, //属性下发
TOPIC_PROPERTY_REPLY, // 属性应答
+ TOPIC_PROPERTY_SET,
+
TOPIC_PROPERTY_REPORT, // 主动上报
TOPIC_SERVICE_POST, //服务下发
#define MQTT_TOPIC_LEN 64
#define MQTT_MAX_TRY_CONNECT_TIME 10
#define MQTT_DATA_BUF_SIZE MQTT_SEND_RECV_DATA_BUF_SIZE
-#define MQTT_RECV_QUEUE_LENGTH 4
+#define MQTT_RECV_QUEUE_LENGTH 8
#define MQTT_SEND_QUEUE_LENGTH 4
#define MQTT_SEND_MSG_MAX_LEN MQTT_SEND_RECV_DATA_BUF_SIZE
void Mqtt_Send_Mcu_Info(void);
uint8_t Mqtt_Get_Connect_Flag(void);
void Mqtt_Send_Connect_Status(void);//发送mqtt连接状态给主控
-void mqtt_set_dev_property(const cJSON *array);
-void mqtt_get_dev_property(const cJSON *array);
+uint8_t mqtt_set_dev_property(const cJSON *array);
+uint8_t mqtt_get_dev_property(const cJSON *array);
uint8_t dev_report_property(uint8_t cmd,uint8_t *data,uint16_t len);
uint8_t uart_send_iot_data(uint8_t cmd,uint8_t *data,uint16_t len);
-cJSON* create_publish_ack_json(const char* msgId,const char* service_id, const char* method);
+cJSON* create_publish_ack_json(const char* msgId,const char* service_id, const char* method,uint16_t code);
cJSON* create_publish_post_json(const char* msgId,const char* service_id, const char* method);
cJSON* pid_serialNumber_replay_json(const char* msgId,const char* service_id, const char* method,uint16_t code);
uint8_t mqtt_send_publish_json(TOPIC_TYPE topic_type,cJSON *root);
-void mqtt_parse_recv_data(char *data, int data_len);
+void mqtt_parse_recv_data(char *topic, int topic_len,char *data, int data_len);
void mqtt_check_isconnect(void);
#endif
#endif
\ No newline at end of file