Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize VE.Direct to SolarCharger #1442

Draft
wants to merge 13 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#define CONFIG_FILENAME "/config.json"
#define CONFIG_VERSION 0x00011d00 // 0.1.29 // make sure to clean all after change
#define CONFIG_VERSION_ONBATTERY 2
#define CONFIG_VERSION_ONBATTERY 3

#define WIFI_MAX_SSID_STRLEN 32
#define WIFI_MAX_PASSWORD_STRLEN 64
Expand Down Expand Up @@ -196,6 +196,16 @@ struct BATTERY_CONFIG_T {
};
using BatteryConfig = struct BATTERY_CONFIG_T;

enum SolarChargerProviderType { VEDIRECT = 0 };

struct SOLAR_CHARGER_CONFIG_T {
bool Enabled;
bool VerboseLogging;
SolarChargerProviderType Provider;
bool PublishUpdatesOnly;
};
using SolarChargerConfig = struct SOLAR_CHARGER_CONFIG_T;

struct CONFIG_T {
struct {
uint32_t Version;
Expand Down Expand Up @@ -307,11 +317,7 @@ struct CONFIG_T {
uint8_t Brightness;
} Led_Single[PINMAPPING_LED_COUNT];

struct {
bool Enabled;
bool VerboseLogging;
bool UpdatesOnly;
} Vedirect;
SolarChargerConfig SolarCharger;

struct PowerMeterConfig {
bool Enabled;
Expand Down Expand Up @@ -373,6 +379,7 @@ class ConfigurationClass {
void deleteInverterById(const uint8_t id);

static void serializeHttpRequestConfig(HttpRequestConfig const& source, JsonObject& target);
static void serializeSolarChargerConfig(SolarChargerConfig const& source, JsonObject& target);
static void serializePowerMeterMqttConfig(PowerMeterMqttConfig const& source, JsonObject& target);
static void serializePowerMeterSerialSdmConfig(PowerMeterSerialSdmConfig const& source, JsonObject& target);
static void serializePowerMeterHttpJsonConfig(PowerMeterHttpJsonConfig const& source, JsonObject& target);
Expand All @@ -381,6 +388,7 @@ class ConfigurationClass {
static void serializePowerLimiterConfig(PowerLimiterConfig const& source, JsonObject& target);

static void deserializeHttpRequestConfig(JsonObject const& source_http_config, HttpRequestConfig& target);
static void deserializeSolarChargerConfig(JsonObject const& source, SolarChargerConfig& target);
static void deserializePowerMeterMqttConfig(JsonObject const& source, PowerMeterMqttConfig& target);
static void deserializePowerMeterSerialSdmConfig(JsonObject const& source, PowerMeterSerialSdmConfig& target);
static void deserializePowerMeterHttpJsonConfig(JsonObject const& source, PowerMeterHttpJsonConfig& target);
Expand Down
8 changes: 0 additions & 8 deletions include/MqttHandleVedirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
#include <map>
#include <TaskSchedulerDeclarations.h>

#ifndef VICTRON_PIN_RX
#define VICTRON_PIN_RX 22
#endif

#ifndef VICTRON_PIN_TX
#define VICTRON_PIN_TX 21
#endif

class MqttHandleVedirectClass {
public:
void init(Scheduler& scheduler);
Expand Down
2 changes: 1 addition & 1 deletion include/MqttHandleVedirectHass.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
class MqttHandleVedirectHassClass {
public:
void init(Scheduler& scheduler);
void publishConfig();
void forceUpdate();

private:
void loop();
void publishConfig();
void publish(const String& subtopic, const String& payload);
void publishBinarySensor(const char *caption, const char *icon, const char *subTopic,
const char *payload_on, const char *payload_off,
Expand Down
48 changes: 48 additions & 0 deletions include/SolarCharger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <memory>
#include <mutex>
#include <TaskSchedulerDeclarations.h>
#include "SolarChargerProvider.h"
#include "VeDirectMpptController.h"

class SolarChargerClass {
public:
void init(Scheduler&);
void updateSettings();

// TODO(andreasboehm): below methods are taken from VictronMppt to start abstracting
// solar chargers without breaking everything.
size_t controllerAmount();
uint32_t getDataAgeMillis();
uint32_t getDataAgeMillis(size_t idx);

// total output of all MPPT charge controllers in Watts
int32_t getOutputPowerWatts();

// total panel input power of all MPPT charge controllers in Watts
int32_t getPanelPowerWatts();

// sum of total yield of all MPPT charge controllers in kWh
float getYieldTotal();

// sum of today's yield of all MPPT charge controllers in kWh
float getYieldDay();

// minimum of all MPPT charge controllers' output voltages in V
float getOutputVoltage();

std::optional<VeDirectMpptController::data_t> getData(size_t idx = 0);

bool isDataValid();

private:
void loop();

Task _loopTask;
mutable std::mutex _mutex;
std::unique_ptr<SolarChargerProvider> _upProvider = nullptr;
};

extern SolarChargerClass SolarCharger;
36 changes: 36 additions & 0 deletions include/SolarChargerProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include "VeDirectMpptController.h"

class SolarChargerProvider {
public:
// returns true if the provider is ready for use, false otherwise
virtual bool init(bool verboseLogging) = 0;
virtual void deinit() = 0;
virtual void loop() = 0;

// TODO(andreasboehm): below methods are taken from VictronMppt to start abstracting
// solar chargers without breaking everything.
virtual size_t controllerAmount() const = 0;
virtual uint32_t getDataAgeMillis() const = 0;
virtual uint32_t getDataAgeMillis(size_t idx) const = 0;
// total output of all MPPT charge controllers in Watts
virtual int32_t getOutputPowerWatts() const = 0;

// total panel input power of all MPPT charge controllers in Watts
virtual int32_t getPanelPowerWatts() const = 0;

// sum of total yield of all MPPT charge controllers in kWh
virtual float getYieldTotal() const = 0;

// sum of today's yield of all MPPT charge controllers in kWh
virtual float getYieldDay() const = 0;

// minimum of all MPPT charge controllers' output voltages in V
virtual float getOutputVoltage() const = 0;

virtual std::optional<VeDirectMpptController::data_t> getData(size_t idx = 0) const = 0;

virtual bool isDataValid() const = 0;
};
48 changes: 22 additions & 26 deletions include/VictronMppt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,45 @@

#include <mutex>
#include <memory>
#include <TaskSchedulerDeclarations.h>

#include "SolarChargerProvider.h"
#include "VeDirectMpptController.h"
#include "Configuration.h"
#include <TaskSchedulerDeclarations.h>

class VictronMpptClass {
class VictronMppt : public SolarChargerProvider {
public:
VictronMpptClass() = default;
~VictronMpptClass() = default;
AndreasBoehm marked this conversation as resolved.
Show resolved Hide resolved
VictronMppt() = default;
~VictronMppt() = default;

void init(Scheduler& scheduler);
void updateSettings();
bool init(bool verboseLogging) final;
void deinit() final;
void loop() final;

bool isDataValid() const;
bool isDataValid(size_t idx) const;
bool isDataValid() const final;

// returns the data age of all controllers,
// i.e, the youngest data's age is returned.
uint32_t getDataAgeMillis() const;
uint32_t getDataAgeMillis(size_t idx) const;
uint32_t getDataAgeMillis() const final;
uint32_t getDataAgeMillis(size_t idx) const final;

size_t controllerAmount() const { return _controllers.size(); }
std::optional<VeDirectMpptController::data_t> getData(size_t idx = 0) const;
size_t controllerAmount() const final { return _controllers.size(); }
std::optional<VeDirectMpptController::data_t> getData(size_t idx = 0) const final;

// total output of all MPPT charge controllers in Watts
int32_t getPowerOutputWatts() const;
int32_t getOutputPowerWatts() const final;

// total panel input power of all MPPT charge controllers in Watts
int32_t getPanelPowerWatts() const;
int32_t getPanelPowerWatts() const final;

// sum of total yield of all MPPT charge controllers in kWh
float getYieldTotal() const;
float getYieldTotal() const final;

// sum of today's yield of all MPPT charge controllers in kWh
float getYieldDay() const;
float getYieldDay() const final;

// minimum of all MPPT charge controllers' output voltages in V
float getOutputVoltage() const;
float getOutputVoltage() const final;

// returns the state of operation from the first available controller
std::optional<uint8_t> getStateOfOperation() const;
Expand All @@ -54,13 +55,10 @@ class VictronMpptClass {
std::optional<float> getVoltage(MPPTVoltage kindOf) const;

private:
void loop();
VictronMpptClass(VictronMpptClass const& other) = delete;
VictronMpptClass(VictronMpptClass&& other) = delete;
VictronMpptClass& operator=(VictronMpptClass const& other) = delete;
VictronMpptClass& operator=(VictronMpptClass&& other) = delete;
AndreasBoehm marked this conversation as resolved.
Show resolved Hide resolved

Task _loopTask;
VictronMppt(VictronMppt const& other) = delete;
VictronMppt(VictronMppt&& other) = delete;
VictronMppt& operator=(VictronMppt const& other) = delete;
VictronMppt& operator=(VictronMppt&& other) = delete;

mutable std::mutex _mutex;
using controller_t = std::unique_ptr<VeDirectMpptController>;
Expand All @@ -70,5 +68,3 @@ class VictronMpptClass {
bool initController(int8_t rx, int8_t tx, bool logging,
uint8_t instance);
};

extern VictronMpptClass VictronMppt;
8 changes: 4 additions & 4 deletions include/WebApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include "WebApi_ws_console.h"
#include "WebApi_ws_live.h"
#include <AsyncJson.h>
#include "WebApi_ws_vedirect_live.h"
#include "WebApi_vedirect.h"
#include "WebApi_ws_solarcharger_live.h"
#include "WebApi_solarcharger.h"
#include "WebApi_ws_Huawei.h"
#include "WebApi_Huawei.h"
#include "WebApi_ws_battery.h"
Expand Down Expand Up @@ -79,8 +79,8 @@ class WebApiClass {
WebApiWebappClass _webApiWebapp;
WebApiWsConsoleClass _webApiWsConsole;
WebApiWsLiveClass _webApiWsLive;
WebApiWsVedirectLiveClass _webApiWsVedirectLive;
WebApiVedirectClass _webApiVedirect;
WebApiWsSolarChargerLiveClass _webApiWsSolarChargerLive;
WebApiSolarChargerlass _webApiSolarCharger;
WebApiHuaweiClass _webApiHuaweiClass;
WebApiWsHuaweiLiveClass _webApiWsHuaweiLive;
WebApiWsBatteryLiveClass _webApiWsBatteryLive;
Expand Down
10 changes: 5 additions & 5 deletions include/WebApi_vedirect.h → include/WebApi_solarcharger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#include <TaskSchedulerDeclarations.h>


class WebApiVedirectClass {
class WebApiSolarChargerlass {
public:
void init(AsyncWebServer& server, Scheduler& scheduler);

private:
void onVedirectStatus(AsyncWebServerRequest* request);
void onVedirectAdminGet(AsyncWebServerRequest* request);
void onVedirectAdminPost(AsyncWebServerRequest* request);
void onStatus(AsyncWebServerRequest* request);
void onAdminGet(AsyncWebServerRequest* request);
void onAdminPost(AsyncWebServerRequest* request);

AsyncWebServer* _server;
};
};
2 changes: 1 addition & 1 deletion include/WebApi_ws_live.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class WebApiWsLiveClass {
AuthenticationMiddleware _simpleDigestAuth;

uint32_t _lastPublishOnBatteryFull = 0;
uint32_t _lastPublishVictron = 0;
uint32_t _lastPublishSolarCharger = 0;
uint32_t _lastPublishHuawei = 0;
uint32_t _lastPublishBattery = 0;
uint32_t _lastPublishPowerMeter = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include <VeDirectMpptController.h>
#include <mutex>

class WebApiWsVedirectLiveClass {
class WebApiWsSolarChargerLiveClass {
public:
WebApiWsVedirectLiveClass();
WebApiWsSolarChargerLiveClass();
void init(AsyncWebServer& server, Scheduler& scheduler);
void reload();

Expand Down
6 changes: 3 additions & 3 deletions include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@
#define LANG_PACK_SUFFIX ".lang.json"

// values specific to downstream project OpenDTU-OnBattery start here:
#define VEDIRECT_ENABLED false
#define VEDIRECT_VERBOSE_LOGGING false
#define VEDIRECT_UPDATESONLY true
#define SOLAR_CHARGER_ENABLED false
#define SOLAR_CHARGER_VERBOSE_LOGGING false
#define SOLAR_CHARGER_PUBLISH_UPDATES_ONLY true

#define POWERMETER_ENABLED false
#define POWERMETER_POLLING_INTERVAL 10
Expand Down
Loading