解决时间重启不重启

This commit is contained in:
xin
2026-07-29 18:05:56 +08:00
parent 045300359c
commit 044b3a2b6f
6 changed files with 94 additions and 48 deletions

View File

@ -7,13 +7,18 @@
#include "log.h" #include "log.h"
void Reuploaddata1(void *); void Reuploaddata1(void *);
void Reuploaddata2() ; 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_Sepctral_Path="/weather/php/SpectralDataUp.php";
static String Http_Wind_Path="/weather/php/WindsensorUp.php"; static String Http_Wind_Path="/weather/php/WindsensorUp.php";
#endif
static String Http_Log_Path="/weather/php/log.php"; static String Http_Log_Path="/weather/php/log.php";
static String Http_Sensorinfo_UP_Path="/weather/php/SensorInfoUp.php"; static String Http_Sensorinfo_UP_Path="/weather/php/SensorInfoUp.php";
static String Http_Station_GPS_INfo_Path="/weather/php/StationGPSinfo.php"; static String Http_Station_GPS_INfo_Path="/weather/php/StationGPSinfo.php";

View File

@ -47,7 +47,7 @@ STRSensorInfo SensorOptoSky::GetSensorInfo() {
setem.SensorName=str; setem.SensorName=str;
if(setem.SensorName=="") if(setem.SensorName=="")
{ {
setem.SensorName=="ATP1010"; setem.SensorName="ATP1010";
} }
#ifdef DINBIAO #ifdef DINBIAO
@ -167,6 +167,7 @@ void SensorOptoSky::GetOneDate(int msc) {
memcpy(DATABUFF, IS1Sensor.result + 1, SensorInfo.BandNum * 2); memcpy(DATABUFF, IS1Sensor.result + 1, SensorInfo.BandNum * 2);
//memcpy(DATABUFF, IS1Sensor.result , SensorInfo.BandNum * 2); //memcpy(DATABUFF, IS1Sensor.result , SensorInfo.BandNum * 2);
shortLittletoBiG(DATABUFF, SensorInfo.BandNum); shortLittletoBiG(DATABUFF, SensorInfo.BandNum);
delete[] shutter;
esp_task_wdt_reset(); //手动喂狗 esp_task_wdt_reset(); //手动喂狗
} }

View File

@ -23,7 +23,7 @@
//myPort将原来ESP8266对内通讯改为,485对内的通讯 //myPort将原来ESP8266对内通讯改为,485对内的通讯
// #define MYPORT_TX 45 // #define MYPORT_TX 45
// #define MYPORT_RX 46 // #define MYPORT_RX 46
#define WATCHDOG_TIMEOUT_SECONDS 600 #define WATCHDOG_TIMEOUT_SECONDS 600//600
int hassend = 0; int hassend = 0;
GSMMannger *gsmmanger; GSMMannger *gsmmanger;
UpDateClassByme *ProgrameUper; UpDateClassByme *ProgrameUper;
@ -200,10 +200,16 @@ String get_GPS(void)
// } // }
// #include "esp_task_wdt.h" // #include "esp_task_wdt.h"
// 用于存储 loop 任务的句柄(如果需要) // 用于存储 loop 任务的句柄(如果需要)
TaskHandle_t loopTaskHandle = NULL; TaskHandle_t loopTaskHandlemy = NULL;
#include <esp_task_wdt.h> #include <esp_task_wdt.h>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
void IRAM_ATTR my_wdt_isr(void) {
esp_restart();
}
void setup() void setup()
{ {
@ -214,6 +220,37 @@ void setup()
//esp_task_wdt_init(20, true); //esp_task_wdt_init(20, true);
Serial.begin(460800); Serial.begin(460800);
Serial.println("start"); 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; //return;
Serial.println(Curentvsion); Serial.println(Curentvsion);
sdcard::lock(); sdcard::lock();
@ -408,46 +445,35 @@ void setup()
log_data = "System Init Success"; log_data = "System Init Success";
write_log(log_path,10,"-------------------------------------------------system has been restart-----------------------------------------------------"); 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 = { String lastdate="";
.timeout_ms = WATCHDOG_TIMEOUT_MS, // 设置超时时间(毫秒) dateandtime temnow = {2025, 1, 1, 0, 0, 0};
bool isinitnow=false;
void loop()
{
if(isinitnow==false){
// 监控核心 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 监控列表 // 3. 将当前的 loop 任务(运行你的 getDateFromHttp 的任务)添加到 TWDT 监控列表
loopTaskHandle = xTaskGetCurrentTaskHandle(); const uint32_t WATCHDOG_TIMEOUT_MS = WATCHDOG_TIMEOUT_SECONDS * 1000;
loopTaskHandlemy = xTaskGetCurrentTaskHandle();
if (esp_task_wdt_add(loopTaskHandle) != ESP_OK) { if (esp_task_wdt_add(loopTaskHandlemy) != ESP_OK) {
Serial.println("Failed to add loop task to TWDT!"); Serial.println("Failed to add loop task to TWDT!");
} else { } else {
Serial.printf("TWDT initialized and monitoring loop task for %d ms (%d s)\n", Serial.printf("TWDT initialized and monitoring loop task for %d ms (%d s)\n",
WATCHDOG_TIMEOUT_MS, WATCHDOG_TIMEOUT_SECONDS); WATCHDOG_TIMEOUT_MS, WATCHDOG_TIMEOUT_SECONDS);
} }
} isinitnow=true;
String lastdate=""; }
dateandtime temnow = {2025, 1, 1, 0, 0, 0}; //esp_task_wdt_reset();
// Serial.println("loop");
// vTaskDelay(2000);
// return;
void loop() esp_task_wdt_reset(); //重置看门狗计时器,告诉它系统还在正常运行
{
if (xEventGroupGetBits(Eventgroup) & STOP_WORK_BIT) if (xEventGroupGetBits(Eventgroup) & STOP_WORK_BIT)
{ {
Serial.println("system stop work NOW!!"); Serial.println("system stop work NOW!!");
@ -484,14 +510,14 @@ void loop()
//write_log(log_path,10,Date); //write_log(log_path,10,Date);
String yuliang = String(myslave.getYuliang()); String yuliang = String(myslave.getYuliang());
delay(1010); vTaskDelay(1010);
// Serial.println("yuliang "+yuliang); // Serial.println("yuliang "+yuliang);
write_log(log_path,10,"getYuliang :"+yuliang); write_log(log_path,10,"getYuliang :"+yuliang);
/////////////////NOW///////////////////// /////////////////NOW/////////////////////
String yuliangfz = String(myslave.getYuliang()); String yuliangfz = String(myslave.getYuliang());
delay(1010); vTaskDelay(1010);
// Serial.println("fuzhao " + yuliangfz); // Serial.println("fuzhao " + yuliangfz);
log_data ="fuzhao :"+yuliangfz; log_data ="fuzhao :"+yuliangfz;
@ -549,13 +575,13 @@ void loop()
}else{ }else{
Date="error"; Date="error";
} }
write_log(log_path,10,"finish getnetData() date is "+Date);
if(Date == "error") if(Date == "error")
{ {
write_log(log_path,10,"start get_GPS()"); write_log(log_path,10,"start get_GPS()");
Date = get_GPS(); Date = get_GPS();
if(Date != "-1" & IsNetOK) if(Date != "-1" && IsNetOK)
{ {
break; break;
if(hassend == 0) if(hassend == 0)

View File

@ -64,7 +64,8 @@ double slave::getMLX(){
void slave::getWehter(){ void slave::getWehter(){
while (my485Port->read()!=-1); // while (my485Port->read()!=-1);
clearSerialBuffer();
my485Port->write(a,8); my485Port->write(a,8);
delay(90); delay(90);
for (int j = 0; j <17 ; ++j) { for (int j = 0; j <17 ; ++j) {
@ -84,7 +85,8 @@ void slave::getWehter(){
} }
float slave::getYuliang(){ float slave::getYuliang(){
while (my485Port->read()!=-1); //while (my485Port->read()!=-1);
clearSerialBuffer();
my485Port->write(yuliang,8); my485Port->write(yuliang,8);
delay(90); delay(90);
@ -103,7 +105,8 @@ float slave::getYuliang(){
int32_t slave::getYuliangfuliang(){ int32_t slave::getYuliangfuliang(){
while (my485Port->read()!=-1); //while (my485Port->read()!=-1);
clearSerialBuffer();
my485Port->write(yuliangfuliangdu,8); my485Port->write(yuliangfuliangdu,8);
delay(90); delay(90);
@ -122,7 +125,8 @@ int32_t slave::getYuliangfuliang(){
/// 返回1,表示ok /// 返回1,表示ok
uint8_t slave::claeryuliang(){ uint8_t slave::claeryuliang(){
while (my485Port->read()!=-1); clearSerialBuffer();
//while (my485Port->read()!=-1);
my485Port->write(ClearYL,8); my485Port->write(ClearYL,8);
delay(90); delay(90);
for (int j = 0; j <8; ++j) { for (int j = 0; j <8; ++j) {
@ -132,4 +136,12 @@ uint8_t slave::claeryuliang(){
return 1; return 1;
} }
void slave::clearSerialBuffer() {
unsigned long startTime = millis();
while (my485Port->read() != -1) {
if (millis() - startTime > 1000) { // 超时保护 100ms
break;
}
}
}

View File

@ -23,6 +23,8 @@ class slave
int32_t getYuliangfuliang(); int32_t getYuliangfuliang();
uint8_t claeryuliang(); uint8_t claeryuliang();
bool isinit = false; bool isinit = false;
private:
void clearSerialBuffer();
}; };

View File

@ -1,7 +1,7 @@
#ifndef UPDATEBYME_H #ifndef UPDATEBYME_H
#define UPDATEBYME_H #define UPDATEBYME_H
#include<Update.h> #include<Update.h>
#include <HTTPClient.h> #include <HttpClient.h>
class UpDateClassByme{ class UpDateClassByme{
public: public:
UpdateClass updater; UpdateClass updater;