From d41c26a3cec4d0679747cd9135c6fed6a8465eff Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Thu, 21 Jul 2022 17:06:24 +0200 Subject: [PATCH 1/7] first poc set power limit via mqtt --- tools/esp8266/app.cpp | 36 ++++++++++++++++++++++++++++++------ tools/esp8266/app.h | 1 + tools/esp8266/hmRadio.h | 22 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index 38b75e6e..2e4cf4a5 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -81,8 +81,8 @@ void app::setup(uint32_t timeout) { if(0ULL != invSerial) { iv = mSys->addInverter(name, invSerial, modPwr); if(NULL != iv) { - DPRINTLN(DBG_INFO, F("add inverter: ") + String(name) + ", SN: " + String(invSerial, HEX)); - + mEep->read(ADDR_INV_PWR_LIM + (i * 2),&iv->powerLimit); + DPRINTLN(DBG_INFO, F("add inverter: ") + String(name) + ", SN: " + String(invSerial, HEX) + ", Power Limit: " + String(iv->powerLimit)); for(uint8_t j = 0; j < 4; j++) { mEep->read(ADDR_INV_CH_NAME + (i * 4 * MAX_NAME_LENGTH) + j * MAX_NAME_LENGTH, iv->chName[j], MAX_NAME_LENGTH); } @@ -148,6 +148,7 @@ void app::setup(uint32_t timeout) { mqttPort = 1883; mMqtt.setup(mqttAddr, mqttTopic, mqttUser, mqttPwd, mqttPort); + mMqtt.mClient->setCallback(std::bind(&app::cbMqtt, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); mMqttTicker = 0; mSerialTicker = 0; @@ -229,7 +230,7 @@ void app::loop(void) { if(0 != len) { Inverter<> *iv = mSys->findInverter(&p->packet[1]); - if(NULL != iv) { + if(NULL != iv && p->packet[0] == 0x95) { uint8_t *pid = &p->packet[9]; if((*pid & 0x7F) < 5) { memcpy(mPayload[iv->id].data[(*pid & 0x7F) - 1], &p->packet[10], len-11); @@ -349,8 +350,15 @@ void app::loop(void) { yield(); if(mSerialDebug) DPRINTLN(DBG_INFO, F("Requesting Inverter SN ") + String(iv->serial.u64, HEX)); - mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts); - mRxTicker = 0; + if(iv->powerLimitChange){ + if(mSerialDebug) + DPRINTLN(DBG_INFO, F("Requesting Inverter to change power limit to ") + String(iv->powerLimit)); + mSys->Radio.sendControlPacket(iv->radioId.u64, uint16_t(iv->powerLimit*10)); + iv->powerLimitChange = false; + } else { + mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts); + mRxTicker = 0; + } } } else if(mSerialDebug) @@ -600,6 +608,22 @@ void app::showErase() { showReboot(); } +//----------------------------------------------------------------------------- +void app::cbMqtt(const char* topic, byte* payload, unsigned int length) { + DPRINTLN(DBG_INFO, F("app::cbMqtt")); + // DPRINTLN(DBG_INFO, topic); + // ToDo check topic ! + int inverterId = 0; // ToDo get inverter id from topic + Inverter<> *iv = this->mSys->getInverterByPos(inverterId); + if(NULL != iv) { + iv->powerLimit = std::stoi((char*)payload); + iv->powerLimitChange = true; + mEep->write(ADDR_INV_PWR_LIM + inverterId * 2,iv->powerLimit); + DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit) + F("W") ); + } +} + + //----------------------------------------------------------------------------- void app::showStatistics(void) { @@ -690,7 +714,7 @@ void app::showLiveData(void) { } modHtml += F("
" - "
") + String(iv->name) + F(""); + "
") + String(iv->name) + F(" Limit ") + String(iv->powerLimit) + F(" W"); uint8_t list[] = {FLD_UAC, FLD_IAC, FLD_PAC, FLD_F, FLD_PCT, FLD_T, FLD_YT, FLD_YD, FLD_PDC, FLD_EFF}; for(uint8_t fld = 0; fld < 10; fld++) { diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index 306cb238..96d1b9cb 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -51,6 +51,7 @@ class app : public Main { void setup(uint32_t timeout); void loop(void); void handleIntr(void); + void cbMqtt(const char* topic, byte* payload, unsigned int length); uint8_t app_loops; uint8_t getIrqPin(void) { diff --git a/tools/esp8266/hmRadio.h b/tools/esp8266/hmRadio.h index 18058e76..69100118 100644 --- a/tools/esp8266/hmRadio.h +++ b/tools/esp8266/hmRadio.h @@ -166,6 +166,28 @@ class HmRadio { return mTxChLst[mTxChIdx]; }*/ + void sendControlPacket(uint64_t invId, uint16_t data) { + DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendControlPacket")); + sendCmdPacket(invId, 0x51, 0x80, false); + mTxBuf[10] = 0x0b; // control type --> 0x0b => Type_ActivePowerContr + mTxBuf[11] = 0x00; + // 4 bytes control data + // Power Limit fix point 10 eg. 30 W --> 0d300 = 0x012c + mTxBuf[12] = (data >> 8) & 0xff; // 0x01 + mTxBuf[13] = (data ) & 0xff; // 0x2c + // + mTxBuf[14] = 0x00; + mTxBuf[15] = 0x00; + // crc control data + uint16_t crc = crc16(&mTxBuf[10], 6); + mTxBuf[16] = (crc >> 8) & 0xff; + mTxBuf[17] = (crc ) & 0xff; + // crc over all + mTxBuf[18] = crc8(mTxBuf, 18); + + sendPacket(invId, mTxBuf, 19, true); + } + void sendTimePacket(uint64_t invId, uint32_t ts) { //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendTimePacket")); sendCmdPacket(invId, 0x15, 0x80, false); From dccd04ab42ff637829849c8e43d17840f0ef0235 Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Thu, 21 Jul 2022 17:08:55 +0200 Subject: [PATCH 2/7] first poc for power set via mqtt --- .gitignore | 1 + tools/esp8266/defines.h | 4 +++- tools/esp8266/hmInverter.h | 4 ++++ tools/esp8266/mqtt.h | 16 +++++++++++----- tools/esp8266/platformio.ini | 1 - 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 54e17c93..8beaceb6 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ tools/esp8266/binaries *.db *.suo *.ipch +tools/esp8266/.vscode/extensions.json diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index c746930c..f18dcb4c 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -53,6 +53,7 @@ typedef struct { #define INV_CH_CH_NAME_LEN MAX_NUM_INVERTERS * MAX_NAME_LENGTH * 4 // (4 channels) #define INV_INTERVAL_LEN 2 // uint16_t #define INV_MAX_RTRY_LEN 1 // uint8_t +#define INV_PWR_LIM_LEN MAX_NUM_INVERTERS * 2 // uint16_t #define PINOUT_LEN 3 // 3 pins: CS, CE, IRQ @@ -89,8 +90,9 @@ typedef struct { #define ADDR_INV_CH_NAME ADDR_INV_CH_PWR + INV_CH_CH_PWR_LEN #define ADDR_INV_INTERVAL ADDR_INV_CH_NAME + INV_CH_CH_NAME_LEN #define ADDR_INV_MAX_RTRY ADDR_INV_INTERVAL + INV_INTERVAL_LEN +#define ADDR_INV_PWR_LIM ADDR_INV_MAX_RTRY + INV_MAX_RTRY_LEN -#define ADDR_MQTT_ADDR ADDR_INV_MAX_RTRY + INV_MAX_RTRY_LEN +#define ADDR_MQTT_ADDR ADDR_INV_PWR_LIM + INV_PWR_LIM_LEN #define ADDR_MQTT_USER ADDR_MQTT_ADDR + MQTT_ADDR_LEN #define ADDR_MQTT_PWD ADDR_MQTT_USER + MQTT_USER_LEN #define ADDR_MQTT_TOPIC ADDR_MQTT_PWD + MQTT_PWD_LEN diff --git a/tools/esp8266/hmInverter.h b/tools/esp8266/hmInverter.h index 8a27fc88..2e65a2c7 100644 --- a/tools/esp8266/hmInverter.h +++ b/tools/esp8266/hmInverter.h @@ -69,6 +69,8 @@ class Inverter { uint8_t type; // integer which refers to inverter type byteAssign_t* assign; // type of inverter uint8_t listLen; // length of assignments + uint16_t powerLimit; // limit power output + bool powerLimitChange; // true if change needed serial_u serial; // serial number as on barcode serial_u radioId; // id converted to modbus uint8_t channels; // number of PV channels (1-4) @@ -79,6 +81,8 @@ class Inverter { Inverter() { ts = 0; + powerLimit = -1; // 65535 W Limit -> unlimited + powerLimitChange = false; } ~Inverter() { diff --git a/tools/esp8266/mqtt.h b/tools/esp8266/mqtt.h index 89570344..c6a2bf2b 100644 --- a/tools/esp8266/mqtt.h +++ b/tools/esp8266/mqtt.h @@ -12,6 +12,8 @@ class mqtt { public: + PubSubClient *mClient; + mqtt() { mClient = new PubSubClient(mEspClient); mAddressSet = false; @@ -35,6 +37,10 @@ class mqtt { snprintf(mTopic, MQTT_TOPIC_LEN, "%s", topic); } + void setCallback(void (*func)(const char* topic, byte* payload, unsigned int length)){ + mClient->setCallback(func); + } + void sendMsg(const char *topic, const char *msg) { //DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg")); char top[64]; @@ -79,25 +85,25 @@ class mqtt { void loop() { //DPRINT(F("m")); - //if(!mClient->connected()) - // reconnect(); + if(!mClient->connected()) + reconnect(); mClient->loop(); } private: void reconnect(void) { - //DPRINTLN(DBG_VERBOSE, F("mqtt.h:reconnect")); + DPRINTLN(DBG_INFO, F("mqtt.h:reconnect")); if(!mClient->connected()) { if((strlen(mUser) > 0) && (strlen(mPwd) > 0)) mClient->connect(DEF_DEVICE_NAME, mUser, mPwd); else mClient->connect(DEF_DEVICE_NAME); } + mClient->subscribe("home/huette/powerset"); } WiFiClient mEspClient; - PubSubClient *mClient; - + bool mAddressSet; uint16_t mPort; char mUser[MQTT_USER_LEN]; diff --git a/tools/esp8266/platformio.ini b/tools/esp8266/platformio.ini index 0728ec47..21b37c67 100644 --- a/tools/esp8266/platformio.ini +++ b/tools/esp8266/platformio.ini @@ -28,7 +28,6 @@ framework = arduino board = nodemcuv2 monitor_speed = 115200 board_build.f_cpu = 80000000L -upload_port = /dev/ttyUSB0 lib_deps = nrf24/RF24@1.4.2 From 7ed5a0a9e1a40a1198264abafb70d0d8b84b8df5 Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Mon, 1 Aug 2022 21:26:25 +0200 Subject: [PATCH 3/7] active power limit added via mqtt and setup page --- tools/esp8266/app.cpp | 103 +++++++++++++++++++++++++++++++------ tools/esp8266/app.h | 2 +- tools/esp8266/hmInverter.h | 5 +- tools/esp8266/hmRadio.h | 36 +++++++------ tools/esp8266/mqtt.h | 5 +- 5 files changed, 116 insertions(+), 35 deletions(-) diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index f21a72ec..0a071a74 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -82,6 +82,7 @@ void app::setup(uint32_t timeout) { iv = mSys->addInverter(name, invSerial, modPwr); if(NULL != iv) { mEep->read(ADDR_INV_PWR_LIM + (i * 2),&iv->powerLimit); + iv->devControlRequest = true; // set to true to update the active power limit from setup html page DPRINTLN(DBG_INFO, F("add inverter: ") + String(name) + ", SN: " + String(invSerial, HEX) + ", Power Limit: " + String(iv->powerLimit)); for(uint8_t j = 0; j < 4; j++) { mEep->read(ADDR_INV_CH_NAME + (i * 4 * MAX_NAME_LENGTH) + j * MAX_NAME_LENGTH, iv->chName[j], MAX_NAME_LENGTH); @@ -216,6 +217,8 @@ void app::loop(void) { bool rxRdy = mSys->Radio.switchRxCh(); + char respTopic[64]; + if(!mSys->BufCtrl.empty()) { uint8_t len; packet_t *p = mSys->BufCtrl.getBack(); @@ -230,7 +233,7 @@ void app::loop(void) { if(0 != len) { Inverter<> *iv = mSys->findInverter(&p->packet[1]); - if(NULL != iv && p->packet[0] == 0x95) { + if(NULL != iv && p->packet[0] == (0x15 + 0x80)) { // response from get all information command uint8_t *pid = &p->packet[9]; if (*pid == 0x00) { DPRINT(DBG_DEBUG, "fragment number zero received and ignored"); @@ -249,6 +252,10 @@ void app::loop(void) { } } } + if(NULL != iv && p->packet[0] == (0x51 + 0x80)) { // response from dev control command q'n'd + snprintf(respTopic, 64, "%s/devcontrol/%d/resp", mMqtt.getTopic(), iv->id); + mMqtt.sendMsg2(respTopic,(const char*)p->packet,false); + } } } @@ -354,11 +361,13 @@ void app::loop(void) { yield(); if(mSerialDebug) DPRINTLN(DBG_INFO, F("Requesting Inverter SN ") + String(iv->serial.u64, HEX)); - if(iv->powerLimitChange){ + if(iv->devControlRequest){ if(mSerialDebug) - DPRINTLN(DBG_INFO, F("Requesting Inverter to change power limit to ") + String(iv->powerLimit)); - mSys->Radio.sendControlPacket(iv->radioId.u64, uint16_t(iv->powerLimit*10)); - iv->powerLimitChange = false; + DPRINTLN(DBG_INFO, F("Devcontrol request ") + String(iv->devControlCmd) + F(" power limit ") + String(iv->powerLimit)); + mSys->Radio.sendControlPacket(iv->radioId.u64, uint16_t(iv->powerLimit),iv->devControlCmd); + // ToDo: Only set Request to false if succesful executed + iv->devControlRequest = false; + // ^^ } else { mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts); mRxTicker = 0; @@ -504,10 +513,12 @@ void app::showSetup(void) { uint64_t invSerial; char name[MAX_NAME_LENGTH + 1] = {0}; uint16_t modPwr[4]; + uint16_t invActivePowerLimit = -1; for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { mEep->read(ADDR_INV_ADDR + (i * 8), &invSerial); mEep->read(ADDR_INV_NAME + (i * MAX_NAME_LENGTH), name, MAX_NAME_LENGTH); mEep->read(ADDR_INV_CH_PWR + (i * 2 * 4), modPwr, 4); + mEep->read(ADDR_INV_PWR_LIM + (i * 2),&invActivePowerLimit); inv += F("

Inverter ") + String(i) + "

"; inv += F(""); @@ -521,6 +532,12 @@ void app::showSetup(void) { inv += String(name); inv += F("\"/ maxlength=\"") + String(MAX_NAME_LENGTH) + "\">"; + inv += F(""); + inv += F(""; + + inv += F(""); for(uint8_t j = 0; j < 4; j++) { @@ -613,17 +630,68 @@ void app::showErase() { } //----------------------------------------------------------------------------- -void app::cbMqtt(const char* topic, byte* payload, unsigned int length) { +void app::cbMqtt(char* topic, byte* payload, unsigned int length) { + // callback handling on subscribed devcontrol topic DPRINTLN(DBG_INFO, F("app::cbMqtt")); - // DPRINTLN(DBG_INFO, topic); - // ToDo check topic ! - int inverterId = 0; // ToDo get inverter id from topic - Inverter<> *iv = this->mSys->getInverterByPos(inverterId); - if(NULL != iv) { - iv->powerLimit = std::stoi((char*)payload); - iv->powerLimitChange = true; - mEep->write(ADDR_INV_PWR_LIM + inverterId * 2,iv->powerLimit); - DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit) + F("W") ); + DPRINTLN(DBG_INFO, topic); + // subcribed topics are mTopic + "/devcontrol/#" where # is // + // eg. mypvsolar/devcontrol/1/11/400 --> inverter 1 active power limit 400 Watt + char *token = strtok(topic, "/"); + while (token != NULL) + { + // .../devcontrol/// + // ^^^ + if (strcmp(token,"devcontrol")){ + Inverter<> *iv = this->mSys->getInverterByPos(std::stoi(strtok(NULL, "/"))); + // .../devcontrol/// + // ^^^ + if(NULL != iv) { + // switch case subcmd + switch ( std::stoi(strtok(NULL, "/")) ){ + // .../devcontrol/// + // ^^^^ + case 11: // Active Power Control + iv->devControlCmd = 11; + iv->powerLimit = std::stoi(strtok(NULL, "/")); + // .../devcontrol/// + // ^^^ + mEep->write(ADDR_INV_PWR_LIM + iv->id * 2,iv->powerLimit); + // updateCrc(); + // mEep->commit(); + DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit) + F("W") ); + break; + case 0: // Turn On + iv->devControlCmd = 0; + DPRINTLN(DBG_INFO, F("Turn on inverter ") + String(iv->id) ); + break; + case 1: // Turn Off + iv->devControlCmd = 1; + DPRINTLN(DBG_INFO, F("Turn off inverter ") + String(iv->id) ); + break; + case 2: // Restart + iv->devControlCmd = 2; + DPRINTLN(DBG_INFO, F("Restart inverter ") + String(iv->id) ); + break; + case 12: // Reactive Power Control + // iv->devControlCmd = 12; + // uint16_t reactive_power = std::stoi(strtok(NULL, "/")); + // .../devcontrol/// + // ^^^ + DPRINTLN(DBG_INFO, F("Reactive Power Control not implemented for inverter ") + String(iv->id) ); + break; + case 13: // Set Power Factor + // iv->devControlCmd = 13; + // uint16_t power_factor = std::stoi(strtok(NULL, "/")); + // .../devcontrol/// + // ^^^ + DPRINTLN(DBG_INFO, F("Set Power Factor not implemented for inverter ") + String(iv->id) ); + break; + } + iv->devControlRequest = true; + } + break; + } + token = strtok(NULL, "/"); } } @@ -813,6 +881,7 @@ void app::saveValues(bool webSend = true) { char buf[20] = {0}; uint8_t i = 0; uint16_t interval; + uint16_t activepowerlimit=-1; // inverter serial_u addr; @@ -824,6 +893,10 @@ void app::saveValues(bool webSend = true) { addr.u64 = Serial2u64(buf); mEep->write(ADDR_INV_ADDR + (i * 8), addr.u64); + // active power limit + activepowerlimit = mWeb->arg("inv" + String(i) + "ActivePowerLimit").toInt(); + mEep->write(ADDR_INV_PWR_LIM + i * 2,activepowerlimit); + // name mWeb->arg("inv" + String(i) + "Name").toCharArray(buf, 20); mEep->write(ADDR_INV_NAME + (i * MAX_NAME_LENGTH), buf, MAX_NAME_LENGTH); diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index 96d1b9cb..2f3d9e8e 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -51,7 +51,7 @@ class app : public Main { void setup(uint32_t timeout); void loop(void); void handleIntr(void); - void cbMqtt(const char* topic, byte* payload, unsigned int length); + void cbMqtt(char* topic, byte* payload, unsigned int length); uint8_t app_loops; uint8_t getIrqPin(void) { diff --git a/tools/esp8266/hmInverter.h b/tools/esp8266/hmInverter.h index 2e65a2c7..213e8c66 100644 --- a/tools/esp8266/hmInverter.h +++ b/tools/esp8266/hmInverter.h @@ -70,7 +70,8 @@ class Inverter { byteAssign_t* assign; // type of inverter uint8_t listLen; // length of assignments uint16_t powerLimit; // limit power output - bool powerLimitChange; // true if change needed + uint8_t devControlCmd; // carries the requested cmd + bool devControlRequest; // true if change needed serial_u serial; // serial number as on barcode serial_u radioId; // id converted to modbus uint8_t channels; // number of PV channels (1-4) @@ -82,7 +83,7 @@ class Inverter { Inverter() { ts = 0; powerLimit = -1; // 65535 W Limit -> unlimited - powerLimitChange = false; + devControlRequest = false; } ~Inverter() { diff --git a/tools/esp8266/hmRadio.h b/tools/esp8266/hmRadio.h index 69100118..1062664c 100644 --- a/tools/esp8266/hmRadio.h +++ b/tools/esp8266/hmRadio.h @@ -166,26 +166,30 @@ class HmRadio { return mTxChLst[mTxChIdx]; }*/ - void sendControlPacket(uint64_t invId, uint16_t data) { + void sendControlPacket(uint64_t invId, uint16_t data, uint8_t cmd) { DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendControlPacket")); - sendCmdPacket(invId, 0x51, 0x80, false); - mTxBuf[10] = 0x0b; // control type --> 0x0b => Type_ActivePowerContr - mTxBuf[11] = 0x00; - // 4 bytes control data - // Power Limit fix point 10 eg. 30 W --> 0d300 = 0x012c - mTxBuf[12] = (data >> 8) & 0xff; // 0x01 - mTxBuf[13] = (data ) & 0xff; // 0x2c - // - mTxBuf[14] = 0x00; - mTxBuf[15] = 0x00; + // sendCmdPacket(invId, 0x51, 0x80, false); // 0x80 implementation as original DTU code + sendCmdPacket(invId, 0x51, 0x81, false); + int cnt = 0; + mTxBuf[10] = cmd; // cmd --> 0x0b => Type_ActivePowerContr, 0 on, 1 off, 2 restart, 12 reactive power, 13 power factor + mTxBuf[10 + (++cnt)] = 0x00; + if (cmd == 11){ + // 4 bytes control data + // Power Limit fix point 10 eg. 30 W --> 0d300 = 0x012c + mTxBuf[10 + (++cnt)] = (data*10 >> 8) & 0xff; // 0x01 + mTxBuf[10 + (++cnt)] = (data*10 ) & 0xff; // 0x2c + // are these two bytes necessary? + mTxBuf[10 + (++cnt)] = 0x00; + mTxBuf[10 + (++cnt)] = 0x00; + } // crc control data - uint16_t crc = crc16(&mTxBuf[10], 6); - mTxBuf[16] = (crc >> 8) & 0xff; - mTxBuf[17] = (crc ) & 0xff; + uint16_t crc = crc16(&mTxBuf[10], 10 - (cnt+1)); + mTxBuf[10 + (++cnt)] = (crc >> 8) & 0xff; + mTxBuf[10 + (++cnt)] = (crc ) & 0xff; // crc over all - mTxBuf[18] = crc8(mTxBuf, 18); + mTxBuf[10 + (++cnt)] = crc8(mTxBuf, 18); - sendPacket(invId, mTxBuf, 19, true); + sendPacket(invId, mTxBuf, 10 + (++cnt), true); } void sendTimePacket(uint64_t invId, uint32_t ts) { diff --git a/tools/esp8266/mqtt.h b/tools/esp8266/mqtt.h index c6a2bf2b..9e973162 100644 --- a/tools/esp8266/mqtt.h +++ b/tools/esp8266/mqtt.h @@ -99,7 +99,10 @@ class mqtt { else mClient->connect(DEF_DEVICE_NAME); } - mClient->subscribe("home/huette/powerset"); + char topic[MQTT_TOPIC_LEN + 13 ]; // "/devcontrol/#" --> + 6 byte + // ToDo: "/devcontrol/#" is hardcoded + snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mTopic); + mClient->subscribe(topic); // subscribe to mTopic + "/devcontrol/#" } WiFiClient mEspClient; From 375a1ca9553069b80a6928bb161b02041290e3e6 Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 4 Aug 2022 07:31:36 +0200 Subject: [PATCH 4/7] increased version to 0.4.26 --- tools/esp8266/defines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index c746930c..66c1d889 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -21,7 +21,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 4 -#define VERSION_PATCH 25 +#define VERSION_PATCH 26 //------------------------------------- From 6b1f027d01b7778fcd9fd389c8727081f814bd0d Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Fri, 5 Aug 2022 13:20:12 +0200 Subject: [PATCH 5/7] devControl bugs and error handling --- tools/esp8266/app.cpp | 151 ++++++++++++++++++++++--------------- tools/esp8266/app.h | 1 + tools/esp8266/hmInverter.h | 1 + tools/esp8266/hmRadio.h | 19 +++-- tools/esp8266/main.cpp | 5 ++ tools/esp8266/main.h | 1 + tools/esp8266/mqtt.h | 1 + 7 files changed, 111 insertions(+), 68 deletions(-) diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index 0a071a74..7bc6ca9f 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -62,6 +62,7 @@ void app::setup(uint32_t timeout) { mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this)); mWeb->on("/livedata", std::bind(&app::showLiveData, this)); mWeb->on("/json", std::bind(&app::showJSON, this)); + mWeb->on("/devcontrol", std::bind(&app::devControl, this)); if(mSettingsValid) { mEep->read(ADDR_INV_INTERVAL, &mSendInterval); @@ -82,6 +83,7 @@ void app::setup(uint32_t timeout) { iv = mSys->addInverter(name, invSerial, modPwr); if(NULL != iv) { mEep->read(ADDR_INV_PWR_LIM + (i * 2),&iv->powerLimit); + iv->devControlCmd = 11; // set active power limit iv->devControlRequest = true; // set to true to update the active power limit from setup html page DPRINTLN(DBG_INFO, F("add inverter: ") + String(name) + ", SN: " + String(invSerial, HEX) + ", Power Limit: " + String(iv->powerLimit)); for(uint8_t j = 0; j < 4; j++) { @@ -205,7 +207,7 @@ void app::setup(uint32_t timeout) { void app::loop(void) { DPRINTLN(DBG_VERBOSE, F("app::loop")); Main::loop(); - + mSys->Radio.loop(); yield(); @@ -217,8 +219,6 @@ void app::loop(void) { bool rxRdy = mSys->Radio.switchRxCh(); - char respTopic[64]; - if(!mSys->BufCtrl.empty()) { uint8_t len; packet_t *p = mSys->BufCtrl.getBack(); @@ -252,9 +252,22 @@ void app::loop(void) { } } } - if(NULL != iv && p->packet[0] == (0x51 + 0x80)) { // response from dev control command q'n'd - snprintf(respTopic, 64, "%s/devcontrol/%d/resp", mMqtt.getTopic(), iv->id); - mMqtt.sendMsg2(respTopic,(const char*)p->packet,false); + if(NULL != iv && p->packet[0] == (0x51 + 0x80)) { // response from dev control command + DPRINTLN(DBG_INFO, F("Response from devcontrol received")); + iv->devControlRequest = false; + if (p->packet[12] != 11 && iv->devControlCmd == 11){ + // set back to last accpted limit + mEep->read(ADDR_INV_PWR_LIM + iv->id * 2, &iv->powerLimit); + DPRINTLN(DBG_INFO, F("Inverter has not accepted power limit set point")); + } + if (p->packet[12] == 11 && iv->devControlCmd == 11){ // ok inverter accepted the set point copy it to dtu eeprom + // on every reboot the dtu sets the power limit acc to the value in eeprom + mEep->write(ADDR_INV_PWR_LIM + iv->id * 2,iv->powerLimit); + updateCrc(); + mEep->commit(); + DPRINTLN(DBG_INFO, F("Inverter has accepted power limit set point, written to dtu eeprom")); + iv->devControlCmd = 0xff; // set to none known command. + } } } } @@ -364,10 +377,7 @@ void app::loop(void) { if(iv->devControlRequest){ if(mSerialDebug) DPRINTLN(DBG_INFO, F("Devcontrol request ") + String(iv->devControlCmd) + F(" power limit ") + String(iv->powerLimit)); - mSys->Radio.sendControlPacket(iv->radioId.u64, uint16_t(iv->powerLimit),iv->devControlCmd); - // ToDo: Only set Request to false if succesful executed - iv->devControlRequest = false; - // ^^ + mSys->Radio.sendControlPacket(iv->radioId.u64,iv->devControlCmd ,uint16_t(iv->powerLimit)); } else { mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts); mRxTicker = 0; @@ -633,66 +643,70 @@ void app::showErase() { void app::cbMqtt(char* topic, byte* payload, unsigned int length) { // callback handling on subscribed devcontrol topic DPRINTLN(DBG_INFO, F("app::cbMqtt")); - DPRINTLN(DBG_INFO, topic); - // subcribed topics are mTopic + "/devcontrol/#" where # is // - // eg. mypvsolar/devcontrol/1/11/400 --> inverter 1 active power limit 400 Watt - char *token = strtok(topic, "/"); + // subcribed topics are mTopic + "/devcontrol/#" where # is / + // eg. mypvsolar/devcontrol/1/11 with payload "400" --> inverter 1 active power limit 400 Watt + const char *token = strtok(topic, "/"); while (token != NULL) { - // .../devcontrol/// - // ^^^ - if (strcmp(token,"devcontrol")){ - Inverter<> *iv = this->mSys->getInverterByPos(std::stoi(strtok(NULL, "/"))); - // .../devcontrol/// - // ^^^ - if(NULL != iv) { - // switch case subcmd - switch ( std::stoi(strtok(NULL, "/")) ){ - // .../devcontrol/// - // ^^^^ - case 11: // Active Power Control - iv->devControlCmd = 11; - iv->powerLimit = std::stoi(strtok(NULL, "/")); - // .../devcontrol/// - // ^^^ - mEep->write(ADDR_INV_PWR_LIM + iv->id * 2,iv->powerLimit); - // updateCrc(); - // mEep->commit(); - DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit) + F("W") ); - break; - case 0: // Turn On - iv->devControlCmd = 0; - DPRINTLN(DBG_INFO, F("Turn on inverter ") + String(iv->id) ); - break; - case 1: // Turn Off - iv->devControlCmd = 1; - DPRINTLN(DBG_INFO, F("Turn off inverter ") + String(iv->id) ); - break; - case 2: // Restart - iv->devControlCmd = 2; - DPRINTLN(DBG_INFO, F("Restart inverter ") + String(iv->id) ); - break; - case 12: // Reactive Power Control - // iv->devControlCmd = 12; - // uint16_t reactive_power = std::stoi(strtok(NULL, "/")); - // .../devcontrol/// - // ^^^ - DPRINTLN(DBG_INFO, F("Reactive Power Control not implemented for inverter ") + String(iv->id) ); - break; - case 13: // Set Power Factor - // iv->devControlCmd = 13; - // uint16_t power_factor = std::stoi(strtok(NULL, "/")); - // .../devcontrol/// - // ^^^ - DPRINTLN(DBG_INFO, F("Set Power Factor not implemented for inverter ") + String(iv->id) ); - break; + if (std::strcmp(token,"devcontrol")==0){ + token = strtok(NULL, "/"); + uint8_t iv_id = std::stoi(token); + if (iv_id >= 0 && iv_id <= MAX_NUM_INVERTERS){ + Inverter<> *iv = this->mSys->getInverterByPos(iv_id); + if(NULL != iv) { + if (!iv->devControlRequest) { // still pending + token = strtok(NULL, "/"); + uint8_t subcmd = std::stoi(token); + switch ( subcmd ){ + case 11: // Active Power Control + if (true){ // if (std::stoi((char*)payload) > 0) error handling powerlimit needed? + iv->devControlCmd = 11; + iv->powerLimit = std::stoi((char*)payload); + DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit) + F("W") ); + } + iv->devControlRequest = true; + break; + case 0: // Turn On + iv->devControlCmd = 0; + DPRINTLN(DBG_INFO, F("Turn on inverter ") + String(iv->id) ); + iv->devControlRequest = true; + break; + case 1: // Turn Off + iv->devControlCmd = 1; + DPRINTLN(DBG_INFO, F("Turn off inverter ") + String(iv->id) ); + iv->devControlRequest = true; + break; + case 2: // Restart + iv->devControlCmd = 2; + DPRINTLN(DBG_INFO, F("Restart inverter ") + String(iv->id) ); + iv->devControlRequest = true; + break; + case 12: // Reactive Power Control + // iv->devControlCmd = 12; + // uint16_t reactive_power = std::stoi(strtok(NULL, "/")); + // .../devcontrol/// + // ^^^ + DPRINTLN(DBG_INFO, F("Reactive Power Control not implemented for inverter ") + String(iv->id) ); + break; + case 13: // Set Power Factor + // iv->devControlCmd = 13; + // uint16_t power_factor = std::stoi(strtok(NULL, "/")); + // .../devcontrol/// + // ^^^ + DPRINTLN(DBG_INFO, F("Set Power Factor not implemented for inverter ") + String(iv->id) ); + break; + default: + DPRINTLN(DBG_INFO, "Not implemented"); + break; + } + } } - iv->devControlRequest = true; } break; } token = strtok(NULL, "/"); } + DPRINTLN(DBG_INFO, F("app::cbMqtt finished")); } @@ -747,6 +761,19 @@ void app::showStatistics(void) { mWeb->send(200, F("text/plain"), content); } +//----------------------------------------------------------------------------- +void app::devControl(void) { // ToDo + DPRINTLN(DBG_VERBOSE, F("app::devControl")); + if(mWeb->args() > 0) { + //mWeb->arg("ivid").toChar... + // get iv + // set devControl on/off/power limt --> integrate buttons in app::showLiveData + // ... + mWeb->send(200, F("text/html"), F("Command sent" + "

sent

")); + } +} + //----------------------------------------------------------------------------- void app::showHoymiles(void) { diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index 2f3d9e8e..a9309e00 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -71,6 +71,7 @@ class app : public Main { void showHoymiles(void); void showLiveData(void); void showJSON(void); + void devControl(void); void saveValues(bool webSend); diff --git a/tools/esp8266/hmInverter.h b/tools/esp8266/hmInverter.h index 213e8c66..b074284e 100644 --- a/tools/esp8266/hmInverter.h +++ b/tools/esp8266/hmInverter.h @@ -84,6 +84,7 @@ class Inverter { ts = 0; powerLimit = -1; // 65535 W Limit -> unlimited devControlRequest = false; + devControlCmd = 0xff; } ~Inverter() { diff --git a/tools/esp8266/hmRadio.h b/tools/esp8266/hmRadio.h index 1062664c..6ff3a834 100644 --- a/tools/esp8266/hmRadio.h +++ b/tools/esp8266/hmRadio.h @@ -166,7 +166,7 @@ class HmRadio { return mTxChLst[mTxChIdx]; }*/ - void sendControlPacket(uint64_t invId, uint16_t data, uint8_t cmd) { + void sendControlPacket(uint64_t invId, uint8_t cmd, uint16_t data) { DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendControlPacket")); // sendCmdPacket(invId, 0x51, 0x80, false); // 0x80 implementation as original DTU code sendCmdPacket(invId, 0x51, 0x81, false); @@ -176,18 +176,25 @@ class HmRadio { if (cmd == 11){ // 4 bytes control data // Power Limit fix point 10 eg. 30 W --> 0d300 = 0x012c - mTxBuf[10 + (++cnt)] = (data*10 >> 8) & 0xff; // 0x01 - mTxBuf[10 + (++cnt)] = (data*10 ) & 0xff; // 0x2c - // are these two bytes necessary? + // -1 = 0xffff --> no limit + if (data == 0xffff){ + data &= 0xffff; // ToDo: unlimit value is needed and is inverter specific! --> get it via RF from inverter or via user interface + } else { + data*= 10; + } + mTxBuf[10 + (++cnt)] = (data >> 8) & 0xff; // 0x01 + mTxBuf[10 + (++cnt)] = (data ) & 0xff; // 0x2c + // are these two bytes necessary? --> yes it seems so mTxBuf[10 + (++cnt)] = 0x00; mTxBuf[10 + (++cnt)] = 0x00; } // crc control data - uint16_t crc = crc16(&mTxBuf[10], 10 - (cnt+1)); + uint16_t crc = crc16(&mTxBuf[10], cnt+1); mTxBuf[10 + (++cnt)] = (crc >> 8) & 0xff; mTxBuf[10 + (++cnt)] = (crc ) & 0xff; // crc over all - mTxBuf[10 + (++cnt)] = crc8(mTxBuf, 18); + cnt +=1; + mTxBuf[10 + cnt] = crc8(mTxBuf, 10 + cnt); sendPacket(invId, mTxBuf, 10 + (++cnt), true); } diff --git a/tools/esp8266/main.cpp b/tools/esp8266/main.cpp index ffaa0a54..26a47605 100644 --- a/tools/esp8266/main.cpp +++ b/tools/esp8266/main.cpp @@ -71,6 +71,7 @@ void Main::setup(uint32_t timeout) { mUpdater->setup(mWeb); mApActive = startAp; + mStActive = !startAp; } @@ -121,6 +122,10 @@ void Main::loop(void) { stats(); }*/ } + if (WiFi.status() != WL_CONNECTED) { + DPRINTLN(DBG_INFO, "[WiFi]: Connection Lost"); + mStActive = false; + } } diff --git a/tools/esp8266/main.h b/tools/esp8266/main.h index 68f9df5c..98e9f01c 100644 --- a/tools/esp8266/main.h +++ b/tools/esp8266/main.h @@ -118,6 +118,7 @@ class Main { bool mWifiSettingsValid; bool mSettingsValid; bool mApActive; + bool mStActive; ESP8266WebServer *mWeb; char mVersion[9]; char mDeviceName[DEVNAME_LEN]; diff --git a/tools/esp8266/mqtt.h b/tools/esp8266/mqtt.h index 9e973162..a7ff526f 100644 --- a/tools/esp8266/mqtt.h +++ b/tools/esp8266/mqtt.h @@ -102,6 +102,7 @@ class mqtt { char topic[MQTT_TOPIC_LEN + 13 ]; // "/devcontrol/#" --> + 6 byte // ToDo: "/devcontrol/#" is hardcoded snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mTopic); + DPRINTLN(DBG_INFO, F("subscribe to ") + String(topic)); mClient->subscribe(topic); // subscribe to mTopic + "/devcontrol/#" } From 78d8c6991772c9dd171cf7f4f219c434182732b0 Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Fri, 5 Aug 2022 13:47:10 +0200 Subject: [PATCH 6/7] update revision --- tools/esp8266/defines.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index 9283a52e..76035dcb 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -20,8 +20,8 @@ // VERSION //------------------------------------- #define VERSION_MAJOR 0 -#define VERSION_MINOR 4 -#define VERSION_PATCH 26 +#define VERSION_MINOR 5 +#define VERSION_PATCH 1 //------------------------------------- From 56cc48106908b60d9a0fcf326185e89ec60b07f0 Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Fri, 5 Aug 2022 15:00:50 +0200 Subject: [PATCH 7/7] power limit persistent --- tools/esp8266/hmRadio.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/esp8266/hmRadio.h b/tools/esp8266/hmRadio.h index 41b0cc5d..da4c78ee 100644 --- a/tools/esp8266/hmRadio.h +++ b/tools/esp8266/hmRadio.h @@ -177,8 +177,8 @@ class HmRadio { } mTxBuf[10 + (++cnt)] = (data >> 8) & 0xff; // 0x01 mTxBuf[10 + (++cnt)] = (data ) & 0xff; // 0x2c - // are these two bytes necessary? --> yes it seems so - mTxBuf[10 + (++cnt)] = 0x00; + // mTxBuf[10 + (++cnt)] = 0x00; // not persistent + mTxBuf[10 + (++cnt)] = 0x01; // persistent mTxBuf[10 + (++cnt)] = 0x00; } // crc control data