Added cnfig parameter to set mqtt publish interval

This commit is contained in:
Thomas Basler 2022-06-16 14:57:12 +02:00
parent 52b5cdbdb6
commit 7e0807bda6
5 changed files with 21 additions and 3 deletions

View File

@ -3,7 +3,7 @@
#include <Arduino.h>
#define CONFIG_FILENAME "/config.bin"
#define CONFIG_VERSION 0x00010900 // 0.1.9 // make sure to clean all after change
#define CONFIG_VERSION 0x00011000 // 0.1.10 // make sure to clean all after change
#define WIFI_MAX_SSID_STRLEN 31
#define WIFI_MAX_PASSWORD_STRLEN 31
@ -61,6 +61,8 @@ struct CONFIG_T {
uint64_t Dtu_Serial;
uint32_t Dtu_PollInterval;
uint8_t Dtu_PaLevel;
uint32_t Mqtt_PublishInterval;
};
class ConfigurationClass {

View File

@ -31,6 +31,7 @@
#define MQTT_LWT_TOPIC "dtu/status"
#define MQTT_LWT_ONLINE "online"
#define MQTT_LWT_OFFLINE "offline"
#define MQTT_PUBLISH_INTERVAL 5
#define DTU_SERIAL 99978563412
#define DTU_POLL_INTERVAL 5

View File

@ -32,6 +32,7 @@ void ConfigurationClass::init()
strlcpy(config.Mqtt_LwtTopic, MQTT_LWT_TOPIC, sizeof(config.Mqtt_LwtTopic));
strlcpy(config.Mqtt_LwtValue_Online, MQTT_LWT_ONLINE, sizeof(config.Mqtt_LwtValue_Online));
strlcpy(config.Mqtt_LwtValue_Offline, MQTT_LWT_OFFLINE, sizeof(config.Mqtt_LwtValue_Offline));
config.Mqtt_PublishInterval = MQTT_PUBLISH_INTERVAL;
for (uint8_t i = 0; i < INV_MAX_COUNT; i++) {
config.Inverter[i].Serial = 0;
@ -112,6 +113,10 @@ void ConfigurationClass::migrate()
config.Dtu_PaLevel = DTU_PA_LEVEL;
}
if (config.Cfg_Version < 0x00011000) {
config.Mqtt_PublishInterval = MQTT_PUBLISH_INTERVAL;
}
config.Cfg_Version = CONFIG_VERSION;
write();
}

View File

@ -15,7 +15,7 @@ void MqttPublishingClass::loop()
CONFIG_T& config = Configuration.get();
if (millis() - _lastPublish > (config.Dtu_PollInterval * 1000)) {
if (millis() - _lastPublish > (config.Mqtt_PublishInterval * 1000)) {
MqttSettings.publish("dtu/uptime", String(millis() / 1000));
MqttSettings.publish("dtu/ip", WiFi.localIP().toString());

View File

@ -34,6 +34,7 @@ void WebApiMqttClass::onMqttStatus(AsyncWebServerRequest* request)
root[F("mqtt_connected")] = MqttSettings.getConnected();
root[F("mqtt_retain")] = config.Mqtt_Retain;
root[F("mqtt_lwt_topic")] = String(config.Mqtt_Topic) + config.Mqtt_LwtTopic;
root[F("mqtt_publish_interval")] = config.Mqtt_PublishInterval;
response->setLength();
request->send(response);
@ -55,6 +56,7 @@ void WebApiMqttClass::onMqttAdminGet(AsyncWebServerRequest* request)
root[F("mqtt_lwt_topic")] = config.Mqtt_LwtTopic;
root[F("mqtt_lwt_online")] = config.Mqtt_LwtValue_Online;
root[F("mqtt_lwt_offline")] = config.Mqtt_LwtValue_Offline;
root[F("mqtt_publish_interval")] = config.Mqtt_PublishInterval;
response->setLength();
request->send(response);
@ -92,7 +94,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
return;
}
if (!(root.containsKey("mqtt_enabled") && root.containsKey("mqtt_hostname") && root.containsKey("mqtt_port") && root.containsKey("mqtt_username") && root.containsKey("mqtt_password") && root.containsKey("mqtt_topic") && root.containsKey("mqtt_retain") && root.containsKey("mqtt_lwt_topic") && root.containsKey("mqtt_lwt_online") && root.containsKey("mqtt_lwt_offline"))) {
if (!(root.containsKey("mqtt_enabled") && root.containsKey("mqtt_hostname") && root.containsKey("mqtt_port") && root.containsKey("mqtt_username") && root.containsKey("mqtt_password") && root.containsKey("mqtt_topic") && root.containsKey("mqtt_retain") && root.containsKey("mqtt_lwt_topic") && root.containsKey("mqtt_lwt_online") && root.containsKey("mqtt_lwt_offline") && root.containsKey("mqtt_publish_interval"))) {
retMsg[F("message")] = F("Values are missing!");
response->setLength();
request->send(response);
@ -153,6 +155,13 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
request->send(response);
return;
}
if (root[F("mqtt_publish_interval")].as<uint32_t>() < 5 || root[F("mqtt_publish_interval")].as<uint32_t>() > 65535) {
retMsg[F("message")] = F("Publish interval must be a number between 5 and 65535!");
response->setLength();
request->send(response);
return;
}
}
CONFIG_T& config = Configuration.get();
@ -166,6 +175,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
strcpy(config.Mqtt_LwtTopic, root[F("mqtt_lwt_topic")].as<String>().c_str());
strcpy(config.Mqtt_LwtValue_Online, root[F("mqtt_lwt_online")].as<String>().c_str());
strcpy(config.Mqtt_LwtValue_Offline, root[F("mqtt_lwt_offline")].as<String>().c_str());
config.Mqtt_PublishInterval = root[F("mqtt_publish_interval")].as<uint32_t>();
Configuration.write();
retMsg[F("type")] = F("success");