Browse Source

devControl bugs and error handling

pull/124/head
Andreas Schiffler 2 years ago
parent
commit
6b1f027d01
  1. 149
      tools/esp8266/app.cpp
  2. 1
      tools/esp8266/app.h
  3. 1
      tools/esp8266/hmInverter.h
  4. 19
      tools/esp8266/hmRadio.h
  5. 5
      tools/esp8266/main.cpp
  6. 1
      tools/esp8266/main.h
  7. 1
      tools/esp8266/mqtt.h

149
tools/esp8266/app.cpp

@ -62,6 +62,7 @@ void app::setup(uint32_t timeout) {
mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this)); mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this));
mWeb->on("/livedata", std::bind(&app::showLiveData, this)); mWeb->on("/livedata", std::bind(&app::showLiveData, this));
mWeb->on("/json", std::bind(&app::showJSON, this)); mWeb->on("/json", std::bind(&app::showJSON, this));
mWeb->on("/devcontrol", std::bind(&app::devControl, this));
if(mSettingsValid) { if(mSettingsValid) {
mEep->read(ADDR_INV_INTERVAL, &mSendInterval); mEep->read(ADDR_INV_INTERVAL, &mSendInterval);
@ -82,6 +83,7 @@ void app::setup(uint32_t timeout) {
iv = mSys->addInverter(name, invSerial, modPwr); iv = mSys->addInverter(name, invSerial, modPwr);
if(NULL != iv) { if(NULL != iv) {
mEep->read(ADDR_INV_PWR_LIM + (i * 2),&iv->powerLimit); 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 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)); 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++) { for(uint8_t j = 0; j < 4; j++) {
@ -217,8 +219,6 @@ void app::loop(void) {
bool rxRdy = mSys->Radio.switchRxCh(); bool rxRdy = mSys->Radio.switchRxCh();
char respTopic[64];
if(!mSys->BufCtrl.empty()) { if(!mSys->BufCtrl.empty()) {
uint8_t len; uint8_t len;
packet_t *p = mSys->BufCtrl.getBack(); 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 if(NULL != iv && p->packet[0] == (0x51 + 0x80)) { // response from dev control command
snprintf(respTopic, 64, "%s/devcontrol/%d/resp", mMqtt.getTopic(), iv->id); DPRINTLN(DBG_INFO, F("Response from devcontrol received"));
mMqtt.sendMsg2(respTopic,(const char*)p->packet,false); 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(iv->devControlRequest){
if(mSerialDebug) if(mSerialDebug)
DPRINTLN(DBG_INFO, F("Devcontrol request ") + String(iv->devControlCmd) + F(" power limit ") + String(iv->powerLimit)); 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); mSys->Radio.sendControlPacket(iv->radioId.u64,iv->devControlCmd ,uint16_t(iv->powerLimit));
// ToDo: Only set Request to false if succesful executed
iv->devControlRequest = false;
// ^^
} else { } else {
mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts); mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts);
mRxTicker = 0; mRxTicker = 0;
@ -633,66 +643,70 @@ void app::showErase() {
void app::cbMqtt(char* topic, byte* payload, unsigned int length) { void app::cbMqtt(char* topic, byte* payload, unsigned int length) {
// callback handling on subscribed devcontrol topic // callback handling on subscribed devcontrol topic
DPRINTLN(DBG_INFO, F("app::cbMqtt")); DPRINTLN(DBG_INFO, F("app::cbMqtt"));
DPRINTLN(DBG_INFO, topic); // subcribed topics are mTopic + "/devcontrol/#" where # is <inverter_id>/<subcmd in dec>
// subcribed topics are mTopic + "/devcontrol/#" where # is <inverter_id>/<subcmd in dec>/<value in dec> // eg. mypvsolar/devcontrol/1/11 with payload "400" --> inverter 1 active power limit 400 Watt
// eg. mypvsolar/devcontrol/1/11/400 --> inverter 1 active power limit 400 Watt const char *token = strtok(topic, "/");
char *token = strtok(topic, "/");
while (token != NULL) while (token != NULL)
{ {
// .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec> if (std::strcmp(token,"devcontrol")==0){
// ^^^ token = strtok(NULL, "/");
if (strcmp(token,"devcontrol")){ uint8_t iv_id = std::stoi(token);
Inverter<> *iv = this->mSys->getInverterByPos(std::stoi(strtok(NULL, "/"))); if (iv_id >= 0 && iv_id <= MAX_NUM_INVERTERS){
// .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec> Inverter<> *iv = this->mSys->getInverterByPos(iv_id);
// ^^^ if(NULL != iv) {
if(NULL != iv) { if (!iv->devControlRequest) { // still pending
// switch case subcmd token = strtok(NULL, "/");
switch ( std::stoi(strtok(NULL, "/")) ){ uint8_t subcmd = std::stoi(token);
// .../devcontrol/<inverter_id as int>/<subcmd in dec>/<value in dec> switch ( subcmd ){
// ^^^^ case 11: // Active Power Control
case 11: // Active Power Control if (true){ // if (std::stoi((char*)payload) > 0) error handling powerlimit needed?
iv->devControlCmd = 11; iv->devControlCmd = 11;
iv->powerLimit = std::stoi(strtok(NULL, "/")); iv->powerLimit = std::stoi((char*)payload);
// .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec> DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit) + F("W") );
// ^^^ }
mEep->write(ADDR_INV_PWR_LIM + iv->id * 2,iv->powerLimit); iv->devControlRequest = true;
// updateCrc(); break;
// mEep->commit(); case 0: // Turn On
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit) + F("W") ); iv->devControlCmd = 0;
break; DPRINTLN(DBG_INFO, F("Turn on inverter ") + String(iv->id) );
case 0: // Turn On iv->devControlRequest = true;
iv->devControlCmd = 0; break;
DPRINTLN(DBG_INFO, F("Turn on inverter ") + String(iv->id) ); case 1: // Turn Off
break; iv->devControlCmd = 1;
case 1: // Turn Off DPRINTLN(DBG_INFO, F("Turn off inverter ") + String(iv->id) );
iv->devControlCmd = 1; iv->devControlRequest = true;
DPRINTLN(DBG_INFO, F("Turn off inverter ") + String(iv->id) ); break;
break; case 2: // Restart
case 2: // Restart iv->devControlCmd = 2;
iv->devControlCmd = 2; DPRINTLN(DBG_INFO, F("Restart inverter ") + String(iv->id) );
DPRINTLN(DBG_INFO, F("Restart inverter ") + String(iv->id) ); iv->devControlRequest = true;
break; break;
case 12: // Reactive Power Control case 12: // Reactive Power Control
// iv->devControlCmd = 12; // iv->devControlCmd = 12;
// uint16_t reactive_power = std::stoi(strtok(NULL, "/")); // uint16_t reactive_power = std::stoi(strtok(NULL, "/"));
// .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec> // .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec>
// ^^^ // ^^^
DPRINTLN(DBG_INFO, F("Reactive Power Control not implemented for inverter ") + String(iv->id) ); DPRINTLN(DBG_INFO, F("Reactive Power Control not implemented for inverter ") + String(iv->id) );
break; break;
case 13: // Set Power Factor case 13: // Set Power Factor
// iv->devControlCmd = 13; // iv->devControlCmd = 13;
// uint16_t power_factor = std::stoi(strtok(NULL, "/")); // uint16_t power_factor = std::stoi(strtok(NULL, "/"));
// .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec> // .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec>
// ^^^ // ^^^
DPRINTLN(DBG_INFO, F("Set Power Factor not implemented for inverter ") + String(iv->id) ); DPRINTLN(DBG_INFO, F("Set Power Factor not implemented for inverter ") + String(iv->id) );
break; break;
default:
DPRINTLN(DBG_INFO, "Not implemented");
break;
}
}
} }
iv->devControlRequest = true;
} }
break; break;
} }
token = strtok(NULL, "/"); 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); 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("<!doctype html><html><head><title>Command sent</title><meta http-equiv=\"refresh\" content=\"2; URL=/hoymiles\"></head><body>"
"<p>sent</p></body></html>"));
}
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::showHoymiles(void) { void app::showHoymiles(void) {

1
tools/esp8266/app.h

@ -71,6 +71,7 @@ class app : public Main {
void showHoymiles(void); void showHoymiles(void);
void showLiveData(void); void showLiveData(void);
void showJSON(void); void showJSON(void);
void devControl(void);
void saveValues(bool webSend); void saveValues(bool webSend);

1
tools/esp8266/hmInverter.h

@ -84,6 +84,7 @@ class Inverter {
ts = 0; ts = 0;
powerLimit = -1; // 65535 W Limit -> unlimited powerLimit = -1; // 65535 W Limit -> unlimited
devControlRequest = false; devControlRequest = false;
devControlCmd = 0xff;
} }
~Inverter() { ~Inverter() {

19
tools/esp8266/hmRadio.h

@ -166,7 +166,7 @@ class HmRadio {
return mTxChLst[mTxChIdx]; 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")); DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendControlPacket"));
// sendCmdPacket(invId, 0x51, 0x80, false); // 0x80 implementation as original DTU code // sendCmdPacket(invId, 0x51, 0x80, false); // 0x80 implementation as original DTU code
sendCmdPacket(invId, 0x51, 0x81, false); sendCmdPacket(invId, 0x51, 0x81, false);
@ -176,18 +176,25 @@ class HmRadio {
if (cmd == 11){ if (cmd == 11){
// 4 bytes control data // 4 bytes control data
// Power Limit fix point 10 eg. 30 W --> 0d300 = 0x012c // Power Limit fix point 10 eg. 30 W --> 0d300 = 0x012c
mTxBuf[10 + (++cnt)] = (data*10 >> 8) & 0xff; // 0x01 // -1 = 0xffff --> no limit
mTxBuf[10 + (++cnt)] = (data*10 ) & 0xff; // 0x2c if (data == 0xffff){
// are these two bytes necessary? 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;
mTxBuf[10 + (++cnt)] = 0x00; mTxBuf[10 + (++cnt)] = 0x00;
} }
// crc control data // 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 >> 8) & 0xff;
mTxBuf[10 + (++cnt)] = (crc ) & 0xff; mTxBuf[10 + (++cnt)] = (crc ) & 0xff;
// crc over all // crc over all
mTxBuf[10 + (++cnt)] = crc8(mTxBuf, 18); cnt +=1;
mTxBuf[10 + cnt] = crc8(mTxBuf, 10 + cnt);
sendPacket(invId, mTxBuf, 10 + (++cnt), true); sendPacket(invId, mTxBuf, 10 + (++cnt), true);
} }

5
tools/esp8266/main.cpp

@ -71,6 +71,7 @@ void Main::setup(uint32_t timeout) {
mUpdater->setup(mWeb); mUpdater->setup(mWeb);
mApActive = startAp; mApActive = startAp;
mStActive = !startAp;
} }
@ -121,6 +122,10 @@ void Main::loop(void) {
stats(); stats();
}*/ }*/
} }
if (WiFi.status() != WL_CONNECTED) {
DPRINTLN(DBG_INFO, "[WiFi]: Connection Lost");
mStActive = false;
}
} }

1
tools/esp8266/main.h

@ -118,6 +118,7 @@ class Main {
bool mWifiSettingsValid; bool mWifiSettingsValid;
bool mSettingsValid; bool mSettingsValid;
bool mApActive; bool mApActive;
bool mStActive;
ESP8266WebServer *mWeb; ESP8266WebServer *mWeb;
char mVersion[9]; char mVersion[9];
char mDeviceName[DEVNAME_LEN]; char mDeviceName[DEVNAME_LEN];

1
tools/esp8266/mqtt.h

@ -102,6 +102,7 @@ class mqtt {
char topic[MQTT_TOPIC_LEN + 13 ]; // "/devcontrol/#" --> + 6 byte char topic[MQTT_TOPIC_LEN + 13 ]; // "/devcontrol/#" --> + 6 byte
// ToDo: "/devcontrol/#" is hardcoded // ToDo: "/devcontrol/#" is hardcoded
snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mTopic); snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mTopic);
DPRINTLN(DBG_INFO, F("subscribe to ") + String(topic));
mClient->subscribe(topic); // subscribe to mTopic + "/devcontrol/#" mClient->subscribe(topic); // subscribe to mTopic + "/devcontrol/#"
} }

Loading…
Cancel
Save