From b93edc248dfce3c37ec2a9c0e2d555611dceecc8 Mon Sep 17 00:00:00 2001 From: nick Date: Mon, 15 Jun 2026 14:50:27 -0600 Subject: [PATCH] dashboard: improve relay command responsiveness --- .../esp32-s3-dashboard/esp32-s3-dashboard.ino | 33 +++++++++++++++---- .../overland-controller.ino | 16 +++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/firmware/esp32-s3-dashboard/esp32-s3-dashboard.ino b/firmware/esp32-s3-dashboard/esp32-s3-dashboard.ino index fe1cd66..20cc64d 100644 --- a/firmware/esp32-s3-dashboard/esp32-s3-dashboard.ino +++ b/firmware/esp32-s3-dashboard/esp32-s3-dashboard.ino @@ -479,21 +479,42 @@ static bool post_relay_state(const String &relay_id, bool state) Serial.println(payload); HTTPClient http; - http.setTimeout(5000); + http.setTimeout(1200); http.begin(CARGO_API_RELAY_SET_URL); http.addHeader("Content-Type", "application/json"); + unsigned long start_ms = millis(); int code = http.POST(payload); String body = http.getString(); Serial.print("Relay HTTP code: "); - Serial.println(code); + Serial.print(code); + Serial.print(" total="); + Serial.print(millis() - start_ms); + Serial.println("ms"); Serial.println(body); + bool ok = code >= 200 && code < 300; + + if (ok && body.length() > 0) { + DynamicJsonDocument response(512); + DeserializationError error = deserializeJson(response, body); + if (!error && response["state"].is()) { + bool applied_state = response["state"].as(); + const char *applied_id = response["id"] | ""; + for (int i = 0; i < relay_count; i++) { + if (relay_ids[i] == String(applied_id)) { + relay_states[i] = applied_state; + break; + } + } + } + } + http.end(); api_request_in_progress = false; - return code >= 200 && code < 300; + return ok; } static void relay_button_event_cb(lv_event_t *event) @@ -1030,7 +1051,7 @@ static bool fetch_status_fields(const char *url, const char *label) unsigned long start_ms = millis(); HTTPClient http; - http.setTimeout(5000); + http.setTimeout(900); http.begin(url); int code = http.GET(); @@ -1066,7 +1087,7 @@ static void poll_status_api() return; } - if (fast_status_refresh_requested || millis() - last_status_poll_ms >= 2000) { + if (fast_status_refresh_requested || millis() - last_status_poll_ms >= 1500) { fast_status_refresh_requested = false; last_status_poll_ms = millis(); fetch_status_fields(CARGO_API_FAST_STATUS_URL, "fast status"); @@ -1444,5 +1465,5 @@ void loop() process_pending_relay_command(); poll_status_api(); - delay(250); + delay(50); } diff --git a/firmware/esp32/overland-controller/overland-controller.ino b/firmware/esp32/overland-controller/overland-controller.ino index 37e2ea5..626a4e4 100644 --- a/firmware/esp32/overland-controller/overland-controller.ino +++ b/firmware/esp32/overland-controller/overland-controller.ino @@ -3829,6 +3829,8 @@ void handleSelectBms() { void handleSetRelayPost() { + unsigned long startMs = millis(); + DynamicJsonDocument doc(512); DeserializationError error = deserializeJson(doc, server.arg("plain")); @@ -3840,20 +3842,34 @@ void handleSetRelayPost() { String relayId = doc["id"] | ""; bool state = doc["state"] | false; + unsigned long applyStartMs = millis(); if (!setRelayById(relayId, state)) { server.send(404, "application/json", "{\"ok\":false,\"error\":\"unknown_relay\"}"); return; } + unsigned long appliedMs = millis() - applyStartMs; DynamicJsonDocument response(512); response["type"] = "relay_response"; response["ok"] = true; response["id"] = relayId; response["state"] = state; + response["applied_ms"] = appliedMs; + response["total_ms"] = millis() - startMs; String output; serializeJson(response, output); server.send(200, "application/json", output); + + Serial.print("Relay "); + Serial.print(relayId); + Serial.print(" -> "); + Serial.print(state ? "ON" : "OFF"); + Serial.print(" applied="); + Serial.print(appliedMs); + Serial.print("ms total="); + Serial.print(millis() - startMs); + Serial.println("ms"); } void handleGenericRelayRoute() {