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

Enable Shaper with Divert mode active #530

Merged
merged 3 commits into from
Jan 29, 2023
Merged
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
16 changes: 14 additions & 2 deletions src/current_shaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void CurrentShaperTask::setup() {
}

unsigned long CurrentShaperTask::loop(MicroTasks::WakeReason reason) {
if (_enabled && !_evse->clientHasClaim(EvseClient_OpenEVSE_Divert)) {
if (_enabled) {
EvseProperties props;
if (_changed) {
props.setMaxCurrent(_max_cur);
Expand Down Expand Up @@ -116,7 +116,19 @@ void CurrentShaperTask::setState(bool state) {

void CurrentShaperTask::shapeCurrent() {
_updated = true;
_max_cur = round(((_max_pwr - _live_pwr) / evse.getVoltage()) + (evse.getAmps()));
// adding self produced energy to total
int max_pwr = _max_pwr;
if (config_divert_enabled()) {
if (mqtt_solar != "") {
max_pwr += solar;
}
else if (mqtt_grid_ie != "" && (grid_ie <= 0)) {
max_pwr -= grid_ie;
}
}
_max_cur = round(((max_pwr - _live_pwr) / evse.getVoltage()) + (evse.getAmps()));


_changed = true;
}

Expand Down
5 changes: 2 additions & 3 deletions src/current_shaper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "http_update.h"
#include "input.h"
#include "event.h"
#include "divert.h"

class CurrentShaperTask: public MicroTasks::Task
{
Expand All @@ -34,14 +35,12 @@ class CurrentShaperTask: public MicroTasks::Task
protected:
void setup();
unsigned long loop(MicroTasks::WakeReason reason);
void shapeCurrent();


public:
CurrentShaperTask();
~CurrentShaperTask();
void begin(EvseManager &evse);

void shapeCurrent();
void setMaxPwr(int max_pwr);
void setLivePwr(int live_pwr);
void setState(bool state);
Expand Down
18 changes: 17 additions & 1 deletion src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static bool connecting = false;
static bool mqttRetained = false;
uint8_t claimsVersion = 0;
uint8_t overrideVersion = 0;
uint8_t scheduleVersion = 0;

String lastWill = "";

Expand Down Expand Up @@ -64,12 +65,21 @@ void mqttmsg_callback(MongooseString topic, MongooseString payload) {
solar = payload_str.toInt();
DBUGF("solar:%dW", solar);
divert.update_state();
//recalculate shaper
if (shaper.getState()) {
shaper.shapeCurrent();
}

}
else if (topic_string == mqtt_grid_ie)
{
grid_ie = payload_str.toInt();
DBUGF("grid:%dW", grid_ie);
divert.update_state();
//recalculate shaper
if (shaper.getState()) {
shaper.shapeCurrent();
}
}
else if (topic_string == mqtt_live_pwr)
{
Expand Down Expand Up @@ -521,10 +531,16 @@ mqtt_loop() {
}

if (overrideVersion != manual.getVersion()) {
mqtt_publish_override;
mqtt_publish_override();
DBUGF("Override has changed, publishing to MQTT");
overrideVersion = manual.getVersion();
}

if (scheduleVersion != scheduler.getVersion()) {
mqtt_publish_schedule();
DBUGF("Schedule has changed, publishing to MQTT");
scheduleVersion = scheduler.getVersion();
}
}
Profile_End(mqtt_loop, 5);
}
Expand Down
9 changes: 9 additions & 0 deletions src/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ void buildStatus(DynamicJsonDocument &doc) {
doc["charge_rate"] = divert.getChargeRate();
doc["divert_update"] = (millis() - divert.getLastUpdate()) / 1000;
doc["divert_active"] = divert.isActive();
doc["divertmode"] = (uint8_t)divert.getMode();

doc["shaper"] = shaper.getState()?1:0;
doc["shaper_live_pwr"] = shaper.getLivePwr();
Expand Down Expand Up @@ -709,12 +710,20 @@ void handleStatusPost(MongooseHttpServerRequest *request, MongooseHttpServerResp
solar = doc["solar"];
DBUGF("solar:%dW", solar);
divert.update_state();
// recalculate shaper
if (shaper.getState()) {
shaper.shapeCurrent();
}
send_event = false; // Divert sends the event so no need to send here
}
else if(doc.containsKey("grid_ie")) {
grid_ie = doc["grid_ie"];
DBUGF("grid:%dW", grid_ie);
divert.update_state();
// recalculate shaper
if (shaper.getState()) {
shaper.shapeCurrent();
}
send_event = false; // Divert sends the event so no need to send here
}
if(doc.containsKey("battery_level")) {
Expand Down