Skip to content

Commit

Permalink
Fix web control page.
Browse files Browse the repository at this point in the history
settings updates in change_states() were failing as the
String.c_str() values go out of scope too quickly.
The scope of changes is now more tightly controlled.

More details are also logged if debug logging is enabled.

The control web page now auto-refreshes when settings changes
are received from the hvac unit, either from external changes,
mqtt changes or just delayed responses to web changes.
  • Loading branch information
andrewleech committed Feb 9, 2024
1 parent 0a7d5ef commit 47cd6b0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
19 changes: 19 additions & 0 deletions src/mitsubishi2mqtt/html_pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();"
"}"
"</script>"
;

Expand Down
64 changes: 51 additions & 13 deletions src/mitsubishi2mqtt/mitsubishi2mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ boolean remoteTempActive = false;

//HVAC
HeatPump hp;
unsigned long lastUpdated = 0;
unsigned long lastTempSend;
unsigned long lastMqttRetry;
unsigned long lastHpSync;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -909,8 +911,6 @@ void handleStatus() {
sendWrappedHTML(statusPage);
}



void handleControl() {
if (!checkLogin()) return;

Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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() {
Expand All @@ -1374,6 +1411,7 @@ void hpSettingsChanged() {
}

hpStatusChanged(hp.getStatus());
lastUpdated = millis();
}

String hpGetMode(heatpumpSettings hpSettings) {
Expand Down

0 comments on commit 47cd6b0

Please sign in to comment.