diff --git a/src/mitsubishi2mqtt/html_pages.h b/src/mitsubishi2mqtt/html_pages.h index bb0eb08..ea962c9 100644 --- a/src/mitsubishi2mqtt/html_pages.h +++ b/src/mitsubishi2mqtt/html_pages.h @@ -282,6 +282,25 @@ const char html_page_control[] PROGMEM = "var options = document.getElementById('MODE').options;" "options[3].disabled = (options[3].value == 'HEAT');" "}" + +"setInterval(function() {" + "checkUpdated();" +"}, 1000);" + +"var lastUpdated = _LAST_UPDATED_;" +"function checkUpdated() {" + "var xhttp = new XMLHttpRequest();" + "xhttp.onreadystatechange = function() {" + "if (this.readyState == 4 && this.status == 200) {" + "var updated = parseInt(this.responseText);" + "if (updated != lastUpdated) {" + "location.reload();" + "}" + "}" + "};" + "xhttp.open('GET', 'updated', true);" + "xhttp.send();" +"}" "" ; diff --git a/src/mitsubishi2mqtt/mitsubishi2mqtt.ino b/src/mitsubishi2mqtt/mitsubishi2mqtt.ino index acb12d2..b54cf4d 100644 --- a/src/mitsubishi2mqtt/mitsubishi2mqtt.ino +++ b/src/mitsubishi2mqtt/mitsubishi2mqtt.ino @@ -89,6 +89,7 @@ boolean remoteTempActive = false; //HVAC HeatPump hp; +unsigned long lastUpdated = 0; unsigned long lastTempSend; unsigned long lastMqttRetry; unsigned long lastHpSync; @@ -187,6 +188,7 @@ void setup() { //Web interface server.on("/", handleRoot); server.on("/control", handleControl); + server.on("/updated", handleUpdated); server.on("/setup", handleSetup); server.on("/mqtt", handleMqtt); server.on("/wifi", handleWifi); @@ -909,8 +911,6 @@ void handleStatus() { sendWrappedHTML(statusPage); } - - void handleControl() { if (!checkLogin()) return; @@ -921,8 +921,8 @@ void handleControl() { server.send(302); return; } - heatpumpSettings settings = hp.getSettings(); - settings = change_states(settings); + + heatpumpSettings settings = change_states(); String controlPage = FPSTR(html_page_control); String headerContent = FPSTR(html_common_header); String footerContent = FPSTR(html_common_footer); @@ -936,6 +936,7 @@ void handleControl() { controlPage.replace("_USE_FAHRENHEIT_", (String)useFahrenheit); controlPage.replace("_TEMP_SCALE_", getTemperatureScale()); controlPage.replace("_HEAT_MODE_SUPPORT_", (String)supportHeatMode); + controlPage.replace("_LAST_UPDATED_", String(lastUpdated)); controlPage.replace(F("_MIN_TEMP_"), String(convertCelsiusToLocalUnit(min_temp, useFahrenheit))); controlPage.replace(F("_MAX_TEMP_"), String(convertCelsiusToLocalUnit(max_temp, useFahrenheit))); controlPage.replace(F("_TEMP_STEP_"), String(temp_step)); @@ -1060,6 +1061,13 @@ void handleControl() { //delay(100); } +void handleUpdated() { + // Simply send the timestamp of the last settings update from the hvac + if (!checkLogin()) return; + + server.send(200, "text/plain", String(lastUpdated)); +} + void handleMetrics(){ String metrics = FPSTR(html_metrics); @@ -1314,41 +1322,70 @@ void write_log(String log) { logFile.close(); } -heatpumpSettings change_states(heatpumpSettings settings) { +void debug_log_settings(heatpumpSettings settings) { + const size_t capacity = 512; + DynamicJsonDocument debuglog(capacity); + debuglog["Power"] = settings.power; + debuglog["Mode"] = settings.mode; + debuglog["Temperature"] = settings.temperature; + debuglog["Fan"] = settings.fan; + debuglog["Vane"] = settings.vane; + debuglog["Widevane"] = settings.wideVane; + + String mqttOutput; + serializeJson(debuglog, mqttOutput); + DEBUG_LOG(mqttOutput); +} + +heatpumpSettings change_states() { if (server.hasArg("CONNECT")) { hp.connect(SerialHvac, HVAC_UART_RX, HVAC_UART_TX); } else { bool update = false; if (server.hasArg("POWER")) { - settings.power = server.arg("POWER").c_str(); + String arg = server.arg("POWER"); + DEBUG_LOG(String("WEB: POWER = ") + arg); + hp.setPowerSetting(arg.c_str()); update = true; } if (server.hasArg("MODE")) { - settings.mode = server.arg("MODE").c_str(); + String arg = server.arg("MODE"); + DEBUG_LOG(String("WEB: MODE = ") + arg); + hp.setModeSetting(arg.c_str()); update = true; } if (server.hasArg("TEMP")) { - settings.temperature = convertLocalUnitToCelsius(server.arg("TEMP").toInt(), useFahrenheit); + String arg = server.arg("TEMP"); + DEBUG_LOG(String("WEB: TEMP = ") + arg); + float temperature = convertLocalUnitToCelsius(arg.toInt(), useFahrenheit); + hp.setTemperature(temperature); update = true; } if (server.hasArg("FAN")) { - settings.fan = server.arg("FAN").c_str(); + String arg = server.arg("FAN"); + DEBUG_LOG(String("WEB: FAN = ") + arg); + hp.setFanSpeed(arg.c_str()); update = true; } if (server.hasArg("VANE")) { - settings.vane = server.arg("VANE").c_str(); + String arg = server.arg("VANE"); + DEBUG_LOG(String("WEB: VANE = ") + arg); + hp.setVaneSetting(arg.c_str()); update = true; } if (server.hasArg("WIDEVANE")) { - settings.wideVane = server.arg("WIDEVANE").c_str(); + String arg = server.arg("WIDEVANE"); + DEBUG_LOG(String("WEB: WIDEVANE = ") + arg); + hp.setWideVaneSetting(arg.c_str()); update = true; } if (update) { - hp.setSettings(settings); + hp.update(); + hp.update(); } } - return settings; + return hp.getSettings(); } void readHeatPumpSettings() { @@ -1374,6 +1411,7 @@ void hpSettingsChanged() { } hpStatusChanged(hp.getStatus()); + lastUpdated = millis(); } String hpGetMode(heatpumpSettings hpSettings) {