fix pressure measurement

This commit is contained in:
thek4n 2026-06-05 19:40:50 +03:00
parent 7fac1ddc6b
commit 7f58c2adbc
3 changed files with 33 additions and 24 deletions

View File

@ -517,7 +517,7 @@
throw new Error('Failed to fetch pressure'); throw new Error('Failed to fetch pressure');
} }
const data = await response.json(); const data = await response.json();
const pressure = typeof data === 'number' ? data : data.value; const pressure = typeof data === 'number' ? data : data.value / 100;
if (typeof pressure === 'number' && !isNaN(pressure)) { if (typeof pressure === 'number' && !isNaN(pressure)) {
currentPressure = Math.min(Math.max(pressure, MIN_PRESSURE), MAX_PRESSURE); currentPressure = Math.min(Math.max(pressure, MIN_PRESSURE), MAX_PRESSURE);
pressureDisplay.innerText = currentPressure.toFixed(2) + " атм"; pressureDisplay.innerText = currentPressure.toFixed(2) + " атм";
@ -536,8 +536,8 @@
} }
const data = await response.json(); const data = await response.json();
if (typeof data.low === 'number' && typeof data.up === 'number') { if (typeof data.low === 'number' && typeof data.up === 'number') {
minThreshold = Math.min(Math.max(data.low, MIN_PRESSURE), MAX_PRESSURE); minThreshold = Math.min(Math.max(data.low / 100, MIN_PRESSURE), MAX_PRESSURE);
maxThreshold = Math.min(Math.max(data.up, MIN_PRESSURE), MAX_PRESSURE); maxThreshold = Math.min(Math.max(data.up / 100, MIN_PRESSURE), MAX_PRESSURE);
updateUI(); updateUI();
} }
} catch (error) { } catch (error) {
@ -562,8 +562,8 @@
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
low: parseFloat(minThreshold.toFixed(2)), low: parseFloat(minThreshold * 100),
up: parseFloat(maxThreshold.toFixed(2)) up: parseFloat(maxThreshold * 100)
}) })
}); });
@ -594,8 +594,8 @@
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
low: parseFloat(minThreshold.toFixed(2)), low: parseFloat(minThreshold * 100),
up: parseFloat(maxThreshold.toFixed(2)) up: parseFloat(maxThreshold * 100)
}) })
}); });
@ -654,7 +654,7 @@
ctx.font = `500 ${w * 0.042}px "Segoe UI"`; ctx.font = `500 ${w * 0.042}px "Segoe UI"`;
ctx.fillStyle = '#b8cfec'; ctx.fillStyle = '#b8cfec';
ctx.shadowBlur = 2; ctx.shadowBlur = 2;
ctx.fillText("атм", centerX - 18, centerY + radius * 0.69); ctx.fillText("atm", centerX - 18, centerY + radius * 0.69);
ctx.fillStyle = '#85a9d0'; ctx.fillStyle = '#85a9d0';
ctx.font = `300 ${w * 0.028}px monospace`; ctx.font = `300 ${w * 0.028}px monospace`;
ctx.shadowBlur = 0; ctx.shadowBlur = 0;

View File

@ -12,7 +12,7 @@ dependencies:
idf: idf:
source: source:
type: idf type: idf
version: 6.2.0 version: 6.1.0
direct_dependencies: direct_dependencies:
- espressif/cjson - espressif/cjson
- idf - idf

View File

@ -21,6 +21,7 @@
#include "esp_adc/adc_cali_scheme.h" #include "esp_adc/adc_cali_scheme.h"
#include "esp_task_wdt.h" #include "esp_task_wdt.h"
#include "lwip/inet.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "frontend.h" #include "frontend.h"
@ -280,6 +281,8 @@ static esp_err_t save_thresholds_handler(httpd_req_t *req) {
atomic_store(&g_threshold_low, low_value); atomic_store(&g_threshold_low, low_value);
atomic_store(&g_threshold_up, up_value); atomic_store(&g_threshold_up, up_value);
ESP_LOGI(TAG, "Thresholds saved: low: %d, up: %d", low_value, 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);
} }
@ -311,6 +314,7 @@ static esp_err_t set_thresholds_handler(httpd_req_t *req) {
atomic_store(&g_threshold_low, low_value); atomic_store(&g_threshold_low, low_value);
atomic_store(&g_threshold_up, up_value); atomic_store(&g_threshold_up, up_value);
ESP_LOGI(TAG, "Thresholds updated: low: %d, up: %d", low_value, 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);
} }
@ -343,9 +347,9 @@ void wifi_init_softap(void) {
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
esp_netif_ip_info_t ip_info; esp_netif_ip_info_t ip_info;
ip_info.ip.addr = ipaddr_addr(CONFIG_AP_IP); ip_info.ip.addr = inet_addr(CONFIG_AP_IP);
ip_info.gw.addr = ipaddr_addr(CONFIG_AP_GATEWAY); ip_info.gw.addr = inet_addr(CONFIG_AP_GATEWAY);
ip_info.netmask.addr = ipaddr_addr(CONFIG_AP_NETMASK); ip_info.netmask.addr = inet_addr(CONFIG_AP_NETMASK);
ESP_ERROR_CHECK(esp_netif_dhcps_stop(ap_netif)); ESP_ERROR_CHECK(esp_netif_dhcps_stop(ap_netif));
ESP_ERROR_CHECK(esp_netif_set_ip_info(ap_netif, &ip_info)); ESP_ERROR_CHECK(esp_netif_set_ip_info(ap_netif, &ip_info));
@ -362,17 +366,20 @@ static void pump_enable(void) {
static void vPumpControlTask(void *pvParameters) { static void vPumpControlTask(void *pvParameters) {
esp_task_wdt_add(xTaskGetCurrentTaskHandle()); esp_task_wdt_add(xTaskGetCurrentTaskHandle());
bool pump_enabled = false;
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);
int up_threshold = atomic_load(&g_threshold_up); int up_threshold = atomic_load(&g_threshold_up);
if (current_pressure < low_threshold) { if ((current_pressure < low_threshold) && (!pump_enabled)) {
pump_enable(); pump_enable();
pump_enabled = true;
ESP_LOGI(TAG, "Pump enabled"); ESP_LOGI(TAG, "Pump enabled");
} else if (current_pressure >= up_threshold) { } else if ((current_pressure >= up_threshold) && pump_enabled) {
pump_disable(); pump_disable();
pump_enabled = false;
ESP_LOGI(TAG, "Pump disabled"); ESP_LOGI(TAG, "Pump disabled");
} }
@ -382,7 +389,7 @@ static void vPumpControlTask(void *pvParameters) {
} }
} }
static int read_pressure_filtered(void) { static int read_voltage_filtered(void) {
int sum = 0; int sum = 0;
for (int i = 0; i < FILTER_SAMPLES; i++) { for (int i = 0; i < FILTER_SAMPLES; i++) {
sum += adc_read_voltage(SENSOR_ADC_CHAN); sum += adc_read_voltage(SENSOR_ADC_CHAN);
@ -391,12 +398,22 @@ static int read_pressure_filtered(void) {
return sum / FILTER_SAMPLES; return sum / FILTER_SAMPLES;
} }
int map(int x, int in_min, int in_max, int out_min, int out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
static int convert_adc_to_pressure_atm(int adc_value) {
return map(adc_value, 330, 3145, 0, 1184);
}
static void vReadSensorTask(void *pvParameters) { static void vReadSensorTask(void *pvParameters) {
esp_task_wdt_add(xTaskGetCurrentTaskHandle()); esp_task_wdt_add(xTaskGetCurrentTaskHandle());
while (1) { while (1) {
int pressure = read_pressure_filtered(); int voltage = read_voltage_filtered();
int pressure = convert_adc_to_pressure_atm(voltage);
atomic_store(&g_current_pressure, pressure); atomic_store(&g_current_pressure, pressure);
ESP_LOGI(TAG, "current pressure %d", pressure);
esp_task_wdt_reset(); esp_task_wdt_reset();
@ -517,14 +534,6 @@ void app_main(void) {
} }
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
esp_task_wdt_config_t twdt_config = {
.timeout_ms = 10000, // 10 секунд таймаут
.idle_core_mask = (1 << 0) | (1 << 1), // Мониторинг idle-задач на обоих ядрах
.trigger_panic = true // Паника при таймауте (перезагрузка)
};
ESP_ERROR_CHECK(esp_task_wdt_init(&twdt_config));
load_thresholds_from_nvs(); load_thresholds_from_nvs();
ESP_ERROR_CHECK(adc_init()); ESP_ERROR_CHECK(adc_init());
ESP_ERROR_CHECK(pump_init()); ESP_ERROR_CHECK(pump_init());