This commit is contained in:
thek4n 2026-06-05 16:11:08 +03:00
parent 0d2ae768d2
commit bc179e574f

View File

@ -24,6 +24,18 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#include "frontend.h" #include "frontend.h"
#if !defined(CONFIG_PUMP_PIN)
#error "CONFIG_PUMP_PIN must be defined in menuconfig"
#endif
#if !defined(CONFIG_AP_WIFI_SSID)
#error "CONFIG_AP_WIFI_SSID must be defined in menuconfig"
#endif
#if !defined(CONFIG_AP_IP) || !defined(CONFIG_AP_GATEWAY) || !defined(CONFIG_AP_NETMASK)
#error "AP network configuration must be defined in menuconfig"
#endif
#define AP_MAX_CONN 4 #define AP_MAX_CONN 4
#define AP_CHANNEL 6 #define AP_CHANNEL 6
@ -87,11 +99,12 @@ static esp_err_t receive_http_content(httpd_req_t *req, char **content) {
return ESP_FAIL; return ESP_FAIL;
} }
int ret = httpd_req_recv(req, *content, content_len); int ret;
if (ret <= 0) { int received = 0;
free(*content); while (received < content_len) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Failed to receive data"); ret = httpd_req_recv(req, *content + received, content_len - received);
return ESP_FAIL; if (ret <= 0) break;
received += ret;
} }
(*content)[content_len] = '\0'; (*content)[content_len] = '\0';
@ -146,7 +159,7 @@ esp_err_t adc_init(void) {
return ESP_OK; return ESP_OK;
} }
static void pump_init(void) { static esp_err_t pump_init(void) {
gpio_config_t io_conf = { gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << CONFIG_PUMP_PIN), .pin_bit_mask = (1ULL << CONFIG_PUMP_PIN),
.mode = GPIO_MODE_OUTPUT, .mode = GPIO_MODE_OUTPUT,
@ -154,12 +167,16 @@ static void pump_init(void) {
.pull_down_en = 1, .pull_down_en = 1,
.pull_up_en = 0, .pull_up_en = 0,
}; };
gpio_config(&io_conf);
esp_err_t ret = gpio_config(&io_conf);
if (ret != ESP_OK) return ret;
gpio_set_level(CONFIG_PUMP_PIN, false); gpio_set_level(CONFIG_PUMP_PIN, false);
return ESP_OK;
} }
int adc_read_raw(uint8_t channel) int adc_read_raw(uint8_t channel) {
{
int raw_value = 0; int raw_value = 0;
adc_channel_t adc_channel; adc_channel_t adc_channel;
@ -172,6 +189,11 @@ int adc_read_raw(uint8_t channel)
return -1; return -1;
} }
if (adc_handle == NULL) {
ESP_LOGE(TAG, "ADC not initialized");
return -1;
}
esp_err_t ret = adc_oneshot_read(adc_handle, adc_channel, &raw_value); esp_err_t ret = adc_oneshot_read(adc_handle, adc_channel, &raw_value);
if (ret != ESP_OK) { if (ret != ESP_OK) {
ESP_LOGE(TAG, "Error reading ADC"); ESP_LOGE(TAG, "Error reading ADC");
@ -249,9 +271,15 @@ static esp_err_t save_thresholds_handler(httpd_req_t *req) {
} }
err = nvs_commit(my_handle); err = nvs_commit(my_handle);
ESP_ERROR_CHECK(err); if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to commit NVS: %s", esp_err_to_name(err));
}
nvs_close(my_handle); nvs_close(my_handle);
atomic_store(&g_threshold_low, low_value);
atomic_store(&g_threshold_up, up_value);
return send_json_response(req, "{\"success\":true,\"low\":%d,\"up\":%d}", low_value, up_value); return send_json_response(req, "{\"success\":true,\"low\":%d,\"up\":%d}", low_value, up_value);
} }
@ -326,6 +354,8 @@ static void pump_enable(void) {
} }
static void vPumpControlTask(void *pvParameters) { static void vPumpControlTask(void *pvParameters) {
esp_task_wdt_add(xTaskGetCurrentTaskHandle());
while (1) { while (1) {
int current_pressure = atomic_load(&g_current_pressure); int current_pressure = atomic_load(&g_current_pressure);
int low_threshold = atomic_load(&g_threshold_low); int low_threshold = atomic_load(&g_threshold_low);
@ -338,6 +368,9 @@ static void vPumpControlTask(void *pvParameters) {
pump_disable(); pump_disable();
ESP_LOGI(TAG, "Pump disabled"); ESP_LOGI(TAG, "Pump disabled");
} }
esp_task_wdt_reset();
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
} }
} }
@ -352,7 +385,8 @@ static int read_pressure_filtered(void) {
} }
static void vReadSensorTask(void *pvParameters) { static void vReadSensorTask(void *pvParameters) {
esp_task_wdt_add(NULL); esp_task_wdt_add(xTaskGetCurrentTaskHandle());
while (1) { while (1) {
int pressure = read_pressure_filtered(); int pressure = read_pressure_filtered();
atomic_store(&g_current_pressure, pressure); atomic_store(&g_current_pressure, pressure);
@ -370,7 +404,9 @@ static void register_http_handlers(httpd_handle_t server) {
.handler = root_get_handler, .handler = root_get_handler,
.user_ctx = NULL .user_ctx = NULL
}; };
httpd_register_uri_handler(server, &root); if (httpd_register_uri_handler(server, &root) != ESP_OK) {
ESP_LOGE(TAG, "Failed to register / handler");
}
httpd_uri_t pressure = { httpd_uri_t pressure = {
.uri = "/pressure", .uri = "/pressure",
@ -378,7 +414,9 @@ static void register_http_handlers(httpd_handle_t server) {
.handler = current_pressure_handler, .handler = current_pressure_handler,
.user_ctx = NULL .user_ctx = NULL
}; };
httpd_register_uri_handler(server, &pressure); if (httpd_register_uri_handler(server, &pressure) != ESP_OK) {
ESP_LOGE(TAG, "Failed to register / handler");
}
httpd_uri_t set_thresholds = { httpd_uri_t set_thresholds = {
.uri = "/thresholds", .uri = "/thresholds",
@ -386,7 +424,9 @@ static void register_http_handlers(httpd_handle_t server) {
.handler = set_thresholds_handler, .handler = set_thresholds_handler,
.user_ctx = NULL .user_ctx = NULL
}; };
httpd_register_uri_handler(server, &set_thresholds); if (httpd_register_uri_handler(server, &set_thresholds) != ESP_OK) {
ESP_LOGE(TAG, "Failed to register / handler");
}
httpd_uri_t save_thresholds = { httpd_uri_t save_thresholds = {
.uri = "/persist_thresholds", .uri = "/persist_thresholds",
@ -394,7 +434,9 @@ static void register_http_handlers(httpd_handle_t server) {
.handler = save_thresholds_handler, .handler = save_thresholds_handler,
.user_ctx = NULL .user_ctx = NULL
}; };
httpd_register_uri_handler(server, &save_thresholds); if (httpd_register_uri_handler(server, &save_thresholds) != ESP_OK) {
ESP_LOGE(TAG, "Failed to register / handler");
}
} }
static void vHttpServerTask(void *pvParameters) { static void vHttpServerTask(void *pvParameters) {
@ -465,21 +507,17 @@ void app_main(void) {
}; };
ESP_ERROR_CHECK(esp_task_wdt_init(&twdt_config)); ESP_ERROR_CHECK(esp_task_wdt_init(&twdt_config));
// Добавляем main задачу
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
load_thresholds_from_nvs(); load_thresholds_from_nvs();
adc_init(); adc_init();
pump_init(); ESP_ERROR_CHECK(pump_init());
wifi_init_softap(); wifi_init_softap();
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
xTaskCreate(vReadSensorTask, "read_sensor", 2048, NULL, PRIORITY_SENSOR, NULL); xTaskCreate(vReadSensorTask, "read_sensor", 2048, NULL, PRIORITY_SENSOR, NULL);
xTaskCreate(vPumpControlTask, "pump_control", 2048, NULL, PRIORITY_CONTROL, NULL); xTaskCreate(vPumpControlTask, "pump_control", 2048, NULL, PRIORITY_CONTROL, NULL);
xTaskCreate(vHttpServerTask, "http_server", 4096, NULL, PRIORITY_HTTP, NULL); xTaskCreate(vHttpServerTask, "http_server", 8192, NULL, PRIORITY_HTTP, NULL);
ESP_LOGI(TAG, "========================================="); ESP_LOGI(TAG, "=========================================");
ESP_LOGI(TAG, "✅ Ready to work"); ESP_LOGI(TAG, "✅ Ready to work");