move NVS initisalisation higher, add check for poll_id == bus ID

This commit is contained in:
proddy
2026-03-29 15:43:03 +02:00
parent 434bf483fd
commit 69a129d80e
3 changed files with 31 additions and 15 deletions

View File

@@ -1600,9 +1600,11 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
wait_km_ = true;
connect_time = uuid::get_uptime_sec();
}
// this could also be by coincidence, so we should add a counter to the EMSbus class to check if the poll_id is the same as the EMS_BUS_ID for a certain number of times
if (poll_id == EMSbus::ems_bus_id()) {
// TODO this could also be by coincidence, so we should add a counter to the EMSbus class to check if the poll_id is the same as the EMS_BUS_ID for a certain number of times
EMSbus::last_bus_activity(uuid::get_uptime()); // set the flag indication the EMS bus is active
EMSbus::poll_matched(uuid::get_uptime());
} else {
EMSbus::poll_match_reset();
}
if (wait_km_) {
if (poll_id != 0x48 && (uuid::get_uptime_sec() - connect_time) < EMS_WAIT_KM_TIMEOUT) {
@@ -1709,6 +1711,11 @@ void EMSESP::start() {
bool factory_settings = false;
#endif
// start NVS storage
if (!nvs_.begin("ems-esp", false, "nvs1")) { // try bigger nvs partition on 16M flash first
nvs_.begin("ems-esp", false, "nvs"); // fallback to small nvs
}
// set valid GPIOs list based on ESP32 chip/platform type
system_.set_valid_system_gpios();
@@ -1757,7 +1764,7 @@ void EMSESP::start() {
LOG_INFO("Last system reset reason Core0: %s, Core1: %s", system_.reset_reason(0).c_str(), system_.reset_reason(1).c_str());
#endif
// see if we're restoring a settings file
// see if we're restoring a settings file
#ifndef EMSESP_STANDALONE
if (system_.check_restore()) {
LOG_WARNING("EMS-ESP will restart to apply new settings. Please wait.");
@@ -1765,11 +1772,7 @@ void EMSESP::start() {
};
#endif
if (!nvs_.begin("ems-esp", false, "nvs1")) { // try bigger nvs partition on 16M flash first
nvs_.begin("ems-esp", false, "nvs"); // fallback to small nvs
}
LOG_DEBUG("Fuse device information: %s", system_.getBBQKeesGatewayDetails().isEmpty() ? "not set" : system_.getBBQKeesGatewayDetails().c_str());
LOG_DEBUG("eFuse device information: %s", system_.getBBQKeesGatewayDetails().isEmpty() ? "not set" : system_.getBBQKeesGatewayDetails().c_str());
webSettingsService.begin(); // load EMS-ESP Application settings
@@ -1848,7 +1851,7 @@ void EMSESP::loop() {
return; // LED flashing is active, skip the rest of the loop
}
esp32React.loop(); // web services like network, AP, MQTT
esp32React.loop(); // core services: Network, AP, MQTT and NTP
webLogService.loop(); // log in Web UI
// run the loop, unless we're in the middle of an OTA upload

View File

@@ -39,6 +39,7 @@ const uint8_t ems_crc_table[] = {0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E,
uint32_t EMSbus::last_bus_activity_ = 0; // timestamp of last time a valid Rx came in
uint32_t EMSbus::bus_uptime_start_ = 0; // timestamp of when the bus was started
bool EMSbus::bus_connected_ = false; // start assuming the bus hasn't been connected
uint8_t EMSbus::poll_match_count_ = 0; // consecutive poll ID matches
uint8_t EMSbus::ems_mask_ = EMS_MASK_UNSET; // unset so its triggered when booting, the its 0x00=buderus, 0x80=junker/ht3
uint8_t EMSbus::ems_bus_id_ = EMSESP_DEFAULT_EMS_BUS_ID;
uint8_t EMSbus::tx_mode_ = EMSESP_DEFAULT_TX_MODE;

View File

@@ -159,10 +159,11 @@ class EMSbus {
public:
static uuid::log::Logger logger_;
static constexpr uint8_t EMS_MASK_UNSET = 0xFF; // EMS bus type (budrus/junkers) hasn't been detected yet
static constexpr uint8_t EMS_MASK_HT3 = 0x80; // EMS bus type Junkers/HT3
static constexpr uint8_t EMS_MASK_BUDERUS = 0xFF; // EMS bus type Buderus
static constexpr uint8_t EMS_TX_ERROR_LIMIT = 10; // % limit of failed Tx read/write attempts before showing a warning
static constexpr uint8_t EMS_MASK_UNSET = 0xFF; // EMS bus type (budrus/junkers) hasn't been detected yet
static constexpr uint8_t EMS_MASK_HT3 = 0x80; // EMS bus type Junkers/HT3
static constexpr uint8_t EMS_MASK_BUDERUS = 0xFF; // EMS bus type Buderus
static constexpr uint8_t EMS_TX_ERROR_LIMIT = 10; // % limit of failed Tx read/write attempts before showing a warning
static constexpr uint8_t EMS_POLL_MATCH_LIMIT = 3; // consecutive poll matches needed before declaring bus connected
static bool is_ht3() {
return (ems_mask_ == EMS_MASK_HT3);
@@ -204,17 +205,27 @@ class EMSbus {
#endif
}
// called on each poll match; requires EMS_POLL_MATCH_LIMIT consecutive matches before declaring bus connected
static void poll_matched(uint32_t timestamp) {
if (++poll_match_count_ < EMS_POLL_MATCH_LIMIT) {
return;
}
last_bus_activity(timestamp);
}
// sets the flag for EMS bus connected
static void last_bus_activity(uint32_t timestamp) {
// record the first time we connected to the BUS, as this will be our uptime
if (!last_bus_activity_) {
bus_uptime_start_ = timestamp;
}
last_bus_activity_ = timestamp;
bus_connected_ = true;
}
static void poll_match_reset() {
poll_match_count_ = 0;
}
// return bus uptime in seconds
static uint32_t bus_uptime() {
if (!bus_uptime_start_) {
@@ -238,6 +249,7 @@ class EMSbus {
static uint32_t last_bus_activity_; // timestamp of last time a valid Rx came in
static uint32_t bus_uptime_start_; // timestamp of first time we connected to the bus
static bool bus_connected_; // start assuming the bus hasn't been connected
static uint8_t poll_match_count_; // consecutive poll ID matches seen so far
static uint8_t ems_mask_; // unset=0xFF, buderus=0x00, junkers/ht3=0x80
static uint8_t ems_bus_id_; // the bus id, which configurable and stored in settings
static uint8_t tx_mode_; // local copy of the tx mode