Skip to content

Commit

Permalink
Fixed WDT resets for mqtt discovery
Browse files Browse the repository at this point in the history
- Fixed Watchdog resets on registering HA discovery topics. Need to put all messages into publish queue. For that several loops needed for the boiler,
- increade MQTT queue to 25
- added gas meter reading in boiler list
- back to custom MMU settings
  • Loading branch information
harrymayr committed Dec 18, 2022
1 parent bb268ba commit b2e554c
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 268 deletions.
10 changes: 5 additions & 5 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ board_build.f_cpu = 160000000L ; 160MHz
; eagle.flash.4m2m.ld = 1019 KB sketch, 2024 KB SPIFFS. 4KB EEPROM, 4KB RFCAL, 12KB WIFI stack, 1028 KB OTA & buffer
; board_build.ldscript = eagle.flash.4m2m.ld
build_flags = ${common.build_flags} ${common.debug_flags}
; -D PIO_FRAMEWORK_ARDUINO_MMU_CUSTOM
; -D MMU_IRAM_SIZE=0xC000
; -D MMU_ICACHE_SIZE=0x4000
; -D MMU_IRAM_HEAP
-D FLASH_MAP_SUPPORT=1
;-D PIO_FRAMEWORK_ARDUINO_MMU_CUSTOM
;-D MMU_IRAM_SIZE=0xC000
;-D MMU_ICACHE_SIZE=0x4000
;-D MMU_IRAM_HEAP
-D FLASH_MAP_SUPPORT=1
lib_ignore =
AsyncTCP
491 changes: 254 additions & 237 deletions src/devices/boiler.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/devices/boiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class Boiler : public EMSdevice {
bool export_values_info(JsonObject & doc, const bool textformat = false);

bool changed_ = false;
bool mqtt_ha_config_ = false; // HA MQTT Discovery
bool mqtt_ha_config_ww_ = false; // HA MQTT Discovery
uint8_t mqtt_ha_config_ = 0; // HA MQTT Discovery
uint8_t mqtt_ha_config_ww_ = 0; // HA MQTT Discovery

static constexpr uint8_t EMS_TYPE_UBAParameterWW = 0x33;
static constexpr uint8_t EMS_TYPE_UBAFunctionTest = 0x1D;
Expand Down
4 changes: 2 additions & 2 deletions src/devices/thermostat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Thermostat::Thermostat(uint8_t device_type, uint8_t device_id, uint8_t product_i

// prepare data for Web UI
void Thermostat::device_info_web(JsonArray & root, uint8_t & part) {
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_LARGE);
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_LARGE_DYN);
//StaticJsonDocument<EMSESP_MAX_JSON_SIZE_LARGE> doc;
JsonObject json = doc.to<JsonObject>();
if (part == 0) {
Expand Down Expand Up @@ -334,7 +334,7 @@ void Thermostat::publish_values(JsonObject & json, bool force) {
}
}

DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_LARGE);
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_LARGE_DYN);
//StaticJsonDocument<EMSESP_MAX_JSON_SIZE_LARGE> doc;
JsonObject json_data = doc.to<JsonObject>();

Expand Down
4 changes: 2 additions & 2 deletions src/emsesp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ void EMSESP::publish_all_loop() {
// special case for Mixer units, since we want to bundle all devices together into one payload
void EMSESP::publish_device_values(uint8_t device_type, bool force) {
if (device_type == EMSdevice::DeviceType::MIXER && Mqtt::mqtt_format() != Mqtt::Format::SINGLE) {
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_LARGE);
// StaticJsonDocument<EMSESP_MAX_JSON_SIZE_LARGE> doc;
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_LARGE_DYN);
//StaticJsonDocument<EMSESP_MAX_JSON_SIZE_LARGE> doc;
JsonObject json = doc.to<JsonObject>();
for (const auto & emsdevice : emsdevices) {
if (emsdevice && (emsdevice->device_type() == device_type)) {
Expand Down
3 changes: 2 additions & 1 deletion src/emsesp.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
#define EMSESP_MAX_JSON_SIZE_LARGE 1924 // for large json docs from ems devices, like boiler or thermostat data, using StaticJsonDocument
#define EMSESP_MAX_JSON_SIZE_MEDIUM_DYN 1024 // for large json docs, using DynamicJsonDocument
#define EMSESP_MAX_JSON_SIZE_LARGE_DYN 2048 // for very large json docs, using DynamicJsonDocument
#define EMSESP_MAX_JSON_SIZE_MAX_DYN 4096 // for very very large json docs, using DynamicJsonDocument
#define EMSESP_MAX_JSON_SIZE_MAX_DYN 3072 // for very very large json docs, using DynamicJsonDocument
#define EMSESP_MAX_JSON_SIZE_SMAX_DYN 4096 // for very super large json docs, using DynamicJsonDocument
/* #define EMSESP_MAX_JSON_SIZE_HA_CONFIG min(384,ESP.getMaxFreeBlockSize() - 512) // for small HA config payloads, using StaticJsonDocument
#define EMSESP_MAX_JSON_SIZE_SMALL min(256,ESP.getMaxFreeBlockSize() - 512) // for smaller json docs, using StaticJsonDocument
#define EMSESP_MAX_JSON_SIZE_MEDIUM min(768,ESP.getMaxFreeBlockSize() - 512) // for medium json docs from ems devices, using StaticJsonDocument
Expand Down
1 change: 1 addition & 0 deletions src/locale_EN.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ MAKE_PSTR(min_boiler_wh_fmt, "Boiler minimum power = %dWh")
MAKE_PSTR(max_boiler_wh_fmt, "Boiler maximum power = %dWh")
MAKE_PSTR(gas_meter_reading_fmt, "Gas meter = %dm³")
MAKE_PSTR(conv_factor_fmt, "Conversion factor m³<-> Wh = %d")
MAKE_PSTR(gas_meter_reading, "Gas meter reading")

// status
MAKE_PSTR(wifirssi, "WiFi RSSI")
Expand Down
16 changes: 9 additions & 7 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,14 @@ void Mqtt::publish_ha(const std::string & topic, const JsonObject & payload) {
#if defined(EMSESP_STANDALONE)
LOG_DEBUG(F("Publishing HA topic=%s, payload=%s"), topic.c_str(), payload_text.c_str());
#else
LOG_DEBUG(F("Publishing HA topic %s"), topic.c_str());
LOG_DEBUG(F("Publishing HA topic %s size %d"), topic.c_str(), payload_text.length());
#endif

#if defined(ESP32)
LOG_DEBUG(F("%d queued messages"), mqtt_messages_.size());
queue_publish_message(topic, payload_text, true); // with retain true
/* #if defined(ESP32)
bool queued = true; // queue MQTT publish
#else
bool queued = false; // publish immediately
bool queued = true; // queue MQTT publish if enough space on should not forced
#endif
// if MQTT is not connected, then we have to queue the msg until the MQTT is online
Expand All @@ -682,12 +683,13 @@ void Mqtt::publish_ha(const std::string & topic, const JsonObject & payload) {
}
// send immediately and then wait a while
if (!mqttClient_->publish(topic.c_str(), 0, true, payload_text.c_str())) {
if (!mqttClient_->publish(topic.c_str(), 0, true, payload_text.c_str(), payload_text.size())) {
LOG_ERROR(F("Failed to publish topic %s"), topic.c_str());
}

EspClass::wdtFeed();
delay(MQTT_HA_PUBLISH_DELAY); // enough time to send the short message out
}
yield();
*/}

// take top from queue and perform the publish or subscribe action
// assumes there is an MQTT connection
Expand Down
2 changes: 1 addition & 1 deletion src/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class Mqtt {
#elif defined(ESP32)
static constexpr size_t MAX_MQTT_MESSAGES = 100; // size of queue
#else
static constexpr size_t MAX_MQTT_MESSAGES = 20; // size of queue
static constexpr size_t MAX_MQTT_MESSAGES = 25; // size of queue
#endif

static constexpr uint32_t MQTT_PUBLISH_WAIT = 100; // delay between sending publishes, to account for large payloads
Expand Down
21 changes: 13 additions & 8 deletions src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,17 @@ void System::loop() {
if (syslog_enabled_) {
syslog_.loop();
}
yield();
myPButton_.check(); // check button press

yield();
led_monitor(); // check status and report back using the LED
yield();
system_check(); // check system health
yield();
if (analog_enabled_) {
measure_analog();
}

yield();
// send out heartbeat
uint32_t currentMillis = uuid::get_uptime();
if (!last_heartbeat_ || (currentMillis - last_heartbeat_ > SYSTEM_HEARTBEAT_INTERVAL)) {
Expand All @@ -349,7 +352,8 @@ void System::loop() {
#if defined(EMSESP_DEBUG)
LOG_INFO(F("last burner power %d, current burner power %d"), last_burnPower_, EMSESP::current_burn_pow());
#endif
if (!last_burnPower_) {
yield();
if (gasReading_ == 0) {
EMSESP::webSettingsService.read([&](WebSettings & settings) {
convFactor_ = settings.conv_factor;
gasReading_ = settings.gas_meter_reading * convFactor_*4;
Expand All @@ -360,6 +364,7 @@ void System::loop() {
else {
gasReading_ = gasReading_+ (last_burnPower_ + EMSESP::current_burn_pow())*maxWhPower_ / 3000.0;
}
yield();
last_burnPower_ = EMSESP::current_burn_pow();
if (gasReading_ - storedGasReading_ > convFactor_*4) {
EMSESP::webSettingsService.update(
Expand All @@ -369,6 +374,7 @@ void System::loop() {
},
"local");
storedGasReading_ = gasReading_;
yield();
EMSESP::webSettingsService.save(); // local settings
}

Expand Down Expand Up @@ -436,8 +442,8 @@ void System::send_heartbeat() {
return;
}
}
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_MEDIUM);
//StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_MEDIUM);
//StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;

uint8_t ems_status = EMSESP::bus_status();
if (ems_status == EMSESP::BUS_STATUS_TX_ERRORS) {
Expand Down Expand Up @@ -469,8 +475,7 @@ void System::send_heartbeat() {
if (analog_enabled_) {
doc["adc"] = analog_;
}
doc["gasReading"] = (float)gasReading_/convFactor_/4;

doc["gasReading"] = (float)(gasReading_/convFactor_/4);
Mqtt::publish(F("heartbeat"), doc.as<JsonObject>()); // send to MQTT with retain off. This will add to MQTT queue.
}

Expand Down Expand Up @@ -883,7 +888,7 @@ bool System::check_upgrade() {
bool failed = false;
File file;
JsonObject network, general, mqtt, custom_settings;
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_LARGE);
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_LARGE_DYN);
//StaticJsonDocument<EMSESP_MAX_JSON_SIZE_LARGE> doc;

// open the system settings:
Expand Down
4 changes: 2 additions & 2 deletions src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class System {

bool check_upgrade();
void send_heartbeat();
static float gasReading_; // calculated gas meter reading
static uint16_t convFactor_; // convertion factor m³<->Wh

private:
static uuid::log::Logger logger_;
Expand Down Expand Up @@ -124,11 +126,9 @@ class System {
static uint8_t led_gpio_;
static bool syslog_enabled_;
static bool analog_enabled_;
static float gasReading_; // calculated gas meter reading
static uint32_t maxWhPower_; // max boiler power in Wh
static uint32_t storedGasReading_; // gas meter reading stored in SPIFFS
uint32_t last_burnPower_ = 0;
static uint16_t convFactor_; // convertion factor m³<->Wh
bool mqtt_ha_status_config = false; // HA MQTT Discovery
};

Expand Down
1 change: 1 addition & 0 deletions src/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
shell.invoke_command("show");

DynamicJsonDocument doc(500);
// StaticJsonDocument<500> doc;
JsonObject root = doc.to<JsonObject>();
EMSESP::device_info_web(2, root); // show thermostat. use 1 for boiler
serializeJsonPretty(doc, shell);
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#define EMSESP_APP_VERSION "2.3.1b1"
#define EMSESP_APP_VERSION "2.3.1b3"
#define EMSESP_PLATFORM "ESP8266"

0 comments on commit b2e554c

Please sign in to comment.