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 / Disable power limiter via MQTT #172

Merged
merged 47 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
7ba2058
Merge branch 'development'
helgeerbe Mar 27, 2023
6f2901b
Merge branch 'development'
helgeerbe Mar 27, 2023
724f1cf
Merge branch 'development'
helgeerbe Apr 6, 2023
e4822f0
Merge branch 'development'
helgeerbe Apr 6, 2023
9521dee
refactoring calcPowerLimit and setPowerLimit
MalteSchm Apr 7, 2023
cb6b984
merging
MalteSchm Apr 7, 2023
f84bdf7
adding missing statement from merge
MalteSchm Apr 7, 2023
734d34b
make calcPowerLimit respect DTU poll interval
MalteSchm Apr 7, 2023
869d8e6
fixing a bug introduced in merge
MalteSchm Apr 7, 2023
e3964f8
typo
MalteSchm Apr 9, 2023
86ecc62
fixing mppt string for SmartSolar 150/48
MalteSchm Apr 9, 2023
28e204f
typo
MalteSchm Apr 9, 2023
a1252c5
Merge branch 'typo_fix'
MalteSchm Apr 9, 2023
406332f
fixed linting issues, pushing to github for test run
MalteSchm Apr 9, 2023
9da5be7
Merge branch 'development'
helgeerbe Apr 11, 2023
4bff31e
adding option to disable power limiter via mqtt
MalteSchm Apr 12, 2023
b6edc11
adding option to disable power limiter via mqtt - adding missing file
MalteSchm Apr 12, 2023
97a8545
merging branches to prepare mqtt enablement
MalteSchm Apr 12, 2023
a893260
refactor state machine
MalteSchm Apr 5, 2023
1e4337e
merging
MalteSchm Apr 6, 2023
515bb1c
merging mqtt enable/disable with new refactored state machine code
MalteSchm Apr 12, 2023
1b29133
merging functionality from PL refactor
MalteSchm Apr 6, 2023
79834e4
adding Mqtt handling to main.cpp
MalteSchm Apr 12, 2023
c430b69
Merge branch 'helgeerbe:master' into master
MalteSchm Apr 12, 2023
f99f801
Merge remote-tracking branch 'upstream/development'
MalteSchm Apr 12, 2023
d2fc00b
Merge branch 'master' of https://github.com/MalteSchm/OpenDTU-OnBattery
MalteSchm Apr 12, 2023
74dcdda
merging linting error fixes
MalteSchm Apr 12, 2023
03ce715
Merge remote-tracking branch 'upstream/development'
MalteSchm Apr 12, 2023
9ff1885
Removing un-necessary timestamp, commenting code and cleanups
MalteSchm Apr 13, 2023
fc5089e
resolving merge conflict
MalteSchm Apr 13, 2023
9efe076
resolving merge conflict
MalteSchm Apr 13, 2023
ee82c8c
adding option to disable power limiter via mqtt
MalteSchm Apr 12, 2023
9999fa2
refactor state machine
MalteSchm Apr 5, 2023
ee37682
merging functionality from PL refactor
MalteSchm Apr 6, 2023
01849dc
adding Mqtt handling to main.cpp
MalteSchm Apr 12, 2023
be7a43f
Removing un-necessary timestamp, commenting code and cleanups
MalteSchm Apr 13, 2023
690025e
fixing a bug from merging and remove a leftover debug message
MalteSchm Apr 13, 2023
3c50822
remove a leftover debug message
MalteSchm Apr 13, 2023
a306bc1
Bugfix for Mqtt enable / disable (was float, uses int now)
MalteSchm Apr 16, 2023
0d0a624
make sure that PL is enabled if user uses webinterface
MalteSchm Apr 16, 2023
4ddaa76
make sure that PL is enabled if user uses webinterface
MalteSchm Apr 16, 2023
82699b1
Merge branch 'mqtt_power_limiter_enable' of https://github.com/MalteS…
MalteSchm Apr 16, 2023
8c9afbc
fix an issue if inverter is behind power meter
MalteSchm Apr 23, 2023
322f532
Proper handling of use solar power only case
MalteSchm Apr 25, 2023
a8554f9
refactored use solar power code
MalteSchm Apr 25, 2023
0a0488f
refactored use solar power code
MalteSchm Apr 25, 2023
c7505ea
useSolarPowerOnly did not take current consumption into account. fixed
MalteSchm Apr 25, 2023
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ Topics for 3 phases of a power meter is configurable. Given is an example for th
| huawei/output_temp | R | Output air temperature | °C |
| huawei/efficiency | R | Efficiency | Percentage |

## Power Limiter topics
| Topic | R / W | Description | Value / Unit |
| --------------------------------------- | ----- | ---------------------------------------------------- | -------------------------- |
| powerlimiter/cmd/disable | W | Power Limiter disable override for external PL control | 0 / 1 |
| powerlimiter/status/disabled | R | Power Limiter disable override status | 0 / 1 |

## Currently supported Inverters
* Hoymiles HM-300
* Hoymiles HM-350
Expand Down
20 changes: 20 additions & 0 deletions include/MqttHandlePowerLimiter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include "Configuration.h"
#include <espMqttClient.h>

class MqttHandlePowerLimiterClass {
public:
void init();
void loop();

private:
void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);

uint32_t _lastPublishStats;
uint32_t _lastPublish;

};

extern MqttHandlePowerLimiterClass MqttHandlePowerLimiter;
13 changes: 7 additions & 6 deletions include/PowerLimiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
#include <memory>

typedef enum {
STATE_DISCOVER = 0,
STATE_OFF,
STATE_CONSUME_SOLAR_POWER_ONLY,
STATE_NORMAL_OPERATION
SHUTDOWN = 0,
ACTIVE
} plStates;

typedef enum {
Expand All @@ -26,13 +24,16 @@ class PowerLimiterClass {
void loop();
plStates getPowerLimiterState();
int32_t getLastRequestedPowewrLimit();
void setDisable(bool disable);
bool getDisable();

private:
uint32_t _lastCommandSent = 0;
uint32_t _lastLoop = 0;
int32_t _lastRequestedPowerLimit = 0;
uint32_t _lastLimitSetTime = 0;
plStates _plState = STATE_DISCOVER;
plStates _plState = ACTIVE;
bool _disabled = false;
bool _batteryDischargeEnabled = false;

float _powerMeter1Power;
float _powerMeter2Power;
Expand Down
87 changes: 87 additions & 0 deletions src/MqttHandlePowerLimiter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler, Malte Schmidt and others
*/
#include "MessageOutput.h"
#include "MqttSettings.h"
#include "MqttHandlePowerLimiter.h"
#include "PowerLimiter.h"
#include <ctime>

#define TOPIC_SUB_POWER_LIMITER "disable"

MqttHandlePowerLimiterClass MqttHandlePowerLimiter;

void MqttHandlePowerLimiterClass::init()
{
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
using std::placeholders::_4;
using std::placeholders::_5;
using std::placeholders::_6;

String topic = MqttSettings.getPrefix();
MqttSettings.subscribe(String(topic + "powerlimiter/cmd/" + TOPIC_SUB_POWER_LIMITER).c_str(), 0, std::bind(&MqttHandlePowerLimiterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));

_lastPublish = millis();

}


void MqttHandlePowerLimiterClass::loop()
{
if (!MqttSettings.getConnected() ) {
return;
}

const CONFIG_T& config = Configuration.get();

if ((millis() - _lastPublish) > (config.Mqtt_PublishInterval * 1000) ) {
MqttSettings.publish("powerlimiter/status/disabled", String(PowerLimiter.getDisable()));

yield();
_lastPublish = millis();
}
}


void MqttHandlePowerLimiterClass::onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total)
{
const CONFIG_T& config = Configuration.get();

char token_topic[MQTT_MAX_TOPIC_STRLEN + 40]; // respect all subtopics
strncpy(token_topic, topic, MQTT_MAX_TOPIC_STRLEN + 40); // convert const char* to char*

char* setting;
char* rest = &token_topic[strlen(config.Mqtt_Topic)];

strtok_r(rest, "/", &rest); // Remove "powerlimiter"
strtok_r(rest, "/", &rest); // Remove "cmd"

setting = strtok_r(rest, "/", &rest);

if (setting == NULL) {
return;
}

char* str = new char[len + 1];
memcpy(str, payload, len);
str[len] = '\0';
uint8_t payload_val = atoi(str);
delete[] str;

if (!strcmp(setting, TOPIC_SUB_POWER_LIMITER)) {
if(payload_val == 1) {
MessageOutput.println("Power limiter disabled");
PowerLimiter.setDisable(true);
return;
}
if(payload_val == 0) {
MessageOutput.println("Power limiter enabled");
PowerLimiter.setDisable(false);
return;
}
MessageOutput.println("Power limiter enable / disable - unknown command received. Please use 0 or 1");
}
}
Loading