Skip to content

Commit

Permalink
Merge branch 'pr/MalteSchm/172' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
helgeerbe committed Apr 26, 2023
2 parents e91935a + c7505ea commit c337df6
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 142 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,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

| Model | Required RF Module | DC Inputs | MPP-Tracker | AC Phases |
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

0 comments on commit c337df6

Please sign in to comment.