diff --git a/lib/Hoymiles/src/Hoymiles.cpp b/lib/Hoymiles/src/Hoymiles.cpp index 4745e19a..aef62059 100644 --- a/lib/Hoymiles/src/Hoymiles.cpp +++ b/lib/Hoymiles/src/Hoymiles.cpp @@ -49,7 +49,7 @@ void HoymilesClass::loop() std::shared_ptr HoymilesClass::addInverter(const char* name, uint64_t serial) { - std::shared_ptr i; + std::shared_ptr i = nullptr; if (HM_4CH::isValidSerial(serial)) { i = std::make_shared(serial); } else if (HM_2CH::isValidSerial(serial)) { diff --git a/src/WebApi_inverter.cpp b/src/WebApi_inverter.cpp index 473425e8..e82a5c5b 100644 --- a/src/WebApi_inverter.cpp +++ b/src/WebApi_inverter.cpp @@ -141,8 +141,10 @@ void WebApiInverterClass::onInverterAdd(AsyncWebServerRequest* request) auto inv = Hoymiles.addInverter(inverter->Name, inverter->Serial); - for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) { - inv->Statistics()->setChannelMaxPower(c, inverter->MaxChannelPower[c]); + if (inv != nullptr) { + for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) { + inv->Statistics()->setChannelMaxPower(c, inverter->MaxChannelPower[c]); + } } MqttHassPublishing.forceUpdate(); @@ -243,14 +245,18 @@ void WebApiInverterClass::onInverterEdit(AsyncWebServerRequest* request) std::shared_ptr inv = Hoymiles.getInverterByPos(root[F("id")].as()); - if (inv->serial() != inverter.Serial) { + if (inv != nullptr && inv->serial() != inverter.Serial) { // Serial was changed Hoymiles.removeInverterByPos(root[F("id")].as()); inv = Hoymiles.addInverter(inverter.Name, inverter.Serial); + } else if (inv == nullptr) { + inv = Hoymiles.addInverter(inverter.Name, inverter.Serial); } - for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) { - inv->Statistics()->setChannelMaxPower(c, inverter.MaxChannelPower[c]); + if (inv != nullptr) { + for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) { + inv->Statistics()->setChannelMaxPower(c, inverter.MaxChannelPower[c]); + } } MqttHassPublishing.forceUpdate(); diff --git a/src/main.cpp b/src/main.cpp index 69542946..eed7a9e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,8 +100,10 @@ void setup() config.Inverter[i].Name, config.Inverter[i].Serial); - for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) { - inv->Statistics()->setChannelMaxPower(c, config.Inverter[i].MaxChannelPower[c]); + if (inv != nullptr) { + for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) { + inv->Statistics()->setChannelMaxPower(c, config.Inverter[i].MaxChannelPower[c]); + } } Serial.println(F(" done")); }