diff --git a/main/main.c b/main/main.c index 4c012b6..5763d46 100644 --- a/main/main.c +++ b/main/main.c @@ -71,6 +71,24 @@ static void mqtt_connected_callback(void) mqtt_publish_pump_state(1, motor_is_running(MOTOR_PUMP_1)); mqtt_publish_pump_state(2, motor_is_running(MOTOR_PUMP_2)); + // Subscribe to additional topics + static const char* additional_topics[] = { + "plant_watering/pump/+/speed", + "plant_watering/commands/test_pump/+", + "plant_watering/commands/emergency_stop", + "plant_watering/settings/+/+", + NULL + }; + + for (int i = 0; additional_topics[i] != NULL; i++) { + esp_err_t ret = mqtt_client_subscribe(additional_topics[i], MQTT_QOS_1); + if (ret == ESP_OK) { + ESP_LOGI(TAG, "Subscribed to: %s", additional_topics[i]); + } else { + ESP_LOGE(TAG, "Failed to subscribe to: %s", additional_topics[i]); + } + } + // Publish motor statistics motor_stats_t stats; for (int i = 1; i <= 2; i++) { @@ -154,6 +172,39 @@ static void mqtt_data_callback(const char* topic, const char* data, int data_len } else if (strcmp(topic, "plant_watering/commands/emergency_stop") == 0) { ESP_LOGW(TAG, "Emergency stop command received!"); motor_emergency_stop(); + } else if (strncmp(topic, "plant_watering/settings/pump/", 29) == 0) { + // Parse settings commands like: + // plant_watering/settings/pump/1/max_runtime + // plant_watering/settings/pump/1/min_interval + // plant_watering/settings/pump/1/min_speed + // plant_watering/settings/pump/1/max_speed + + int pump_id = 0; + char setting[32] = {0}; + + // Extract pump ID and setting name + if (sscanf(topic + 29, "%d/%31s", &pump_id, setting) == 2) { + if (pump_id >= 1 && pump_id <= 2) { + int value = atoi(data); + + if (strcmp(setting, "max_runtime") == 0 && value > 0) { + motor_set_max_runtime(pump_id, value); + ESP_LOGI(TAG, "Set pump %d max runtime to %d ms", pump_id, value); + } else if (strcmp(setting, "min_interval") == 0 && value > 0) { + motor_set_min_interval(pump_id, value); + ESP_LOGI(TAG, "Set pump %d min interval to %d ms", pump_id, value); + } else if (strcmp(setting, "min_speed") == 0 && value >= 0 && value <= 100) { + // Get current max speed to validate + motor_stats_t stats; + motor_get_stats(pump_id, &stats); + motor_set_speed_limits(pump_id, value, 100); // Assuming max stays at 100 + ESP_LOGI(TAG, "Set pump %d min speed to %d%%", pump_id, value); + } else if (strcmp(setting, "max_speed") == 0 && value > 0 && value <= 100) { + motor_set_speed_limits(pump_id, MOTOR_MIN_SPEED, value); + ESP_LOGI(TAG, "Set pump %d max speed to %d%%", pump_id, value); + } + } + } } } @@ -328,18 +379,6 @@ void app_main(void) motor_set_min_interval(MOTOR_PUMP_1, CONFIG_WATERING_MIN_INTERVAL_MS); motor_set_min_interval(MOTOR_PUMP_2, CONFIG_WATERING_MIN_INTERVAL_MS); - // Subscribe to additional MQTT topics after connection - static const char* additional_topics[] = { - "plant_watering/pump/+/speed", - "plant_watering/commands/test_pump/+", - "plant_watering/commands/emergency_stop", - "plant_watering/settings/+/+", - NULL - }; - - // This would need to be done after MQTT connection - // You might want to add this to the mqtt_connected_callback - // Start WiFi connection esp_err_t ret = wifi_manager_start(); if (ret != ESP_OK) { diff --git a/main/motor_control.c b/main/motor_control.c index 60fcef7..1c88286 100644 --- a/main/motor_control.c +++ b/main/motor_control.c @@ -646,9 +646,16 @@ static void motor_soft_start_timer_callback(TimerHandle_t xTimer) return; } + // Calculate increment based on soft start time + // We want to go from 0 to target in MOTOR_SOFT_START_TIME_MS + // Timer runs every 50ms, so number of steps = MOTOR_SOFT_START_TIME_MS / 50 + int steps = MOTOR_SOFT_START_TIME_MS / 50; + int increment = motor->target_speed / steps; + if (increment < 1) increment = 1; + // Ramp up speed if (motor->speed_percent < motor->target_speed) { - motor->speed_percent += 5; // 5% increment + motor->speed_percent += increment; if (motor->speed_percent > motor->target_speed) { motor->speed_percent = motor->target_speed; } @@ -656,9 +663,13 @@ static void motor_soft_start_timer_callback(TimerHandle_t xTimer) uint8_t duty = (motor->speed_percent * MOTOR_PWM_MAX_DUTY) / 100; motor_update_pwm(id, duty); + ESP_LOGD(TAG, "Soft start motor %d: %d%% (target: %d%%)", + id, motor->speed_percent, motor->target_speed); + // Stop timer when target reached if (motor->speed_percent >= motor->target_speed) { xTimerStop(xTimer, 0); + ESP_LOGD(TAG, "Soft start complete for motor %d", id); } } } diff --git a/main/motor_control.h b/main/motor_control.h index 31b8b7a..f4dbdff 100644 --- a/main/motor_control.h +++ b/main/motor_control.h @@ -23,7 +23,7 @@ #define MOTOR_MIN_SPEED 20 // Minimum pump speed (%) #define MOTOR_MAX_RUNTIME_MS 30000 // Maximum runtime (30 seconds) #define MOTOR_MIN_INTERVAL_MS 300000 // Minimum interval between runs (5 minutes) -#define MOTOR_SOFT_START_TIME_MS 500 // Soft start ramp time +#define MOTOR_SOFT_START_TIME_MS 1000 // Soft start ramp time // Motor IDs typedef enum {