Skip to content

Commit

Permalink
Ability to change MQTT timeout and keepalive
Browse files Browse the repository at this point in the history
  • Loading branch information
gskjold committed Nov 9, 2024
1 parent e022f63 commit 05ce4c5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
4 changes: 3 additions & 1 deletion lib/AmsConfiguration/include/AmsConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ struct MqttConfig {
uint8_t magic;
bool stateUpdate;
uint16_t stateUpdateInterval;
}; // 680
uint16_t timeout;
uint8_t keepalive;
}; // 685

struct WebConfig {
uint8_t security;
Expand Down
18 changes: 14 additions & 4 deletions lib/AmsConfiguration/src/AmsConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ bool AmsConfiguration::getMqttConfig(MqttConfig& config) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_MQTT_START, config);
EEPROM.end();
if(config.magic != 0x7B) {
config.stateUpdate = false;
config.stateUpdateInterval = 10;
config.magic = 0x7B;
if(config.magic != 0x9C) {
if(config.magic != 0x7B) {
config.stateUpdate = false;
config.stateUpdateInterval = 10;
}
config.timeout = 1000;
config.keepalive = 60;
config.magic = 0x9C;
}
return true;
} else {
Expand Down Expand Up @@ -183,6 +187,10 @@ bool AmsConfiguration::setMqttConfig(MqttConfig& config) {
stripNonAscii((uint8_t*) config.subscribeTopic, 64);
stripNonAscii((uint8_t*) config.username, 128);
stripNonAscii((uint8_t*) config.password, 256);
if(config.timeout < 500) config.timeout = 1000;
if(config.timeout > 10000) config.timeout = 1000;
if(config.keepalive < 5) config.keepalive = 60;
if(config.keepalive > 240) config.keepalive = 60;

EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_MQTT_START, config);
Expand All @@ -205,6 +213,8 @@ void AmsConfiguration::clearMqtt(MqttConfig& config) {
config.magic = 0x7B;
config.stateUpdate = false;
config.stateUpdateInterval = 10;
config.timeout = 1000;
config.keepalive = 60;
}

void AmsConfiguration::setMqttChanged() {
Expand Down
1 change: 1 addition & 0 deletions lib/AmsMqttHandler/src/AmsMqttHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ bool AmsMqttHandler::connect() {
}

mqttConfigChanged = false;
mqtt.setTimeout(mqttConfig.timeout);
mqtt.begin(mqttConfig.host, mqttConfig.port, *actualClient);
String statusTopic = String(mqttConfig.publishTopic) + "/status";
mqtt.setWill(statusTopic.c_str(), "offline", true, 0);
Expand Down
12 changes: 6 additions & 6 deletions lib/SvelteUi/app/dist/index.js

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions lib/SvelteUi/app/src/lib/ConfigurationPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,8 @@
<select name="qm" bind:value={configuration.q.m} class="in-l">
<option value={1}>Raw (minimal)</option>
<option value={2}>Raw (full)</option>
<option value={3}>{translations.conf?.mqtt?.domoticz?.title ?? "Domoticz"}</option>
<option value={4}>{translations.conf?.mqtt?.ha?.title ?? "Home-Assistant"}</option>
<option value={3}>Domoticz</option>
<option value={4}>Home-Assistant</option>
<option value={0}>JSON (classic)</option>
<option value={5}>JSON (multi topic)</option>
<option value={6}>JSON (flat)</option>
Expand All @@ -620,15 +620,23 @@
</div>
<div class="my-1">
{translations.conf?.mqtt?.update ?? "Update method"}
<span class="float-right">Interval</span>
<span class="float-right">{translations.conf?.mqtt?.interval ?? "Interval"}</span>
<div class="flex">
<select name="qt" bind:value={configuration.q.t} class="in-f w-1/2">
<option value={0}>Real time</option>
<option value={1}>Interval</option>
<option value={0}>{translations.conf?.mqtt?.realtime ?? "Real time"}</option>
<option value={1}>{translations.conf?.mqtt?.interval ?? "Interval"}</option>
</select>
<input name="qd" bind:value={configuration.q.d} type="number" min="1" max="3600" class="in-l tr w-1/2" disabled={configuration?.q?.t != 1}/>
</div>
</div>
<div class="my-1">
{translations.conf?.mqtt?.timeout ?? "Timeout"}
<span class="float-right">{translations.conf?.mqtt?.keepalive ?? "Keep-alive"}</span>
<div class="flex">
<input name="qi" bind:value={configuration.q.i} type="number" min="500" max="10000" class="in-f tr w-1/2"/>
<input name="qk" bind:value={configuration.q.k} type="number" min="5" max="180" class="in-l tr w-1/2"/>
</div>
</div>
</div>
{/if}
{#if configuration?.q?.m == 3}
Expand Down
4 changes: 3 additions & 1 deletion lib/SvelteUi/json/conf_mqtt.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
"k": %s
},
"t": %d,
"d": %d
"d": %d,
"i": %d,
"k": %d
},
6 changes: 5 additions & 1 deletion lib/SvelteUi/src/AmsWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,9 @@ void AmsWebServer::configurationJson() {
qsr ? "true" : "false",
qsk ? "true" : "false",
mqttConfig.stateUpdate,
mqttConfig.stateUpdateInterval
mqttConfig.stateUpdateInterval,
mqttConfig.timeout,
mqttConfig.keepalive
);
server.sendContent(buf);

Expand Down Expand Up @@ -1389,6 +1391,8 @@ void AmsWebServer::handleSave() {

mqtt.stateUpdate = server.arg(F("qt")).toInt() == 1;
mqtt.stateUpdateInterval = server.arg(F("qd")).toInt();
mqtt.timeout = server.arg(F("qi")).toInt();
mqtt.keepalive = server.arg(F("qk")).toInt();
} else {
config->clearMqtt(mqtt);
}
Expand Down

0 comments on commit 05ce4c5

Please sign in to comment.