10 sec cooldown when switch direction
This commit is contained in:
@ -37,6 +37,7 @@
|
|||||||
#define RAMP_STEP_MS 150 // Time between ramp steps (milliseconds)
|
#define RAMP_STEP_MS 150 // Time between ramp steps (milliseconds)
|
||||||
#define RAMP_STEP_SIZE 5 // PWM duty change per step (0-255)
|
#define RAMP_STEP_SIZE 5 // PWM duty change per step (0-255)
|
||||||
#define MIN_MOTOR_SPEED 10 // Minimum speed to overcome motor inertia
|
#define MIN_MOTOR_SPEED 10 // Minimum speed to overcome motor inertia
|
||||||
|
#define DIRECTION_CHANGE_COOLDOWN_MS 10000 // 10 seconds cooldown for direction changes
|
||||||
|
|
||||||
// Watchdog configuration
|
// Watchdog configuration
|
||||||
#define WATCHDOG_TIMEOUT_S 10 // Watchdog timeout in seconds
|
#define WATCHDOG_TIMEOUT_S 10 // Watchdog timeout in seconds
|
||||||
@ -57,20 +58,38 @@ typedef enum {
|
|||||||
MOTOR_INTAKE
|
MOTOR_INTAKE
|
||||||
} motor_mode_t;
|
} motor_mode_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
MOTOR_STATE_IDLE, // Motor is off or running normally
|
||||||
|
MOTOR_STATE_RAMPING, // Motor is ramping up/down
|
||||||
|
MOTOR_STATE_STOPPING, // Motor is stopping for direction change
|
||||||
|
MOTOR_STATE_COOLDOWN, // Motor is in cooldown period
|
||||||
|
MOTOR_STATE_RESTARTING // Motor is restarting after cooldown
|
||||||
|
} motor_state_enum_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
motor_mode_t mode;
|
motor_mode_t mode;
|
||||||
|
motor_mode_t pending_mode; // Mode to switch to after cooldown
|
||||||
int target_speed;
|
int target_speed;
|
||||||
|
int pending_speed; // Speed to set after cooldown
|
||||||
int current_speed;
|
int current_speed;
|
||||||
|
motor_state_enum_t state;
|
||||||
bool ramping;
|
bool ramping;
|
||||||
TimerHandle_t ramp_timer;
|
TimerHandle_t ramp_timer;
|
||||||
|
TimerHandle_t cooldown_timer;
|
||||||
|
uint32_t cooldown_remaining_ms; // For status reporting
|
||||||
} motor_state_t;
|
} motor_state_t;
|
||||||
|
|
||||||
static motor_state_t motor_state = {
|
static motor_state_t motor_state = {
|
||||||
.mode = MOTOR_OFF,
|
.mode = MOTOR_OFF,
|
||||||
|
.pending_mode = MOTOR_OFF,
|
||||||
.target_speed = 0,
|
.target_speed = 0,
|
||||||
|
.pending_speed = 0,
|
||||||
.current_speed = 0,
|
.current_speed = 0,
|
||||||
|
.state = MOTOR_STATE_IDLE,
|
||||||
.ramping = false,
|
.ramping = false,
|
||||||
.ramp_timer = NULL
|
.ramp_timer = NULL,
|
||||||
|
.cooldown_timer = NULL,
|
||||||
|
.cooldown_remaining_ms = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
// HTTP server handle
|
// HTTP server handle
|
||||||
@ -86,10 +105,13 @@ static const char* html_page =
|
|||||||
"h1{color:#333;text-align:center;margin:0 0 20px}button{padding:12px 20px;margin:5px;border:none;border-radius:4px;cursor:pointer;font-size:14px}"
|
"h1{color:#333;text-align:center;margin:0 0 20px}button{padding:12px 20px;margin:5px;border:none;border-radius:4px;cursor:pointer;font-size:14px}"
|
||||||
".off{background:#f44336;color:white}.exhaust{background:#ff9800;color:white}.intake{background:#4CAF50;color:white}"
|
".off{background:#f44336;color:white}.exhaust{background:#ff9800;color:white}.intake{background:#4CAF50;color:white}"
|
||||||
".status{background:#e3f2fd;padding:15px;border-radius:4px;margin:15px 0}.ramping{background:#fff3e0;padding:8px;margin:8px 0;display:none}"
|
".status{background:#e3f2fd;padding:15px;border-radius:4px;margin:15px 0}.ramping{background:#fff3e0;padding:8px;margin:8px 0;display:none}"
|
||||||
|
".cooldown{background:#ffebee;padding:8px;margin:8px 0;color:#c62828;display:none}"
|
||||||
".error{background:#ffebee;padding:8px;margin:8px 0;color:#c62828;display:none}.slider{width:100%;height:30px;margin:10px 0}"
|
".error{background:#ffebee;padding:8px;margin:8px 0;color:#c62828;display:none}.slider{width:100%;height:30px;margin:10px 0}"
|
||||||
"</style></head><body><div class=\"container\"><h1>Maxxfan Controller</h1>"
|
"</style></head><body><div class=\"container\"><h1>Maxxfan Controller</h1>"
|
||||||
"<div class=\"status\"><h4>Status</h4><p>Mode: <span id=\"mode\">OFF</span></p><p>Speed: <span id=\"speed\">0</span>%</p>"
|
"<div class=\"status\"><h4>Status</h4><p>Mode: <span id=\"mode\">OFF</span></p><p>Speed: <span id=\"speed\">0</span>%</p>"
|
||||||
"<p>Target: <span id=\"target\">0</span>%</p><div id=\"rampStatus\" class=\"ramping\">Ramping...</div>"
|
"<p>Target: <span id=\"target\">0</span>%</p><p>State: <span id=\"state\">IDLE</span></p>"
|
||||||
|
"<div id=\"rampStatus\" class=\"ramping\">Ramping...</div>"
|
||||||
|
"<div id=\"cooldownStatus\" class=\"cooldown\">Direction change cooldown: <span id=\"cooldownTime\">0</span>s</div>"
|
||||||
"<div id=\"errorStatus\" class=\"error\">Error</div><small id=\"connectionStatus\">Connecting...</small></div>"
|
"<div id=\"errorStatus\" class=\"error\">Error</div><small id=\"connectionStatus\">Connecting...</small></div>"
|
||||||
"<div><h3>Fan Control</h3><button class=\"off\" onclick=\"setFan('off',0)\">OFF</button>"
|
"<div><h3>Fan Control</h3><button class=\"off\" onclick=\"setFan('off',0)\">OFF</button>"
|
||||||
"<button class=\"exhaust\" onclick=\"setFan('exhaust',50)\">Exhaust 50%</button>"
|
"<button class=\"exhaust\" onclick=\"setFan('exhaust',50)\">Exhaust 50%</button>"
|
||||||
@ -116,7 +138,11 @@ static const char* html_page =
|
|||||||
"function updateStatus(data){document.getElementById('mode').textContent=data.mode.toUpperCase();"
|
"function updateStatus(data){document.getElementById('mode').textContent=data.mode.toUpperCase();"
|
||||||
"document.getElementById('speed').textContent=data.current_speed;"
|
"document.getElementById('speed').textContent=data.current_speed;"
|
||||||
"document.getElementById('target').textContent=data.target_speed;"
|
"document.getElementById('target').textContent=data.target_speed;"
|
||||||
"document.getElementById('rampStatus').style.display=data.ramping?'block':'none'}"
|
"document.getElementById('state').textContent=data.state.toUpperCase();"
|
||||||
|
"document.getElementById('rampStatus').style.display=data.ramping?'block':'none';"
|
||||||
|
"let cooldownDiv=document.getElementById('cooldownStatus');"
|
||||||
|
"if(data.cooldown_remaining>0){cooldownDiv.style.display='block';"
|
||||||
|
"document.getElementById('cooldownTime').textContent=Math.ceil(data.cooldown_remaining/1000);}else{cooldownDiv.style.display='none';}}"
|
||||||
"function getStatus(){fetch('/status').then(r=>{if(!r.ok)throw new Error('HTTP '+r.status);return r.json()})"
|
"function getStatus(){fetch('/status').then(r=>{if(!r.ok)throw new Error('HTTP '+r.status);return r.json()})"
|
||||||
".then(d=>{updateStatus(d);hideError()}).catch(e=>{errorCount++;if(errorCount>=3)showError('Connection lost')})}"
|
".then(d=>{updateStatus(d);hideError()}).catch(e=>{errorCount++;if(errorCount>=3)showError('Connection lost')})}"
|
||||||
"function startUpdates(){if(updateInterval)clearInterval(updateInterval);updateInterval=setInterval(getStatus,1000)}"
|
"function startUpdates(){if(updateInterval)clearInterval(updateInterval);updateInterval=setInterval(getStatus,1000)}"
|
||||||
@ -124,7 +150,9 @@ static const char* html_page =
|
|||||||
|
|
||||||
// Forward declarations
|
// Forward declarations
|
||||||
static void motor_ramp_timer_callback(TimerHandle_t xTimer);
|
static void motor_ramp_timer_callback(TimerHandle_t xTimer);
|
||||||
|
static void motor_cooldown_timer_callback(TimerHandle_t xTimer);
|
||||||
static void apply_motor_pwm(int speed_percent);
|
static void apply_motor_pwm(int speed_percent);
|
||||||
|
static void start_motor_operation(motor_mode_t mode, int speed_percent);
|
||||||
|
|
||||||
// Initialize watchdog timer
|
// Initialize watchdog timer
|
||||||
void init_watchdog(void) {
|
void init_watchdog(void) {
|
||||||
@ -270,7 +298,7 @@ static void apply_motor_pwm(int speed_percent) {
|
|||||||
|
|
||||||
// Motor ramp timer callback
|
// Motor ramp timer callback
|
||||||
static void motor_ramp_timer_callback(TimerHandle_t xTimer) {
|
static void motor_ramp_timer_callback(TimerHandle_t xTimer) {
|
||||||
if (!motor_state.ramping) {
|
if (motor_state.state != MOTOR_STATE_RAMPING) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,6 +308,7 @@ static void motor_ramp_timer_callback(TimerHandle_t xTimer) {
|
|||||||
// Close enough to target, finish ramping
|
// Close enough to target, finish ramping
|
||||||
motor_state.current_speed = motor_state.target_speed;
|
motor_state.current_speed = motor_state.target_speed;
|
||||||
motor_state.ramping = false;
|
motor_state.ramping = false;
|
||||||
|
motor_state.state = MOTOR_STATE_IDLE;
|
||||||
|
|
||||||
// Stop the timer
|
// Stop the timer
|
||||||
xTimerStop(motor_state.ramp_timer, 0);
|
xTimerStop(motor_state.ramp_timer, 0);
|
||||||
@ -293,14 +322,70 @@ static void motor_ramp_timer_callback(TimerHandle_t xTimer) {
|
|||||||
motor_state.current_speed -= RAMP_STEP_SIZE;
|
motor_state.current_speed -= RAMP_STEP_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Ramping: %d%% -> %d%% (target: %d%%)",
|
ESP_LOGD(TAG, "Ramping: %d%% (target: %d%%)", motor_state.current_speed, motor_state.target_speed);
|
||||||
motor_state.current_speed - (speed_diff > 0 ? RAMP_STEP_SIZE : -RAMP_STEP_SIZE),
|
|
||||||
motor_state.current_speed, motor_state.target_speed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
apply_motor_pwm(motor_state.current_speed);
|
apply_motor_pwm(motor_state.current_speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Motor cooldown timer callback
|
||||||
|
static void motor_cooldown_timer_callback(TimerHandle_t xTimer) {
|
||||||
|
ESP_LOGI(TAG, "Cooldown complete - Starting motor in %s mode at %d%%",
|
||||||
|
motor_state.pending_mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE",
|
||||||
|
motor_state.pending_speed);
|
||||||
|
|
||||||
|
// Reset cooldown tracking
|
||||||
|
motor_state.cooldown_remaining_ms = 0;
|
||||||
|
|
||||||
|
// Start the motor in the pending mode
|
||||||
|
start_motor_operation(motor_state.pending_mode, motor_state.pending_speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update cooldown remaining time (called periodically)
|
||||||
|
static void update_cooldown_time(void) {
|
||||||
|
if (motor_state.state == MOTOR_STATE_COOLDOWN && motor_state.cooldown_remaining_ms > 0) {
|
||||||
|
if (motor_state.cooldown_remaining_ms >= 1000) {
|
||||||
|
motor_state.cooldown_remaining_ms -= 1000;
|
||||||
|
} else {
|
||||||
|
motor_state.cooldown_remaining_ms = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start motor operation (internal function)
|
||||||
|
static void start_motor_operation(motor_mode_t mode, int speed_percent) {
|
||||||
|
motor_state.mode = mode;
|
||||||
|
motor_state.target_speed = speed_percent;
|
||||||
|
motor_state.state = MOTOR_STATE_RAMPING;
|
||||||
|
motor_state.ramping = true;
|
||||||
|
|
||||||
|
if (mode == MOTOR_OFF || speed_percent == 0) {
|
||||||
|
// Immediate stop
|
||||||
|
motor_state.current_speed = 0;
|
||||||
|
motor_state.target_speed = 0;
|
||||||
|
motor_state.state = MOTOR_STATE_IDLE;
|
||||||
|
motor_state.ramping = false;
|
||||||
|
apply_motor_pwm(0);
|
||||||
|
ESP_LOGI(TAG, "Motor stopped immediately");
|
||||||
|
} else {
|
||||||
|
// Start from minimum speed if currently off
|
||||||
|
if (motor_state.current_speed == 0) {
|
||||||
|
int start_speed = (speed_percent < MIN_MOTOR_SPEED) ? speed_percent : MIN_MOTOR_SPEED;
|
||||||
|
motor_state.current_speed = start_speed;
|
||||||
|
apply_motor_pwm(start_speed);
|
||||||
|
ESP_LOGI(TAG, "Motor starting at %d%%, ramping to %d%%", start_speed, speed_percent);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start ramping if needed
|
||||||
|
if (motor_state.current_speed != motor_state.target_speed) {
|
||||||
|
xTimerStart(motor_state.ramp_timer, 0);
|
||||||
|
} else {
|
||||||
|
motor_state.state = MOTOR_STATE_IDLE;
|
||||||
|
motor_state.ramping = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize motor ramping system
|
// Initialize motor ramping system
|
||||||
void init_motor_ramping(void) {
|
void init_motor_ramping(void) {
|
||||||
motor_state.ramp_timer = xTimerCreate(
|
motor_state.ramp_timer = xTimerCreate(
|
||||||
@ -311,10 +396,18 @@ void init_motor_ramping(void) {
|
|||||||
motor_ramp_timer_callback // Callback function
|
motor_ramp_timer_callback // Callback function
|
||||||
);
|
);
|
||||||
|
|
||||||
if (motor_state.ramp_timer == NULL) {
|
motor_state.cooldown_timer = xTimerCreate(
|
||||||
ESP_LOGE(TAG, "Failed to create motor ramp timer");
|
"MotorCooldownTimer", // Timer name
|
||||||
|
pdMS_TO_TICKS(DIRECTION_CHANGE_COOLDOWN_MS), // Timer period
|
||||||
|
pdFALSE, // One-shot
|
||||||
|
(void*)0, // Timer ID
|
||||||
|
motor_cooldown_timer_callback // Callback function
|
||||||
|
);
|
||||||
|
|
||||||
|
if (motor_state.ramp_timer == NULL || motor_state.cooldown_timer == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Failed to create motor timers");
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "Motor ramping system initialized");
|
ESP_LOGI(TAG, "Motor control system initialized with direction change safety");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,59 +416,72 @@ void set_motor_speed(motor_mode_t mode, int speed_percent)
|
|||||||
if (speed_percent < 0) speed_percent = 0;
|
if (speed_percent < 0) speed_percent = 0;
|
||||||
if (speed_percent > 100) speed_percent = 100;
|
if (speed_percent > 100) speed_percent = 100;
|
||||||
|
|
||||||
// Stop any current ramping
|
ESP_LOGI(TAG, "Motor command: %s - Speed: %d%% (Current mode: %s, Current speed: %d%%, State: %d)",
|
||||||
if (motor_state.ramping) {
|
mode == MOTOR_OFF ? "OFF" : (mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE"),
|
||||||
xTimerStop(motor_state.ramp_timer, 0);
|
speed_percent,
|
||||||
motor_state.ramping = false;
|
motor_state.mode == MOTOR_OFF ? "OFF" : (motor_state.mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE"),
|
||||||
|
motor_state.current_speed,
|
||||||
|
motor_state.state);
|
||||||
|
|
||||||
|
// If we're in cooldown, update the pending command
|
||||||
|
if (motor_state.state == MOTOR_STATE_COOLDOWN) {
|
||||||
|
motor_state.pending_mode = mode;
|
||||||
|
motor_state.pending_speed = speed_percent;
|
||||||
|
ESP_LOGI(TAG, "Motor in cooldown - command queued for execution");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
motor_mode_t previous_mode = motor_state.mode;
|
// Check if this is a direction change that requires cooldown
|
||||||
motor_state.mode = mode;
|
bool requires_cooldown = false;
|
||||||
motor_state.target_speed = speed_percent;
|
if (motor_state.current_speed > 0 && motor_state.mode != MOTOR_OFF) {
|
||||||
|
if ((motor_state.mode == MOTOR_EXHAUST && mode == MOTOR_INTAKE) ||
|
||||||
|
(motor_state.mode == MOTOR_INTAKE && mode == MOTOR_EXHAUST)) {
|
||||||
|
requires_cooldown = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Motor command: %s - Target: %d%% (Current: %d%%)",
|
if (requires_cooldown) {
|
||||||
mode == MOTOR_OFF ? "OFF" : (mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE"),
|
ESP_LOGI(TAG, "Direction change detected - initiating safety cooldown sequence");
|
||||||
speed_percent, motor_state.current_speed);
|
|
||||||
|
|
||||||
// Handle different scenarios
|
// Stop any current ramping
|
||||||
if (mode == MOTOR_OFF || speed_percent == 0) {
|
if (motor_state.ramping) {
|
||||||
// Immediate stop
|
xTimerStop(motor_state.ramp_timer, 0);
|
||||||
motor_state.current_speed = 0;
|
motor_state.ramping = false;
|
||||||
motor_state.target_speed = 0;
|
|
||||||
apply_motor_pwm(0);
|
|
||||||
ESP_LOGI(TAG, "Motor stopped immediately");
|
|
||||||
|
|
||||||
} else if (previous_mode == MOTOR_OFF || motor_state.current_speed == 0) {
|
|
||||||
// Starting from stop - apply minimum speed first, then ramp
|
|
||||||
int start_speed = (speed_percent < MIN_MOTOR_SPEED) ? speed_percent : MIN_MOTOR_SPEED;
|
|
||||||
motor_state.current_speed = start_speed;
|
|
||||||
apply_motor_pwm(start_speed);
|
|
||||||
|
|
||||||
if (speed_percent > start_speed) {
|
|
||||||
// Start ramping to target
|
|
||||||
motor_state.ramping = true;
|
|
||||||
xTimerStart(motor_state.ramp_timer, 0);
|
|
||||||
ESP_LOGI(TAG, "Motor starting at %d%%, ramping to %d%%", start_speed, speed_percent);
|
|
||||||
} else {
|
|
||||||
ESP_LOGI(TAG, "Motor started at %d%% (no ramping needed)", start_speed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (previous_mode != mode) {
|
// Stop the motor immediately
|
||||||
// Direction change - ramp down to minimum, change direction, then ramp up
|
motor_state.mode = MOTOR_OFF;
|
||||||
motor_state.target_speed = MIN_MOTOR_SPEED;
|
motor_state.current_speed = 0;
|
||||||
motor_state.ramping = true;
|
motor_state.target_speed = 0;
|
||||||
xTimerStart(motor_state.ramp_timer, 0);
|
motor_state.state = MOTOR_STATE_COOLDOWN;
|
||||||
ESP_LOGI(TAG, "Direction change - ramping down first");
|
motor_state.cooldown_remaining_ms = DIRECTION_CHANGE_COOLDOWN_MS;
|
||||||
|
apply_motor_pwm(0);
|
||||||
|
|
||||||
// Note: In a real implementation, you might want to implement a state machine
|
// Store the pending command
|
||||||
// to handle the direction change sequence properly
|
motor_state.pending_mode = mode;
|
||||||
|
motor_state.pending_speed = speed_percent;
|
||||||
|
|
||||||
|
// Start cooldown timer
|
||||||
|
xTimerStart(motor_state.cooldown_timer, 0);
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Motor stopped for direction change - %d second cooldown started",
|
||||||
|
DIRECTION_CHANGE_COOLDOWN_MS / 1000);
|
||||||
} else {
|
} else {
|
||||||
// Same mode, just speed change - ramp to new speed
|
// No direction change required, proceed normally
|
||||||
motor_state.ramping = true;
|
|
||||||
xTimerStart(motor_state.ramp_timer, 0);
|
// Stop any current ramping
|
||||||
ESP_LOGI(TAG, "Speed change - ramping from %d%% to %d%%",
|
if (motor_state.ramping) {
|
||||||
motor_state.current_speed, speed_percent);
|
xTimerStop(motor_state.ramp_timer, 0);
|
||||||
|
motor_state.ramping = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop cooldown timer if running
|
||||||
|
if (motor_state.state == MOTOR_STATE_COOLDOWN) {
|
||||||
|
xTimerStop(motor_state.cooldown_timer, 0);
|
||||||
|
motor_state.cooldown_remaining_ms = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
start_motor_operation(mode, speed_percent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -399,9 +505,12 @@ static esp_err_t root_get_handler(httpd_req_t *req)
|
|||||||
// HTTP handler for fan status (GET /status)
|
// HTTP handler for fan status (GET /status)
|
||||||
static esp_err_t status_get_handler(httpd_req_t *req)
|
static esp_err_t status_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Status request - Mode: %d, Current: %d%%, Target: %d%%, Ramping: %s",
|
// Update cooldown time before reporting
|
||||||
|
update_cooldown_time();
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Status request - Mode: %d, Current: %d%%, Target: %d%%, State: %d, Ramping: %s",
|
||||||
motor_state.mode, motor_state.current_speed, motor_state.target_speed,
|
motor_state.mode, motor_state.current_speed, motor_state.target_speed,
|
||||||
motor_state.ramping ? "YES" : "NO");
|
motor_state.state, motor_state.ramping ? "YES" : "NO");
|
||||||
|
|
||||||
set_cors_headers(req);
|
set_cors_headers(req);
|
||||||
httpd_resp_set_type(req, "application/json");
|
httpd_resp_set_type(req, "application/json");
|
||||||
@ -412,10 +521,31 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
|||||||
if (motor_state.mode == MOTOR_EXHAUST) mode_str = "exhaust";
|
if (motor_state.mode == MOTOR_EXHAUST) mode_str = "exhaust";
|
||||||
else if (motor_state.mode == MOTOR_INTAKE) mode_str = "intake";
|
else if (motor_state.mode == MOTOR_INTAKE) mode_str = "intake";
|
||||||
|
|
||||||
|
const char* state_str = "idle";
|
||||||
|
switch (motor_state.state) {
|
||||||
|
case MOTOR_STATE_RAMPING: state_str = "ramping"; break;
|
||||||
|
case MOTOR_STATE_STOPPING: state_str = "stopping"; break;
|
||||||
|
case MOTOR_STATE_COOLDOWN: state_str = "cooldown"; break;
|
||||||
|
case MOTOR_STATE_RESTARTING: state_str = "restarting"; break;
|
||||||
|
default: state_str = "idle"; break;
|
||||||
|
}
|
||||||
|
|
||||||
cJSON_AddStringToObject(json, "mode", mode_str);
|
cJSON_AddStringToObject(json, "mode", mode_str);
|
||||||
cJSON_AddNumberToObject(json, "current_speed", motor_state.current_speed);
|
cJSON_AddNumberToObject(json, "current_speed", motor_state.current_speed);
|
||||||
cJSON_AddNumberToObject(json, "target_speed", motor_state.target_speed);
|
cJSON_AddNumberToObject(json, "target_speed", motor_state.target_speed);
|
||||||
|
cJSON_AddStringToObject(json, "state", state_str);
|
||||||
cJSON_AddBoolToObject(json, "ramping", motor_state.ramping);
|
cJSON_AddBoolToObject(json, "ramping", motor_state.ramping);
|
||||||
|
cJSON_AddNumberToObject(json, "cooldown_remaining", motor_state.cooldown_remaining_ms);
|
||||||
|
|
||||||
|
// Add pending command info if in cooldown
|
||||||
|
if (motor_state.state == MOTOR_STATE_COOLDOWN) {
|
||||||
|
const char* pending_mode_str = "off";
|
||||||
|
if (motor_state.pending_mode == MOTOR_EXHAUST) pending_mode_str = "exhaust";
|
||||||
|
else if (motor_state.pending_mode == MOTOR_INTAKE) pending_mode_str = "intake";
|
||||||
|
|
||||||
|
cJSON_AddStringToObject(json, "pending_mode", pending_mode_str);
|
||||||
|
cJSON_AddNumberToObject(json, "pending_speed", motor_state.pending_speed);
|
||||||
|
}
|
||||||
|
|
||||||
char *json_string = cJSON_Print(json);
|
char *json_string = cJSON_Print(json);
|
||||||
if (json_string) {
|
if (json_string) {
|
||||||
@ -638,7 +768,7 @@ void main_task(void *pvParameters) {
|
|||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Starting Maxxfan HTTP Controller with improvements!");
|
ESP_LOGI(TAG, "Starting Maxxfan HTTP Controller with Direction Change Safety!");
|
||||||
|
|
||||||
// Initialize NVS
|
// Initialize NVS
|
||||||
esp_err_t ret = nvs_flash_init();
|
esp_err_t ret = nvs_flash_init();
|
||||||
@ -665,7 +795,8 @@ void app_main(void)
|
|||||||
start_webserver();
|
start_webserver();
|
||||||
|
|
||||||
ESP_LOGI(TAG, "=== Enhanced Maxxfan Controller Ready! ===");
|
ESP_LOGI(TAG, "=== Enhanced Maxxfan Controller Ready! ===");
|
||||||
ESP_LOGI(TAG, "Features: Motor Ramping, Optimized Performance, Real-time Updates");
|
ESP_LOGI(TAG, "Features: Motor Ramping, Direction Change Safety, Real-time Updates");
|
||||||
|
ESP_LOGI(TAG, "Safety: 10-second cooldown for direction changes");
|
||||||
ESP_LOGI(TAG, "Open your browser and go to: http://[ESP32_IP_ADDRESS]");
|
ESP_LOGI(TAG, "Open your browser and go to: http://[ESP32_IP_ADDRESS]");
|
||||||
ESP_LOGI(TAG, "Check the monitor output above for your IP address");
|
ESP_LOGI(TAG, "Check the monitor output above for your IP address");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user