Skip to content

Commit

Permalink
add "State of Operation", "Float voltage" and "Absorption Voltage" to…
Browse files Browse the repository at this point in the history
… solar charger (hoylabs#1576)

* add: get "State of Operation", "Float voltage" and "Absorption Voltage" from the Solar Charger

* getStateOfOperation(), return enum instead of int

* getStateOfOperation() enums limited to actual used states

* remove default declarations from the Stats.h base class
  • Loading branch information
SW-Niko authored Feb 4, 2025
1 parent 4f29817 commit f3fbcf2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/solarcharger/DummyStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class DummyStats : public Stats {
std::optional<uint16_t> getPanelPowerWatts() const final { return std::nullopt; }
std::optional<float> getYieldTotal() const final { return std::nullopt; }
std::optional<float> getYieldDay() const final { return std::nullopt; }
std::optional<StateOfOperation> getStateOfOperation() const final { return std::nullopt; }
std::optional<float> getFloatVoltage() const final { return std::nullopt; }
std::optional<float> getAbsorptionVoltage() const final { return std::nullopt; }
void getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const final {}
void mqttPublish() const final {}
void mqttPublishSensors(const boolean forcePublish) const final {}
Expand Down
10 changes: 10 additions & 0 deletions include/solarcharger/Stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class Stats {
// sum of today's yield of all MPPT charge controllers in Wh
virtual std::optional<float> getYieldDay() const;

// state of operation from the first available controller
enum class StateOfOperation : uint8_t { Off = 0, Bulk = 1, Absorption = 2, Float = 3, Various = 255 };
virtual std::optional<Stats::StateOfOperation> getStateOfOperation() const;

// float voltage from the first available charge controller
virtual std::optional<float> getFloatVoltage() const;

// absorption voltage from the first available charge controller
virtual std::optional<float> getAbsorptionVoltage() const;

// convert stats to JSON for web application live view
virtual void getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const;

Expand Down
3 changes: 3 additions & 0 deletions include/solarcharger/mqtt/Stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ friend class Provider;
std::optional<uint16_t> getPanelPowerWatts() const final { return std::nullopt; }
std::optional<float> getYieldTotal() const final { return std::nullopt; }
std::optional<float> getYieldDay() const final { return std::nullopt; }
std::optional<StateOfOperation> getStateOfOperation() const final { return std::nullopt; }
std::optional<float> getFloatVoltage() const final { return std::nullopt; }
std::optional<float> getAbsorptionVoltage() const final { return std::nullopt; }

void getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const final;

Expand Down
3 changes: 3 additions & 0 deletions include/solarcharger/victron/Stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Stats : public ::SolarChargers::Stats {
std::optional<uint16_t> getPanelPowerWatts() const final;
std::optional<float> getYieldTotal() const final;
std::optional<float> getYieldDay() const final;
std::optional<StateOfOperation> getStateOfOperation() const final;
std::optional<float> getFloatVoltage() const final;
std::optional<float> getAbsorptionVoltage() const final;

void getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const final;
void mqttPublish() const final;
Expand Down
41 changes: 41 additions & 0 deletions src/solarcharger/victron/Stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,47 @@ std::optional<float> Stats::getYieldDay() const
return sum;
}

std::optional<Stats::StateOfOperation> Stats::getStateOfOperation() const
{
for (auto const& entry : _data) {
if (!entry.second) { continue; }
// see victron protocol documentation for CS values
switch (entry.second->currentState_CS) {
case 0: return Stats::StateOfOperation::Off;
case 3: return Stats::StateOfOperation::Bulk;
case 246:
case 4: return Stats::StateOfOperation::Absorption;
case 5: return Stats::StateOfOperation::Float;
default: return Stats::StateOfOperation::Various;
}
}
return std::nullopt;
}

std::optional<float> Stats::getFloatVoltage() const
{
for (auto const& entry : _data) {
if (!entry.second) { continue; }
auto voltage = entry.second->BatteryFloatMilliVolt;
if (voltage.first > 0) { // only return valid and not outdated value
return voltage.second / 1000.0;
}
}
return std::nullopt;
}

std::optional<float> Stats::getAbsorptionVoltage() const
{
for (auto const& entry : _data) {
if (!entry.second) { continue; }
auto voltage = entry.second->BatteryAbsorptionMilliVolt;
if (voltage.first > 0) { // only return valid and not outdated value
return voltage.second / 1000.0;
}
}
return std::nullopt;
}

void Stats::getLiveViewData(JsonVariant& root, const boolean fullUpdate, const uint32_t lastPublish) const
{
::SolarChargers::Stats::getLiveViewData(root, fullUpdate, lastPublish);
Expand Down

0 comments on commit f3fbcf2

Please sign in to comment.