From 044b3a2b6f071b94bd0abf1084e304789f29293e Mon Sep 17 00:00:00 2001 From: xin Date: Wed, 29 Jul 2026 18:05:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=97=B6=E9=97=B4=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E4=B8=8D=E9=87=8D=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Myhttpserver.h | 9 +++- src/SensorOptoSky.cpp | 3 +- src/main.cpp | 106 ++++++++++++++++++++++++++---------------- src/slave.cpp | 20 ++++++-- src/slave.h | 2 + src/updatebyme.h | 2 +- 6 files changed, 94 insertions(+), 48 deletions(-) diff --git a/src/Myhttpserver.h b/src/Myhttpserver.h index b984ca0..1f262ae 100644 --- a/src/Myhttpserver.h +++ b/src/Myhttpserver.h @@ -7,13 +7,18 @@ #include "log.h" void Reuploaddata1(void *); void Reuploaddata2() ; -// static String Http_Sepctral_Path="/DDY/weather/php/SpectralDataUp.php"; -// static String Http_Wind_Path="/DDY/weather/php/WindsensorUp.php"; + +#ifdef DDYDevice +static String Http_Sepctral_Path="/DDY/weather/php/SpectralDataUp.php"; + + static String Http_Wind_Path="/DDY/weather/php/WindsensorUp.php"; +#else static String Http_Sepctral_Path="/weather/php/SpectralDataUp.php"; static String Http_Wind_Path="/weather/php/WindsensorUp.php"; +#endif static String Http_Log_Path="/weather/php/log.php"; static String Http_Sensorinfo_UP_Path="/weather/php/SensorInfoUp.php"; static String Http_Station_GPS_INfo_Path="/weather/php/StationGPSinfo.php"; diff --git a/src/SensorOptoSky.cpp b/src/SensorOptoSky.cpp index 57a05b7..f95960d 100644 --- a/src/SensorOptoSky.cpp +++ b/src/SensorOptoSky.cpp @@ -47,7 +47,7 @@ STRSensorInfo SensorOptoSky::GetSensorInfo() { setem.SensorName=str; if(setem.SensorName=="") { - setem.SensorName=="ATP1010"; + setem.SensorName="ATP1010"; } #ifdef DINBIAO @@ -167,6 +167,7 @@ void SensorOptoSky::GetOneDate(int msc) { memcpy(DATABUFF, IS1Sensor.result + 1, SensorInfo.BandNum * 2); //memcpy(DATABUFF, IS1Sensor.result , SensorInfo.BandNum * 2); shortLittletoBiG(DATABUFF, SensorInfo.BandNum); + delete[] shutter; esp_task_wdt_reset(); //手动喂狗 } diff --git a/src/main.cpp b/src/main.cpp index 5e10c90..709b0f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ //myPort将原来ESP8266对内通讯改为,485对内的通讯 // #define MYPORT_TX 45 // #define MYPORT_RX 46 -#define WATCHDOG_TIMEOUT_SECONDS 600 +#define WATCHDOG_TIMEOUT_SECONDS 600//600 int hassend = 0; GSMMannger *gsmmanger; UpDateClassByme *ProgrameUper; @@ -200,10 +200,16 @@ String get_GPS(void) // } // #include "esp_task_wdt.h" // 用于存储 loop 任务的句柄(如果需要) -TaskHandle_t loopTaskHandle = NULL; +TaskHandle_t loopTaskHandlemy = NULL; #include #include #include + + void IRAM_ATTR my_wdt_isr(void) { + esp_restart(); + } + + void setup() { @@ -214,6 +220,37 @@ void setup() //esp_task_wdt_init(20, true); Serial.begin(460800); Serial.println("start"); + + // --- 转换为毫秒 --- + const uint32_t WATCHDOG_TIMEOUT_MS = WATCHDOG_TIMEOUT_SECONDS * 1000; + + // ------------------- 配置 TWDT ------------------- + + // 1. 定义配置结构体 + esp_task_wdt_config_t twdt_config = { + .timeout_ms = WATCHDOG_TIMEOUT_MS, // 设置超时时间(毫秒) + + // 监控核心 0 和核心 1 的空闲任务。 + // 在 Arduino 环境中,通常 loop() 运行在核心 1 上 (如果启用了双核), + // 核心 0 处理 WiFi/蓝牙/网络栈。监控两者是系统级别的健壮做法。 + // (1 << 0) | (1 << 1) == 0b11 == 3 + .idle_core_mask = (1 << 0) | (1 << 1), + + // 当 TWDT 超时时,触发系统复位(false),而不是触发软件 panic(true)。 + .trigger_panic = true, + }; + esp_task_wdt_deinit(); + esp_err_t err = esp_task_wdt_init(&twdt_config); + + if (err != ESP_OK) { + Serial.printf("TWDT initialization failed: %s\n", esp_err_to_name(err)); + // 如果 TWDT 初始化失败,可能需要重启或报警 + return; + } + + // return; + + //return; Serial.println(Curentvsion); sdcard::lock(); @@ -408,46 +445,35 @@ void setup() log_data = "System Init Success"; write_log(log_path,10,"-------------------------------------------------system has been restart-----------------------------------------------------"); - // --- 转换为毫秒 --- - const uint32_t WATCHDOG_TIMEOUT_MS = WATCHDOG_TIMEOUT_SECONDS * 1000; - - // ------------------- 配置 TWDT ------------------- - - // 1. 定义配置结构体 - esp_task_wdt_config_t twdt_config = { - .timeout_ms = WATCHDOG_TIMEOUT_MS, // 设置超时时间(毫秒) - - // 监控核心 0 和核心 1 的空闲任务。 - // 在 Arduino 环境中,通常 loop() 运行在核心 1 上 (如果启用了双核), - // 核心 0 处理 WiFi/蓝牙/网络栈。监控两者是系统级别的健壮做法。 - // (1 << 0) | (1 << 1) == 0b11 == 3 - .idle_core_mask = (1 << 0) | (1 << 1), - - // 当 TWDT 超时时,触发系统复位(false),而不是触发软件 panic(true)。 - .trigger_panic = false, - }; - esp_err_t err = esp_task_wdt_init(&twdt_config); - if (err != ESP_OK) { - Serial.printf("TWDT initialization failed: %s\n", esp_err_to_name(err)); - // 如果 TWDT 初始化失败,可能需要重启或报警 - return; - } - - // 3. 将当前的 loop 任务(运行你的 getDateFromHttp 的任务)添加到 TWDT 监控列表 - loopTaskHandle = xTaskGetCurrentTaskHandle(); - if (esp_task_wdt_add(loopTaskHandle) != ESP_OK) { + +} +String lastdate=""; +dateandtime temnow = {2025, 1, 1, 0, 0, 0}; +bool isinitnow=false; +void loop() +{ + if(isinitnow==false){ + + + // 3. 将当前的 loop 任务(运行你的 getDateFromHttp 的任务)添加到 TWDT 监控列表 + const uint32_t WATCHDOG_TIMEOUT_MS = WATCHDOG_TIMEOUT_SECONDS * 1000; + loopTaskHandlemy = xTaskGetCurrentTaskHandle(); + + if (esp_task_wdt_add(loopTaskHandlemy) != ESP_OK) { Serial.println("Failed to add loop task to TWDT!"); } else { Serial.printf("TWDT initialized and monitoring loop task for %d ms (%d s)\n", WATCHDOG_TIMEOUT_MS, WATCHDOG_TIMEOUT_SECONDS); } -} -String lastdate=""; -dateandtime temnow = {2025, 1, 1, 0, 0, 0}; + isinitnow=true; + } + //esp_task_wdt_reset(); + // Serial.println("loop"); + // vTaskDelay(2000); + // return; -void loop() -{ +esp_task_wdt_reset(); //重置看门狗计时器,告诉它系统还在正常运行 if (xEventGroupGetBits(Eventgroup) & STOP_WORK_BIT) { Serial.println("system stop work NOW!!"); @@ -474,7 +500,7 @@ void loop() } - + String Date; /////////////////NOW///////////////////// @@ -484,14 +510,14 @@ void loop() //write_log(log_path,10,Date); String yuliang = String(myslave.getYuliang()); - delay(1010); + vTaskDelay(1010); // Serial.println("yuliang "+yuliang); write_log(log_path,10,"getYuliang :"+yuliang); /////////////////NOW///////////////////// String yuliangfz = String(myslave.getYuliang()); - delay(1010); + vTaskDelay(1010); // Serial.println("fuzhao " + yuliangfz); log_data ="fuzhao :"+yuliangfz; @@ -549,13 +575,13 @@ void loop() }else{ Date="error"; } - + write_log(log_path,10,"finish getnetData() date is "+Date); if(Date == "error") { write_log(log_path,10,"start get_GPS()"); Date = get_GPS(); - if(Date != "-1" & IsNetOK) + if(Date != "-1" && IsNetOK) { break; if(hassend == 0) diff --git a/src/slave.cpp b/src/slave.cpp index 757fa1f..9d5ec33 100644 --- a/src/slave.cpp +++ b/src/slave.cpp @@ -64,7 +64,8 @@ double slave::getMLX(){ void slave::getWehter(){ - while (my485Port->read()!=-1); + // while (my485Port->read()!=-1); + clearSerialBuffer(); my485Port->write(a,8); delay(90); for (int j = 0; j <17 ; ++j) { @@ -84,7 +85,8 @@ void slave::getWehter(){ } float slave::getYuliang(){ - while (my485Port->read()!=-1); + //while (my485Port->read()!=-1); + clearSerialBuffer(); my485Port->write(yuliang,8); delay(90); @@ -103,7 +105,8 @@ float slave::getYuliang(){ int32_t slave::getYuliangfuliang(){ - while (my485Port->read()!=-1); + //while (my485Port->read()!=-1); + clearSerialBuffer(); my485Port->write(yuliangfuliangdu,8); delay(90); @@ -122,7 +125,8 @@ int32_t slave::getYuliangfuliang(){ /// 返回1,表示ok uint8_t slave::claeryuliang(){ - while (my485Port->read()!=-1); + clearSerialBuffer(); + //while (my485Port->read()!=-1); my485Port->write(ClearYL,8); delay(90); for (int j = 0; j <8; ++j) { @@ -132,4 +136,12 @@ uint8_t slave::claeryuliang(){ return 1; } + void slave::clearSerialBuffer() { + unsigned long startTime = millis(); + while (my485Port->read() != -1) { + if (millis() - startTime > 1000) { // 超时保护 100ms + break; + } + } + } diff --git a/src/slave.h b/src/slave.h index dd40da2..9b4e34d 100644 --- a/src/slave.h +++ b/src/slave.h @@ -23,6 +23,8 @@ class slave int32_t getYuliangfuliang(); uint8_t claeryuliang(); bool isinit = false; + private: + void clearSerialBuffer(); }; diff --git a/src/updatebyme.h b/src/updatebyme.h index 1603bca..abcb844 100644 --- a/src/updatebyme.h +++ b/src/updatebyme.h @@ -1,7 +1,7 @@ #ifndef UPDATEBYME_H #define UPDATEBYME_H #include -#include +#include class UpDateClassByme{ public: UpdateClass updater;