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