devControl bugs and error handling

This commit is contained in:
Andreas Schiffler 2022-08-05 13:20:12 +02:00
parent 7ed5a0a9e1
commit 6b1f027d01
7 changed files with 111 additions and 68 deletions

View file

@ -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 <inverter_id>/<subcmd in dec>/<value in dec>
// eg. mypvsolar/devcontrol/1/11/400 --> inverter 1 active power limit 400 Watt
char *token = strtok(topic, "/");
// subcribed topics are mTopic + "/devcontrol/#" where # is <inverter_id>/<subcmd in dec>
// eg. mypvsolar/devcontrol/1/11 with payload "400" --> inverter 1 active power limit 400 Watt
const char *token = strtok(topic, "/");
while (token != NULL)
{
// .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec>
// ^^^
if (strcmp(token,"devcontrol")){
Inverter<> *iv = this->mSys->getInverterByPos(std::stoi(strtok(NULL, "/")));
// .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec>
// ^^^
if(NULL != iv) {
// switch case subcmd
switch ( std::stoi(strtok(NULL, "/")) ){
// .../devcontrol/<inverter_id as int>/<subcmd in dec>/<value in dec>
// ^^^^
case 11: // Active Power Control
iv->devControlCmd = 11;
iv->powerLimit = std::stoi(strtok(NULL, "/"));
// .../devcontrol/<inverter_id>/<subcmd in dec>/<value in dec>
// ^^^
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/<inverter_id>/<subcmd in dec>/<value in dec>
// ^^^
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/<inverter_id>/<subcmd in dec>/<value in dec>
// ^^^
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/<inverter_id>/<subcmd in dec>/<value in dec>
// ^^^
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/<inverter_id>/<subcmd in dec>/<value in dec>
// ^^^
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("<!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) {