Merge branch 'main' into dev-v2.3

This commit is contained in:
Gunnar Skjold
2023-12-25 07:41:01 +01:00
36 changed files with 500 additions and 297 deletions

View File

@@ -97,7 +97,6 @@ ADC_MODE(ADC_VCC);
uint8_t commonBuffer[BUF_SIZE_COMMON];
HwTools hw;
DNSServer* dnsServer = NULL;
@@ -555,8 +554,15 @@ void loop() {
if (mqttEnabled || config.isMqttChanged()) {
if(mqttHandler == NULL || !mqttHandler->connected() || config.isMqttChanged()) {
if(mqttHandler != NULL && config.isMqttChanged()) {
MqttConfig mqttConfig;
if(config.getMqttConfig(mqttConfig)) {
mqttHandler->disconnect();
mqttHandler->setConfig(mqttConfig);
config.ackMqttChange();
}
}
MQTT_connect();
config.ackMqttChange();
}
} else if(mqttHandler != NULL) {
mqttHandler->disconnect();
@@ -1248,7 +1254,7 @@ void postConnect() {
}
unsigned long lastMqttRetry = -10000;
unsigned long lastMqttRetry = -20000;
void MQTT_connect() {
if(millis() - lastMqttRetry < (config.isMqttChanged() ? 5000 : 30000)) {
yield();
@@ -1266,9 +1272,14 @@ void MQTT_connect() {
mqttEnabled = true;
ws.setMqttEnabled(true);
if(mqttHandler != NULL && mqttHandler->getFormat() != mqttConfig.payloadFormat) {
delete mqttHandler;
mqttHandler = NULL;
if(mqttHandler != NULL) {
mqttHandler->disconnect();
if(mqttHandler->getFormat() != mqttConfig.payloadFormat) {
delete mqttHandler;
mqttHandler = NULL;
} else if(config.isMqttChanged()) {
mqttHandler->setConfig(mqttConfig);
}
}
if(mqttHandler == NULL) {
@@ -1300,6 +1311,9 @@ void MQTT_connect() {
if(mqttHandler != NULL) {
mqttHandler->connect();
mqttHandler->publishSystem(&hw, ps, &ea);
if(eapi != NULL && eapi->getValueForHour(0) != ENTSOE_NO_VALUE) {
mqttHandler->publishPrices(eapi);
}
}
}

View File

@@ -7,7 +7,7 @@
#include "IEC6205621.h"
#include "Uptime.h"
IEC6205621::IEC6205621(const char* p, Timezone* tz) {
IEC6205621::IEC6205621(const char* p, Timezone* tz, MeterConfig* meterConfig) {
if(strlen(p) < 16)
return;
@@ -25,6 +25,9 @@ IEC6205621::IEC6205621(const char* p, Timezone* tz) {
} else if(listId.startsWith(F("KMP"))) {
meterType = AmsTypeKamstrup;
listId = listId.substring(0,4);
} else if(listId.startsWith(F("KAM"))) {
meterType = AmsTypeKamstrup;
listId = listId.substring(0,4);
} else if(listId.startsWith(F("ISk"))) {
meterType = AmsTypeIskra;
listId = listId.substring(0,5);
@@ -136,6 +139,29 @@ IEC6205621::IEC6205621(const char* p, Timezone* tz) {
if (l1activeImportPower > 0 || l2activeImportPower > 0 || l3activeImportPower > 0 || l1activeExportPower > 0 || l2activeExportPower > 0 || l3activeExportPower > 0)
listType = 4;
if(meterConfig->wattageMultiplier > 0) {
activeImportPower = activeImportPower > 0 ? activeImportPower * (meterConfig->wattageMultiplier / 1000.0) : 0;
activeExportPower = activeExportPower > 0 ? activeExportPower * (meterConfig->wattageMultiplier / 1000.0) : 0;
reactiveImportPower = reactiveImportPower > 0 ? reactiveImportPower * (meterConfig->wattageMultiplier / 1000.0) : 0;
reactiveExportPower = reactiveExportPower > 0 ? reactiveExportPower * (meterConfig->wattageMultiplier / 1000.0) : 0;
}
if(meterConfig->voltageMultiplier > 0) {
l1voltage = l1voltage > 0 ? l1voltage * (meterConfig->voltageMultiplier / 1000.0) : 0;
l2voltage = l2voltage > 0 ? l2voltage * (meterConfig->voltageMultiplier / 1000.0) : 0;
l3voltage = l3voltage > 0 ? l3voltage * (meterConfig->voltageMultiplier / 1000.0) : 0;
}
if(meterConfig->amperageMultiplier > 0) {
l1current = l1current > 0 ? l1current * (meterConfig->amperageMultiplier / 1000.0) : 0;
l2current = l2current > 0 ? l2current * (meterConfig->amperageMultiplier / 1000.0) : 0;
l3current = l3current > 0 ? l3current * (meterConfig->amperageMultiplier / 1000.0) : 0;
}
if(meterConfig->accumulatedMultiplier > 0) {
activeImportCounter = activeImportCounter > 0 ? activeImportCounter * (meterConfig->accumulatedMultiplier / 1000.0) : 0;
activeExportCounter = activeExportCounter > 0 ? activeExportCounter * (meterConfig->accumulatedMultiplier / 1000.0) : 0;
reactiveImportCounter = reactiveImportCounter > 0 ? reactiveImportCounter * (meterConfig->accumulatedMultiplier / 1000.0) : 0;
reactiveExportCounter = reactiveExportCounter > 0 ? reactiveExportCounter * (meterConfig->accumulatedMultiplier / 1000.0) : 0;
}
threePhase = l1voltage > 0 && l2voltage > 0 && l3voltage > 0;
twoPhase = (l1voltage > 0 && l2voltage > 0) || (l2voltage > 0 && l3voltage > 0) || (l3voltage > 0 && l1voltage > 0);
}

View File

@@ -10,10 +10,11 @@
#include "Arduino.h"
#include "AmsData.h"
#include "Timezone.h"
#include "AmsConfiguration.h"
class IEC6205621 : public AmsData {
public:
IEC6205621(const char* payload, Timezone* tz);
IEC6205621(const char* payload, Timezone* tz, MeterConfig* meterConfig);
private:
String extract(String payload, String obis);

View File

@@ -106,10 +106,7 @@ IEC6205675::IEC6205675(const char* d, uint8_t useMeterType, MeterConfig* meterCo
if(listType >= 2 && memcmp(meterModel.c_str(), "MA304T3", 7) == 0) {
l2voltage = sqrt(pow(l1voltage - l3voltage * cos(60 * (PI/180)), 2) + pow(l3voltage * sin(60 * (PI/180)),2));
if(l2voltage > 0) {
l2current = ((activeImportPower - activeExportPower) - (l1voltage * l1current) - (l3voltage * l3current)) / l2voltage;
l2currentEstimated = true;
}
l2currentMissing = true;
}
if(listType == 3) {
@@ -303,6 +300,8 @@ IEC6205675::IEC6205675(const char* d, uint8_t useMeterType, MeterConfig* meterCo
if(val != NOVALUE) {
listType = 2;
l2current = val;
} else if(listType == 2) {
l2currentMissing = true;
}
val = getNumber(AMS_OBIS_CURRENT_L3, sizeof(AMS_OBIS_CURRENT_L3), ((char *) (d)));
if(val != NOVALUE) {
@@ -493,15 +492,7 @@ IEC6205675::IEC6205675(const char* d, uint8_t useMeterType, MeterConfig* meterCo
// Special case for Norwegian IT/TT meters that does not report all values
if(meterConfig->distributionSystem == 1) {
if(threePhase) {
if(l2current == 0.0 && l1current > 0.0 && l3current > 0.0) {
l2current = ((activeImportPower - activeExportPower) - (l1voltage * l1current) - (l3voltage * l3current)) / l2voltage;
if(activeExportPower == 0.0) {
l2current = max((float) 0.0, l2current);
}
l2currentEstimated = true;
}
} else if(twoPhase && l1current > 0.0 && l2current > 0.0 && l3current > 0.0) {
if(twoPhase && l1current > 0.0 && l2current > 0.0 && l3current > 0.0) {
l2voltage = sqrt(pow(l1voltage - l3voltage * cos(60.0 * (PI/180.0)), 2) + pow(l3voltage * sin(60.0 * (PI/180.0)),2));
threePhase = true;
}

View File

@@ -168,8 +168,8 @@ bool PassiveMeterCommunicator::loop() {
// After one hour, adjust buffer size to match the largest payload
if(!maxDetectPayloadDetectDone && now > 3600000) {
if(maxDetectedPayloadSize * 1.5 > meterConfig.bufferSize * 64) {
int bufferSize = min((double) 64, ceil((maxDetectedPayloadSize * 1.5) / 64));
if(maxDetectedPayloadSize * 1.25 > meterConfig.bufferSize * 64) {
int bufferSize = min((double) 64, ceil((maxDetectedPayloadSize * 1.25) / 64));
#if defined(ESP8266)
if(meterConfig.rxPin != 3 && meterConfig.rxPin != 113) {
bufferSize = min(bufferSize, 2);
@@ -504,6 +504,7 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
}
if(meterConfig.bufferSize < 1) meterConfig.bufferSize = 1;
if(meterConfig.bufferSize > 64) meterConfig.bufferSize = 64;
if(hwSerial != NULL) {
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Hardware serial\n"));
@@ -598,6 +599,11 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
break;
}
uint8_t bufferSize = meterConfig.bufferSize;
#if defined(ESP8266)
if(bufferSize > 2) bufferSize = 2;
#endif
if (debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Using serial buffer size %d"), 64 * bufferSize);
swSerial->begin(baud, serialConfig, pin, -1, invert, meterConfig.bufferSize * 64);
hanSerial = swSerial;
@@ -606,6 +612,12 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
hwSerial = NULL;
}
if(hanBuffer != NULL) {
free(hanBuffer);
}
hanBufferSize = max(64 * meterConfig.bufferSize * 2, 512);
hanBuffer = (uint8_t*) malloc(hanBufferSize);
// The library automatically sets the pullup in Serial.begin()
if(!meterConfig.rxPinPullup) {
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("HAN pin pullup disabled\n"));

View File

@@ -39,7 +39,8 @@ protected:
PassthroughMqttHandler* pt = NULL;
uint8_t hanBuffer[BUF_SIZE_HAN];
uint8_t *hanBuffer;
uint16_t hanBufferSize = 512;
Stream *hanSerial;
SoftwareSerial *swSerial = NULL;
HardwareSerial *hwSerial = NULL;

View File

@@ -39,4 +39,7 @@ bool PassthroughMqttHandler::publishString(char* str) {
uint8_t PassthroughMqttHandler::getFormat() {
return 255;
}
}
void PassthroughMqttHandler::onMessage(String &topic, String &payload) {
}

View File

@@ -23,6 +23,8 @@ public:
private:
String topic;
void onMessage(String &topic, String &payload);
uint8_t getFormat();
};
#endif