Compare commits
3 Commits
12c10403d5
...
2febf4f902
| Author | SHA1 | Date | |
|---|---|---|---|
| 2febf4f902 | |||
| a4fa347557 | |||
| 4f3535947f |
@ -3,11 +3,13 @@
|
|||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "freertos/event_groups.h"
|
#include "freertos/event_groups.h"
|
||||||
|
#include "freertos/timers.h"
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_http_server.h"
|
#include "esp_http_server.h"
|
||||||
|
#include "esp_task_wdt.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
#include "driver/ledc.h"
|
#include "driver/ledc.h"
|
||||||
@ -31,6 +33,14 @@
|
|||||||
#define PWM_R_CHANNEL LEDC_CHANNEL_0
|
#define PWM_R_CHANNEL LEDC_CHANNEL_0
|
||||||
#define PWM_L_CHANNEL LEDC_CHANNEL_1
|
#define PWM_L_CHANNEL LEDC_CHANNEL_1
|
||||||
|
|
||||||
|
// Motor ramping configuration
|
||||||
|
#define RAMP_STEP_MS 50 // Time between ramp steps (milliseconds)
|
||||||
|
#define RAMP_STEP_SIZE 5 // PWM duty change per step (0-255)
|
||||||
|
#define MIN_MOTOR_SPEED 10 // Minimum speed to overcome motor inertia
|
||||||
|
|
||||||
|
// Watchdog configuration
|
||||||
|
#define WATCHDOG_TIMEOUT_S 10 // Watchdog timeout in seconds
|
||||||
|
|
||||||
static const char* TAG = "HTTP_MOTOR";
|
static const char* TAG = "HTTP_MOTOR";
|
||||||
|
|
||||||
// WiFi event group
|
// WiFi event group
|
||||||
@ -47,122 +57,104 @@ typedef enum {
|
|||||||
MOTOR_INTAKE
|
MOTOR_INTAKE
|
||||||
} motor_mode_t;
|
} motor_mode_t;
|
||||||
|
|
||||||
static motor_mode_t current_mode = MOTOR_OFF;
|
typedef struct {
|
||||||
static int current_speed = 0;
|
motor_mode_t mode;
|
||||||
|
int target_speed;
|
||||||
|
int current_speed;
|
||||||
|
bool ramping;
|
||||||
|
TimerHandle_t ramp_timer;
|
||||||
|
} motor_state_t;
|
||||||
|
|
||||||
|
static motor_state_t motor_state = {
|
||||||
|
.mode = MOTOR_OFF,
|
||||||
|
.target_speed = 0,
|
||||||
|
.current_speed = 0,
|
||||||
|
.ramping = false,
|
||||||
|
.ramp_timer = NULL
|
||||||
|
};
|
||||||
|
|
||||||
// HTTP server handle
|
// HTTP server handle
|
||||||
static httpd_handle_t server = NULL;
|
static httpd_handle_t server = NULL;
|
||||||
|
|
||||||
// HTML web page for control
|
// Task handles for watchdog
|
||||||
static const char* html_page =
|
static TaskHandle_t main_task_handle = NULL;
|
||||||
"<!DOCTYPE html>"
|
|
||||||
"<html>"
|
|
||||||
"<head>"
|
|
||||||
" <title>Maxxfan Controller</title>"
|
|
||||||
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
|
|
||||||
" <style>"
|
|
||||||
" body { font-family: Arial, sans-serif; margin: 40px; background: #f0f0f0; }"
|
|
||||||
" .container { max-width: 500px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }"
|
|
||||||
" h1 { color: #333; text-align: center; }"
|
|
||||||
" .control-group { margin: 20px 0; padding: 20px; border: 1px solid #ddd; border-radius: 5px; }"
|
|
||||||
" .control-group h3 { margin-top: 0; color: #555; }"
|
|
||||||
" button { padding: 15px 25px; margin: 5px; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; }"
|
|
||||||
" .btn-off { background: #f44336; color: white; }"
|
|
||||||
" .btn-exhaust { background: #ff9800; color: white; }"
|
|
||||||
" .btn-intake { background: #4CAF50; color: white; }"
|
|
||||||
" .btn-off:hover { background: #d32f2f; }"
|
|
||||||
" .btn-exhaust:hover { background: #f57c00; }"
|
|
||||||
" .btn-intake:hover { background: #388e3c; }"
|
|
||||||
" .speed-control { margin: 20px 0; }"
|
|
||||||
" .speed-slider { width: 100%; height: 40px; }"
|
|
||||||
" .status { background: #e3f2fd; padding: 15px; border-radius: 5px; margin: 20px 0; }"
|
|
||||||
" .status h4 { margin: 0 0 10px 0; color: #1976d2; }"
|
|
||||||
" </style>"
|
|
||||||
"</head>"
|
|
||||||
"<body>"
|
|
||||||
" <div class=\"container\">"
|
|
||||||
" <h1>Maxxfan Controller</h1>"
|
|
||||||
" "
|
|
||||||
" <div class=\"status\">"
|
|
||||||
" <h4>Current Status</h4>"
|
|
||||||
" <p><strong>Mode:</strong> <span id=\"mode\">OFF</span></p>"
|
|
||||||
" <p><strong>Speed:</strong> <span id=\"speed\">0</span>%</p>"
|
|
||||||
" </div>"
|
|
||||||
" "
|
|
||||||
" <div class=\"control-group\">"
|
|
||||||
" <h3>Fan Control</h3>"
|
|
||||||
" <button class=\"btn-off\" onclick=\"setFan('off', 0)\">Turn OFF</button>"
|
|
||||||
" <button class=\"btn-exhaust\" onclick=\"setFan('exhaust', 50)\">Exhaust (50%)</button>"
|
|
||||||
" <button class=\"btn-intake\" onclick=\"setFan('intake', 50)\">Intake (50%)</button>"
|
|
||||||
" </div>"
|
|
||||||
" "
|
|
||||||
" <div class=\"control-group\">"
|
|
||||||
" <h3>Speed Control</h3>"
|
|
||||||
" <div class=\"speed-control\">"
|
|
||||||
" <label for=\"speedSlider\">Speed: <span id=\"speedValue\">50</span>%</label><br>"
|
|
||||||
" <input type=\"range\" id=\"speedSlider\" class=\"speed-slider\" min=\"0\" max=\"100\" value=\"50\" oninput=\"updateSpeed(this.value)\">"
|
|
||||||
" </div>"
|
|
||||||
" <button class=\"btn-exhaust\" onclick=\"setFanSpeed('exhaust')\">Set Exhaust Speed</button>"
|
|
||||||
" <button class=\"btn-intake\" onclick=\"setFanSpeed('intake')\">Set Intake Speed</button>"
|
|
||||||
" </div>"
|
|
||||||
" </div>"
|
|
||||||
""
|
|
||||||
" <script>"
|
|
||||||
" let currentSpeed = 50;"
|
|
||||||
" "
|
|
||||||
" function updateSpeed(value) {"
|
|
||||||
" currentSpeed = parseInt(value);"
|
|
||||||
" document.getElementById('speedValue').textContent = value;"
|
|
||||||
" }"
|
|
||||||
" "
|
|
||||||
" function setFan(mode, speed) {"
|
|
||||||
" currentSpeed = speed;"
|
|
||||||
" document.getElementById('speedSlider').value = speed;"
|
|
||||||
" document.getElementById('speedValue').textContent = speed;"
|
|
||||||
" "
|
|
||||||
" fetch('/fan', {"
|
|
||||||
" method: 'POST',"
|
|
||||||
" headers: { 'Content-Type': 'application/json' },"
|
|
||||||
" body: JSON.stringify({ mode: mode, speed: parseInt(speed) })"
|
|
||||||
" })"
|
|
||||||
" .then(response => response.json())"
|
|
||||||
" .then(data => updateStatus(data))"
|
|
||||||
" .catch(error => console.error('Error:', error));"
|
|
||||||
" }"
|
|
||||||
" "
|
|
||||||
" function setFanSpeed(mode) {"
|
|
||||||
" fetch('/fan', {"
|
|
||||||
" method: 'POST',"
|
|
||||||
" headers: { 'Content-Type': 'application/json' },"
|
|
||||||
" body: JSON.stringify({ mode: mode, speed: parseInt(currentSpeed) })"
|
|
||||||
" })"
|
|
||||||
" .then(response => response.json())"
|
|
||||||
" .then(data => updateStatus(data))"
|
|
||||||
" .catch(error => console.error('Error:', error));"
|
|
||||||
" }"
|
|
||||||
" "
|
|
||||||
" function updateStatus(data) {"
|
|
||||||
" document.getElementById('mode').textContent = data.mode.toUpperCase();"
|
|
||||||
" document.getElementById('speed').textContent = data.speed;"
|
|
||||||
" }"
|
|
||||||
" "
|
|
||||||
" function getStatus() {"
|
|
||||||
" fetch('/status')"
|
|
||||||
" .then(response => response.json())"
|
|
||||||
" .then(data => updateStatus(data))"
|
|
||||||
" .catch(error => console.error('Error:', error));"
|
|
||||||
" }"
|
|
||||||
" "
|
|
||||||
" // Update status every 2 seconds"
|
|
||||||
" setInterval(getStatus, 2000);"
|
|
||||||
" "
|
|
||||||
" // Get initial status"
|
|
||||||
" getStatus();"
|
|
||||||
" </script>"
|
|
||||||
"</body>"
|
|
||||||
"</html>";
|
|
||||||
|
|
||||||
// WiFi event handler (same as before)
|
// Compact HTML web page for control
|
||||||
|
static const char* html_page =
|
||||||
|
"<!DOCTYPE html><html><head><title>Maxxfan</title><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><style>"
|
||||||
|
"body{font-family:Arial;margin:20px;background:#f0f0f0}.container{max-width:500px;margin:0 auto;background:white;padding:20px;border-radius:8px}"
|
||||||
|
"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}"
|
||||||
|
".status{background:#e3f2fd;padding:15px;border-radius:4px;margin:15px 0}.ramping{background:#fff3e0;padding:8px;margin:8px 0;display:none}"
|
||||||
|
".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>"
|
||||||
|
"<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>"
|
||||||
|
"<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>"
|
||||||
|
"<button class=\"exhaust\" onclick=\"setFan('exhaust',50)\">Exhaust 50%</button>"
|
||||||
|
"<button class=\"intake\" onclick=\"setFan('intake',50)\">Intake 50%</button></div>"
|
||||||
|
"<div><h3>Speed Control</h3><label>Speed: <span id=\"speedValue\">50</span>%</label>"
|
||||||
|
"<input type=\"range\" id=\"speedSlider\" class=\"slider\" min=\"0\" max=\"100\" value=\"50\" oninput=\"updateSpeed(this.value)\">"
|
||||||
|
"<button class=\"exhaust\" onclick=\"setFanSpeed('exhaust')\">Set Exhaust</button>"
|
||||||
|
"<button class=\"intake\" onclick=\"setFanSpeed('intake')\">Set Intake</button></div></div>"
|
||||||
|
"<script>let currentSpeed=50,updateInterval=null,errorCount=0;"
|
||||||
|
"function updateSpeed(v){currentSpeed=parseInt(v);document.getElementById('speedValue').textContent=v}"
|
||||||
|
"function showError(m){document.getElementById('errorStatus').innerHTML='Error: '+m;document.getElementById('errorStatus').style.display='block';"
|
||||||
|
"document.getElementById('connectionStatus').textContent='Error'}"
|
||||||
|
"function hideError(){document.getElementById('errorStatus').style.display='none';"
|
||||||
|
"document.getElementById('connectionStatus').textContent='Connected';errorCount=0}"
|
||||||
|
"function setFan(mode,speed){currentSpeed=speed;document.getElementById('speedSlider').value=speed;"
|
||||||
|
"document.getElementById('speedValue').textContent=speed;fetch('/fan',{method:'POST',"
|
||||||
|
"headers:{'Content-Type':'application/json'},body:JSON.stringify({mode:mode,speed:parseInt(speed)})})"
|
||||||
|
".then(r=>{if(!r.ok)throw new Error('HTTP '+r.status);return r.json()})"
|
||||||
|
".then(d=>{updateStatus(d);hideError()}).catch(e=>{console.error(e);showError(e.message)})}"
|
||||||
|
"function setFanSpeed(mode){fetch('/fan',{method:'POST',headers:{'Content-Type':'application/json'},"
|
||||||
|
"body:JSON.stringify({mode:mode,speed:parseInt(currentSpeed)})})"
|
||||||
|
".then(r=>{if(!r.ok)throw new Error('HTTP '+r.status);return r.json()})"
|
||||||
|
".then(d=>{updateStatus(d);hideError()}).catch(e=>{console.error(e);showError(e.message)})}"
|
||||||
|
"function updateStatus(data){document.getElementById('mode').textContent=data.mode.toUpperCase();"
|
||||||
|
"document.getElementById('speed').textContent=data.current_speed;"
|
||||||
|
"document.getElementById('target').textContent=data.target_speed;"
|
||||||
|
"document.getElementById('rampStatus').style.display=data.ramping?'block':'none'}"
|
||||||
|
"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')})}"
|
||||||
|
"function startUpdates(){if(updateInterval)clearInterval(updateInterval);updateInterval=setInterval(getStatus,1000)}"
|
||||||
|
"document.addEventListener('DOMContentLoaded',function(){getStatus();startUpdates()})</script></body></html>";
|
||||||
|
|
||||||
|
// Forward declarations
|
||||||
|
static void motor_ramp_timer_callback(TimerHandle_t xTimer);
|
||||||
|
static void apply_motor_pwm(int speed_percent);
|
||||||
|
|
||||||
|
// Initialize watchdog timer
|
||||||
|
void init_watchdog(void) {
|
||||||
|
ESP_LOGI(TAG, "Setting up watchdog monitoring...");
|
||||||
|
|
||||||
|
// Get current task handle and add to watchdog
|
||||||
|
main_task_handle = xTaskGetCurrentTaskHandle();
|
||||||
|
esp_err_t result = esp_task_wdt_add(main_task_handle);
|
||||||
|
|
||||||
|
if (result == ESP_OK) {
|
||||||
|
ESP_LOGI(TAG, "Main task added to watchdog monitoring");
|
||||||
|
} else if (result == ESP_ERR_INVALID_ARG) {
|
||||||
|
ESP_LOGI(TAG, "Task already monitored by watchdog");
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(TAG, "Watchdog not available: %s", esp_err_to_name(result));
|
||||||
|
main_task_handle = NULL; // Disable watchdog feeding
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Feed the watchdog
|
||||||
|
void feed_watchdog(void) {
|
||||||
|
if (main_task_handle != NULL) {
|
||||||
|
esp_err_t result = esp_task_wdt_reset();
|
||||||
|
if (result != ESP_OK) {
|
||||||
|
ESP_LOGD(TAG, "Watchdog reset failed: %s", esp_err_to_name(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WiFi event handler
|
||||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||||
int32_t event_id, void* event_data)
|
int32_t event_id, void* event_data)
|
||||||
{
|
{
|
||||||
@ -240,18 +232,14 @@ void configure_pwm(void)
|
|||||||
ESP_LOGI(TAG, "PWM configured");
|
ESP_LOGI(TAG, "PWM configured");
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_motor_speed(motor_mode_t mode, int speed_percent)
|
// Apply PWM to motor based on current mode and speed
|
||||||
{
|
static void apply_motor_pwm(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;
|
||||||
|
|
||||||
current_mode = mode;
|
|
||||||
current_speed = speed_percent;
|
|
||||||
|
|
||||||
uint32_t duty = (speed_percent * 255) / 100;
|
uint32_t duty = (speed_percent * 255) / 100;
|
||||||
|
|
||||||
if (mode == MOTOR_OFF || speed_percent == 0) {
|
if (motor_state.mode == MOTOR_OFF || speed_percent == 0) {
|
||||||
ESP_LOGI(TAG, "Motor OFF");
|
|
||||||
gpio_set_level(LED_PIN, 0);
|
gpio_set_level(LED_PIN, 0);
|
||||||
gpio_set_level(MOTOR_R_EN, 0);
|
gpio_set_level(MOTOR_R_EN, 0);
|
||||||
gpio_set_level(MOTOR_L_EN, 0);
|
gpio_set_level(MOTOR_L_EN, 0);
|
||||||
@ -260,23 +248,19 @@ void set_motor_speed(motor_mode_t mode, int speed_percent)
|
|||||||
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL);
|
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL);
|
||||||
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_L_CHANNEL);
|
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_L_CHANNEL);
|
||||||
|
|
||||||
} else if (mode == MOTOR_EXHAUST) {
|
} else if (motor_state.mode == MOTOR_EXHAUST) {
|
||||||
ESP_LOGI(TAG, "Motor EXHAUST - Speed: %d%%", speed_percent);
|
|
||||||
gpio_set_level(LED_PIN, 1);
|
gpio_set_level(LED_PIN, 1);
|
||||||
gpio_set_level(MOTOR_R_EN, 1);
|
gpio_set_level(MOTOR_R_EN, 1);
|
||||||
gpio_set_level(MOTOR_L_EN, 0);
|
gpio_set_level(MOTOR_L_EN, 0);
|
||||||
vTaskDelay(pdMS_TO_TICKS(10));
|
|
||||||
ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL, duty);
|
ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL, duty);
|
||||||
ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_L_CHANNEL, 0);
|
ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_L_CHANNEL, 0);
|
||||||
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL);
|
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL);
|
||||||
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_L_CHANNEL);
|
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_L_CHANNEL);
|
||||||
|
|
||||||
} else if (mode == MOTOR_INTAKE) {
|
} else if (motor_state.mode == MOTOR_INTAKE) {
|
||||||
ESP_LOGI(TAG, "Motor INTAKE - Speed: %d%%", speed_percent);
|
|
||||||
gpio_set_level(LED_PIN, 1);
|
gpio_set_level(LED_PIN, 1);
|
||||||
gpio_set_level(MOTOR_R_EN, 0);
|
gpio_set_level(MOTOR_R_EN, 0);
|
||||||
gpio_set_level(MOTOR_L_EN, 1);
|
gpio_set_level(MOTOR_L_EN, 1);
|
||||||
vTaskDelay(pdMS_TO_TICKS(10));
|
|
||||||
ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL, 0);
|
ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL, 0);
|
||||||
ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_L_CHANNEL, duty);
|
ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_L_CHANNEL, duty);
|
||||||
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL);
|
ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_R_CHANNEL);
|
||||||
@ -284,9 +268,129 @@ void set_motor_speed(motor_mode_t mode, int speed_percent)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Motor ramp timer callback
|
||||||
|
static void motor_ramp_timer_callback(TimerHandle_t xTimer) {
|
||||||
|
if (!motor_state.ramping) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int speed_diff = motor_state.target_speed - motor_state.current_speed;
|
||||||
|
|
||||||
|
if (abs(speed_diff) <= RAMP_STEP_SIZE) {
|
||||||
|
// Close enough to target, finish ramping
|
||||||
|
motor_state.current_speed = motor_state.target_speed;
|
||||||
|
motor_state.ramping = false;
|
||||||
|
|
||||||
|
// Stop the timer
|
||||||
|
xTimerStop(motor_state.ramp_timer, 0);
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Ramping complete - Final speed: %d%%", motor_state.current_speed);
|
||||||
|
} else {
|
||||||
|
// Continue ramping
|
||||||
|
if (speed_diff > 0) {
|
||||||
|
motor_state.current_speed += RAMP_STEP_SIZE;
|
||||||
|
} else {
|
||||||
|
motor_state.current_speed -= RAMP_STEP_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGD(TAG, "Ramping: %d%% -> %d%% (target: %d%%)",
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize motor ramping system
|
||||||
|
void init_motor_ramping(void) {
|
||||||
|
motor_state.ramp_timer = xTimerCreate(
|
||||||
|
"MotorRampTimer", // Timer name
|
||||||
|
pdMS_TO_TICKS(RAMP_STEP_MS), // Timer period
|
||||||
|
pdTRUE, // Auto-reload
|
||||||
|
(void*)0, // Timer ID
|
||||||
|
motor_ramp_timer_callback // Callback function
|
||||||
|
);
|
||||||
|
|
||||||
|
if (motor_state.ramp_timer == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Failed to create motor ramp timer");
|
||||||
|
} else {
|
||||||
|
ESP_LOGI(TAG, "Motor ramping system initialized");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_motor_speed(motor_mode_t mode, int speed_percent)
|
||||||
|
{
|
||||||
|
if (speed_percent < 0) speed_percent = 0;
|
||||||
|
if (speed_percent > 100) speed_percent = 100;
|
||||||
|
|
||||||
|
// Stop any current ramping
|
||||||
|
if (motor_state.ramping) {
|
||||||
|
xTimerStop(motor_state.ramp_timer, 0);
|
||||||
|
motor_state.ramping = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
motor_mode_t previous_mode = motor_state.mode;
|
||||||
|
motor_state.mode = mode;
|
||||||
|
motor_state.target_speed = speed_percent;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Motor command: %s - Target: %d%% (Current: %d%%)",
|
||||||
|
mode == MOTOR_OFF ? "OFF" : (mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE"),
|
||||||
|
speed_percent, motor_state.current_speed);
|
||||||
|
|
||||||
|
// Handle different scenarios
|
||||||
|
if (mode == MOTOR_OFF || speed_percent == 0) {
|
||||||
|
// Immediate stop
|
||||||
|
motor_state.current_speed = 0;
|
||||||
|
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) {
|
||||||
|
// Direction change - ramp down to minimum, change direction, then ramp up
|
||||||
|
motor_state.target_speed = MIN_MOTOR_SPEED;
|
||||||
|
motor_state.ramping = true;
|
||||||
|
xTimerStart(motor_state.ramp_timer, 0);
|
||||||
|
ESP_LOGI(TAG, "Direction change - ramping down first");
|
||||||
|
|
||||||
|
// Note: In a real implementation, you might want to implement a state machine
|
||||||
|
// to handle the direction change sequence properly
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Same mode, just speed change - ramp to new speed
|
||||||
|
motor_state.ramping = true;
|
||||||
|
xTimerStart(motor_state.ramp_timer, 0);
|
||||||
|
ESP_LOGI(TAG, "Speed change - ramping from %d%% to %d%%",
|
||||||
|
motor_state.current_speed, speed_percent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to set CORS headers
|
||||||
|
static void set_cors_headers(httpd_req_t *req) {
|
||||||
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||||
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||||
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type, Accept");
|
||||||
|
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
|
||||||
|
}
|
||||||
|
|
||||||
// HTTP handler for the main web page
|
// HTTP handler for the main web page
|
||||||
static esp_err_t root_get_handler(httpd_req_t *req)
|
static esp_err_t root_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
|
set_cors_headers(req);
|
||||||
httpd_resp_set_type(req, "text/html");
|
httpd_resp_set_type(req, "text/html");
|
||||||
httpd_resp_send(req, html_page, HTTPD_RESP_USE_STRLEN);
|
httpd_resp_send(req, html_page, HTTPD_RESP_USE_STRLEN);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@ -295,20 +399,32 @@ 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",
|
||||||
|
motor_state.mode, motor_state.current_speed, motor_state.target_speed,
|
||||||
|
motor_state.ramping ? "YES" : "NO");
|
||||||
|
|
||||||
|
set_cors_headers(req);
|
||||||
|
httpd_resp_set_type(req, "application/json");
|
||||||
|
|
||||||
cJSON *json = cJSON_CreateObject();
|
cJSON *json = cJSON_CreateObject();
|
||||||
|
|
||||||
const char* mode_str = "off";
|
const char* mode_str = "off";
|
||||||
if (current_mode == MOTOR_EXHAUST) mode_str = "exhaust";
|
if (motor_state.mode == MOTOR_EXHAUST) mode_str = "exhaust";
|
||||||
else if (current_mode == MOTOR_INTAKE) mode_str = "intake";
|
else if (motor_state.mode == MOTOR_INTAKE) mode_str = "intake";
|
||||||
|
|
||||||
cJSON_AddStringToObject(json, "mode", mode_str);
|
cJSON_AddStringToObject(json, "mode", mode_str);
|
||||||
cJSON_AddNumberToObject(json, "speed", current_speed);
|
cJSON_AddNumberToObject(json, "current_speed", motor_state.current_speed);
|
||||||
|
cJSON_AddNumberToObject(json, "target_speed", motor_state.target_speed);
|
||||||
|
cJSON_AddBoolToObject(json, "ramping", motor_state.ramping);
|
||||||
|
|
||||||
char *json_string = cJSON_Print(json);
|
char *json_string = cJSON_Print(json);
|
||||||
httpd_resp_set_type(req, "application/json");
|
if (json_string) {
|
||||||
httpd_resp_send(req, json_string, strlen(json_string));
|
httpd_resp_send(req, json_string, strlen(json_string));
|
||||||
|
free(json_string);
|
||||||
|
} else {
|
||||||
|
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "JSON creation failed");
|
||||||
|
}
|
||||||
|
|
||||||
free(json_string);
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@ -316,7 +432,7 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
|||||||
// HTTP handler for fan control (POST /fan)
|
// HTTP handler for fan control (POST /fan)
|
||||||
static esp_err_t fan_post_handler(httpd_req_t *req)
|
static esp_err_t fan_post_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
char buf[100];
|
char buf[200];
|
||||||
int ret, remaining = req->content_len;
|
int ret, remaining = req->content_len;
|
||||||
|
|
||||||
if (remaining >= sizeof(buf)) {
|
if (remaining >= sizeof(buf)) {
|
||||||
@ -333,8 +449,11 @@ static esp_err_t fan_post_handler(httpd_req_t *req)
|
|||||||
}
|
}
|
||||||
buf[ret] = '\0';
|
buf[ret] = '\0';
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Received POST data: %s", buf);
|
||||||
|
|
||||||
cJSON *json = cJSON_Parse(buf);
|
cJSON *json = cJSON_Parse(buf);
|
||||||
if (json == NULL) {
|
if (json == NULL) {
|
||||||
|
ESP_LOGE(TAG, "JSON parsing failed");
|
||||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
|
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
@ -375,20 +494,32 @@ static esp_err_t fan_post_handler(httpd_req_t *req)
|
|||||||
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
|
|
||||||
// Send response
|
// Send response with updated status
|
||||||
return status_get_handler(req);
|
return status_get_handler(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTP handler for OPTIONS requests (CORS preflight)
|
||||||
|
static esp_err_t options_handler(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
set_cors_headers(req);
|
||||||
|
httpd_resp_set_status(req, "200 OK");
|
||||||
|
httpd_resp_send(req, NULL, 0);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
// Start HTTP server
|
// Start HTTP server
|
||||||
static httpd_handle_t start_webserver(void)
|
static httpd_handle_t start_webserver(void)
|
||||||
{
|
{
|
||||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||||
config.max_uri_handlers = 10;
|
config.max_uri_handlers = 15;
|
||||||
|
config.recv_wait_timeout = 10;
|
||||||
|
config.send_wait_timeout = 10;
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
|
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
|
||||||
if (httpd_start(&server, &config) == ESP_OK) {
|
if (httpd_start(&server, &config) == ESP_OK) {
|
||||||
ESP_LOGI(TAG, "Registering URI handlers");
|
ESP_LOGI(TAG, "Registering URI handlers");
|
||||||
|
|
||||||
|
// Root handler
|
||||||
httpd_uri_t root = {
|
httpd_uri_t root = {
|
||||||
.uri = "/",
|
.uri = "/",
|
||||||
.method = HTTP_GET,
|
.method = HTTP_GET,
|
||||||
@ -397,6 +528,7 @@ static httpd_handle_t start_webserver(void)
|
|||||||
};
|
};
|
||||||
httpd_register_uri_handler(server, &root);
|
httpd_register_uri_handler(server, &root);
|
||||||
|
|
||||||
|
// Status handler
|
||||||
httpd_uri_t status = {
|
httpd_uri_t status = {
|
||||||
.uri = "/status",
|
.uri = "/status",
|
||||||
.method = HTTP_GET,
|
.method = HTTP_GET,
|
||||||
@ -405,6 +537,7 @@ static httpd_handle_t start_webserver(void)
|
|||||||
};
|
};
|
||||||
httpd_register_uri_handler(server, &status);
|
httpd_register_uri_handler(server, &status);
|
||||||
|
|
||||||
|
// Fan control handler
|
||||||
httpd_uri_t fan = {
|
httpd_uri_t fan = {
|
||||||
.uri = "/fan",
|
.uri = "/fan",
|
||||||
.method = HTTP_POST,
|
.method = HTTP_POST,
|
||||||
@ -413,6 +546,23 @@ static httpd_handle_t start_webserver(void)
|
|||||||
};
|
};
|
||||||
httpd_register_uri_handler(server, &fan);
|
httpd_register_uri_handler(server, &fan);
|
||||||
|
|
||||||
|
// OPTIONS handler for CORS preflight
|
||||||
|
httpd_uri_t options_status = {
|
||||||
|
.uri = "/status",
|
||||||
|
.method = HTTP_OPTIONS,
|
||||||
|
.handler = options_handler,
|
||||||
|
.user_ctx = NULL
|
||||||
|
};
|
||||||
|
httpd_register_uri_handler(server, &options_status);
|
||||||
|
|
||||||
|
httpd_uri_t options_fan = {
|
||||||
|
.uri = "/fan",
|
||||||
|
.method = HTTP_OPTIONS,
|
||||||
|
.handler = options_handler,
|
||||||
|
.user_ctx = NULL
|
||||||
|
};
|
||||||
|
httpd_register_uri_handler(server, &options_fan);
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -476,9 +626,19 @@ void wifi_init_sta(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Main application task with watchdog feeding
|
||||||
|
void main_task(void *pvParameters) {
|
||||||
|
ESP_LOGI(TAG, "Main task started");
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
feed_watchdog();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(5000)); // Feed watchdog every 5 seconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Starting Maxxfan HTTP Controller!");
|
ESP_LOGI(TAG, "Starting Maxxfan HTTP Controller with improvements!");
|
||||||
|
|
||||||
// Initialize NVS
|
// Initialize NVS
|
||||||
esp_err_t ret = nvs_flash_init();
|
esp_err_t ret = nvs_flash_init();
|
||||||
@ -488,17 +648,30 @@ void app_main(void)
|
|||||||
}
|
}
|
||||||
ESP_ERROR_CHECK(ret);
|
ESP_ERROR_CHECK(ret);
|
||||||
|
|
||||||
|
// Initialize watchdog timer
|
||||||
|
init_watchdog();
|
||||||
|
|
||||||
// Configure hardware
|
// Configure hardware
|
||||||
configure_gpio_pins();
|
configure_gpio_pins();
|
||||||
configure_pwm();
|
configure_pwm();
|
||||||
|
|
||||||
|
// Initialize motor ramping system
|
||||||
|
init_motor_ramping();
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Connecting to WiFi network: %s", WIFI_SSID);
|
ESP_LOGI(TAG, "Connecting to WiFi network: %s", WIFI_SSID);
|
||||||
wifi_init_sta();
|
wifi_init_sta();
|
||||||
|
|
||||||
// Start HTTP server
|
// Start HTTP server
|
||||||
start_webserver();
|
start_webserver();
|
||||||
|
|
||||||
ESP_LOGI(TAG, "=== Maxxfan Controller Ready! ===");
|
ESP_LOGI(TAG, "=== Enhanced Maxxfan Controller Ready! ===");
|
||||||
|
ESP_LOGI(TAG, "Features: Motor Ramping, Optimized Performance, Real-time Updates");
|
||||||
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");
|
||||||
|
|
||||||
|
// Main loop - reset watchdog periodically
|
||||||
|
while (1) {
|
||||||
|
feed_watchdog();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(3000)); // Feed every 3 seconds (system default is usually 5s timeout)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user