default MQTT qos to 0 and added Retain flag option

This commit is contained in:
proddy
2019-10-15 10:02:50 +02:00
parent f39ecb860a
commit 0a6c902872
6 changed files with 41 additions and 10 deletions

View File

@@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Retrieve/Set thermostat mode for Junkers FW100/120 thermostats (thanks @Neonox31) - Retrieve/Set thermostat mode for Junkers FW100/120 thermostats (thanks @Neonox31)
- Added sending of all Mixer Module data via MQTT (thanks @peclik) - Added sending of all Mixer Module data via MQTT (thanks @peclik)
- Improved handling of MQTT publish and subscribe errors - Improved handling of MQTT publish and subscribe errors
- Added MQTT QOS (`mqtt_qos`, default 1) and Keep Alive (`mqtt_keepalive`, default 60 seconds) as parameters to both telnet and WebUI - Added MQTT QOS (`mqtt_qos`, default 1), Keep Alive (`mqtt_keepalive`, default 60 seconds) and Retain (`mqtt_retain`, default on) as parameters to both telnet and WebUI
### Fixed ### Fixed

View File

@@ -379,9 +379,10 @@ bool MyESP::mqttSubscribe(const char * topic) {
return true; return true;
} else { } else {
myDebug_P(PSTR("[MQTT] Error subscribing to %s, error %d"), _mqttTopic(topic), packet_id); myDebug_P(PSTR("[MQTT] Error subscribing to %s, error %d"), _mqttTopic(topic), packet_id);
return false;
} }
} }
return false; // didn't work
} }
// MQTT unsubscribe // MQTT unsubscribe
@@ -681,6 +682,7 @@ void MyESP::_printSetCommands() {
myDebug_P(PSTR(" set mqtt_port [number]")); myDebug_P(PSTR(" set mqtt_port [number]"));
myDebug_P(PSTR(" set mqtt_qos [0,1,2,3]")); myDebug_P(PSTR(" set mqtt_qos [0,1,2,3]"));
myDebug_P(PSTR(" set mqtt_keepalive [seconds]")); myDebug_P(PSTR(" set mqtt_keepalive [seconds]"));
myDebug_P(PSTR(" set mqtt_retain [on | off]"));
myDebug_P(PSTR(" set ntp_enabled <on | off>")); myDebug_P(PSTR(" set ntp_enabled <on | off>"));
myDebug_P(PSTR(" set serial <on | off>")); myDebug_P(PSTR(" set serial <on | off>"));
myDebug_P(PSTR(" set log_events <on | off>")); myDebug_P(PSTR(" set log_events <on | off>"));
@@ -735,6 +737,7 @@ void MyESP::_printSetCommands() {
} }
myDebug_P(PSTR(" mqtt_port=%d"), _mqtt_port); myDebug_P(PSTR(" mqtt_port=%d"), _mqtt_port);
myDebug_P(PSTR(" mqtt_keepalive=%d"), _mqtt_keepalive); myDebug_P(PSTR(" mqtt_keepalive=%d"), _mqtt_keepalive);
myDebug_P(PSTR(" mqtt_retain=%d"), (_mqtt_retain) ? "on" : "off");
myDebug_P(PSTR(" mqtt_qos=%d"), _mqtt_qos); myDebug_P(PSTR(" mqtt_qos=%d"), _mqtt_qos);
myDebug_P(PSTR(" mqtt_heartbeat=%s"), (_mqtt_heartbeat) ? "on" : "off"); myDebug_P(PSTR(" mqtt_heartbeat=%s"), (_mqtt_heartbeat) ? "on" : "off");
@@ -825,6 +828,8 @@ bool MyESP::_changeSetting(uint8_t wc, const char * setting, const char * value)
save_config = fs_setSettingValue(&_mqtt_qos, value, MQTT_QOS); save_config = fs_setSettingValue(&_mqtt_qos, value, MQTT_QOS);
} else if (strcmp(setting, "mqtt_enabled") == 0) { } else if (strcmp(setting, "mqtt_enabled") == 0) {
save_config = fs_setSettingValue(&_mqtt_enabled, value, false); save_config = fs_setSettingValue(&_mqtt_enabled, value, false);
} else if (strcmp(setting, "mqtt_retain") == 0) {
save_config = fs_setSettingValue(&_mqtt_retain, value, MQTT_RETAIN);
} else if (strcmp(setting, "serial") == 0) { } else if (strcmp(setting, "serial") == 0) {
save_config = fs_setSettingValue(&_general_serial, value, false); save_config = fs_setSettingValue(&_general_serial, value, false);
restart = save_config; restart = save_config;
@@ -1699,6 +1704,7 @@ bool MyESP::_fs_loadConfig() {
_mqtt_user = strdup(mqtt["user"] | ""); _mqtt_user = strdup(mqtt["user"] | "");
_mqtt_port = mqtt["port"] | MQTT_PORT; _mqtt_port = mqtt["port"] | MQTT_PORT;
_mqtt_keepalive = mqtt["keepalive"] | MQTT_KEEPALIVE; _mqtt_keepalive = mqtt["keepalive"] | MQTT_KEEPALIVE;
_mqtt_retain = mqtt["retain"];
_mqtt_qos = mqtt["qos"] | MQTT_QOS; _mqtt_qos = mqtt["qos"] | MQTT_QOS;
_mqtt_password = strdup(mqtt["password"] | ""); _mqtt_password = strdup(mqtt["password"] | "");
_mqtt_base = strdup(mqtt["base"] | MQTT_BASE_DEFAULT); _mqtt_base = strdup(mqtt["base"] | MQTT_BASE_DEFAULT);
@@ -1759,9 +1765,9 @@ bool MyESP::fs_setSettingValue(uint8_t * setting, const char * value, uint8_t va
// returns true if successful // returns true if successful
bool MyESP::fs_setSettingValue(bool * setting, const char * value, bool value_default) { bool MyESP::fs_setSettingValue(bool * setting, const char * value, bool value_default) {
if (_hasValue(value)) { if (_hasValue(value)) {
if ((strcmp(value, "on") == 0) || (strcmp(value, "yes") == 0) || (strcmp(value, "1") == 0)) { if ((strcmp(value, "on") == 0) || (strcmp(value, "yes") == 0) || (strcmp(value, "1") == 0) || (strcmp(value, "true") == 0)) {
*setting = true; *setting = true;
} else if ((strcmp(value, "off") == 0) || (strcmp(value, "no") == 0) || (strcmp(value, "0") == 0)) { } else if ((strcmp(value, "off") == 0) || (strcmp(value, "no") == 0) || (strcmp(value, "0") == 0) || (strcmp(value, "false") == 0)) {
*setting = false; *setting = false;
} else { } else {
return false; // invalid setting value return false; // invalid setting value
@@ -1903,6 +1909,7 @@ bool MyESP::_fs_writeConfig() {
mqtt["port"] = _mqtt_port; mqtt["port"] = _mqtt_port;
mqtt["qos"] = _mqtt_qos; mqtt["qos"] = _mqtt_qos;
mqtt["keepalive"] = _mqtt_keepalive; mqtt["keepalive"] = _mqtt_keepalive;
mqtt["retain"] = _mqtt_retain;
mqtt["password"] = _mqtt_password; mqtt["password"] = _mqtt_password;
mqtt["base"] = _mqtt_base; mqtt["base"] = _mqtt_base;

View File

@@ -79,9 +79,9 @@ extern struct rst_info resetInfo;
#define MQTT_WILL_ONLINE_PAYLOAD "online" // for last will & testament payload #define MQTT_WILL_ONLINE_PAYLOAD "online" // for last will & testament payload
#define MQTT_WILL_OFFLINE_PAYLOAD "offline" // for last will & testament payload #define MQTT_WILL_OFFLINE_PAYLOAD "offline" // for last will & testament payload
#define MQTT_BASE_DEFAULT "home" // default MQTT prefix to topics #define MQTT_BASE_DEFAULT "home" // default MQTT prefix to topics
#define MQTT_RETAIN false #define MQTT_RETAIN true
#define MQTT_KEEPALIVE 60 // default keepalive 1 minute #define MQTT_KEEPALIVE 60 // default keepalive 1 minute
#define MQTT_QOS 1 // default qos #define MQTT_QOS 0 // default qos 0
#define MQTT_WILL_TOPIC "status" // for last will & testament topic name #define MQTT_WILL_TOPIC "status" // for last will & testament topic name
#define MQTT_MAX_TOPIC_SIZE 50 // max length of MQTT topic #define MQTT_MAX_TOPIC_SIZE 50 // max length of MQTT topic
#define MQTT_MAX_PAYLOAD_SIZE 700 // max size of a JSON object. See https://arduinojson.org/v6/assistant/ #define MQTT_MAX_PAYLOAD_SIZE 700 // max size of a JSON object. See https://arduinojson.org/v6/assistant/

View File

@@ -226,6 +226,19 @@
</span> </span>
<br> <br>
</div> </div>
<div class="row form-group">
<label class="col-xs-3">Retain<i style="margin-left: 10px;" class="glyphicon glyphicon-info-sign"
aria-hidden="true" data-toggle="popover" data-trigger="hover" data-placement="right"
data-content="MQTT Retain Flag"></i></label>
<div class="col-xs-9">
<form>
<label class="radio-inline">
<input type="radio" value="1" name="mqttretain">Enabled</label>
<label class="radio-inline">
<input type="radio" value="0" name="mqttretain" checked>Disabled</label>
</form>
</div>
</div>
<div class="row form-group"> <div class="row form-group">
<label class="col-xs-3">Username<i style="margin-left: 10px;" class="glyphicon glyphicon-info-sign" <label class="col-xs-3">Username<i style="margin-left: 10px;" class="glyphicon glyphicon-info-sign"
aria-hidden="true" data-toggle="popover" data-trigger="hover" data-placement="right" aria-hidden="true" data-toggle="popover" data-trigger="hover" data-placement="right"

View File

@@ -27,6 +27,7 @@ var config = {
"port": 1883, "port": 1883,
"qos": 1, "qos": 1,
"keepalive": 60, "keepalive": 60,
"retain": true,
"base": "", "base": "",
"user": "", "user": "",
"password": "", "password": "",
@@ -173,6 +174,11 @@ function savemqtt() {
config.mqtt.heartbeat = true; config.mqtt.heartbeat = true;
} }
config.mqtt.retain = false;
if (parseInt($("input[name=\"mqttretain\"]:checked").val()) === 1) {
config.mqtt.retain = true;
}
config.mqtt.ip = document.getElementById("mqttip").value; config.mqtt.ip = document.getElementById("mqttip").value;
config.mqtt.port = parseInt(document.getElementById("mqttport").value); config.mqtt.port = parseInt(document.getElementById("mqttport").value);
config.mqtt.qos = parseInt(document.getElementById("mqttqos").value); config.mqtt.qos = parseInt(document.getElementById("mqttqos").value);
@@ -325,6 +331,10 @@ function listmqtt() {
$("input[name=\"mqttheartbeat\"][value=\"1\"]").prop("checked", true); $("input[name=\"mqttheartbeat\"][value=\"1\"]").prop("checked", true);
} }
if (config.mqtt.retain) {
$("input[name=\"mqttretain\"][value=\"1\"]").prop("checked", true);
}
document.getElementById("mqttip").value = config.mqtt.ip; document.getElementById("mqttip").value = config.mqtt.ip;
document.getElementById("mqttport").value = config.mqtt.port; document.getElementById("mqttport").value = config.mqtt.port;
document.getElementById("mqttqos").value = config.mqtt.qos; document.getElementById("mqttqos").value = config.mqtt.qos;

View File

@@ -74,10 +74,11 @@ var configfile = {
}, },
"mqtt": { "mqtt": {
"enabled": false, "enabled": false,
"ip": "ip", "ip": "10.10.10.10",
"port": "port", "port": 1883,
"qos": "qos", "qos": 1,
"keepalive": "keepalive", "keepalive": 60,
"retain": true,
"base": "base", "base": "base",
"user": "user", "user": "user",
"password": "password", "password": "password",