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/app.cpp b/tools/esp8266/app.cpp index 6b40cf2c..31750269 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); @@ -81,8 +82,10 @@ 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); + 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++) { mEep->read(ADDR_INV_CH_NAME + (i * 4 * MAX_NAME_LENGTH) + j * MAX_NAME_LENGTH, iv->chName[j], MAX_NAME_LENGTH); } @@ -155,6 +158,7 @@ void app::setup(uint32_t timeout) { mqttPort = 1883; mMqtt.setup(mqttAddr, mqttTopic, mqttUser, mqttPwd, mqttDevName, mqttPort); + mMqtt.mClient->setCallback(std::bind(&app::cbMqtt, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); mMqttTicker = 0; mSerialTicker = 0; @@ -211,7 +215,7 @@ void app::setup(uint32_t timeout) { void app::loop(void) { DPRINTLN(DBG_VERBOSE, F("app::loop")); Main::loop(); - + mSys->Radio.loop(); yield(); @@ -238,7 +242,7 @@ void app::loop(void) { if(0 != len) { Inverter<> *iv = mSys->findInverter(&p->packet[1]); - if(NULL != iv) { + 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"); @@ -257,6 +261,23 @@ void app::loop(void) { } } } + 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. + } + } } } @@ -365,8 +386,14 @@ void app::loop(void) { if(mSerialDebug) DPRINTLN(DBG_DEBUG, F("app:loop WiFi WiFi.status ") + String(WiFi.status()) ); 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->devControlRequest){ + if(mSerialDebug) + DPRINTLN(DBG_INFO, F("Devcontrol request ") + String(iv->devControlCmd) + F(" power limit ") + String(iv->powerLimit)); + 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; + } } } else if(mSerialDebug) @@ -508,10 +535,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(""); @@ -525,6 +554,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++) { @@ -623,6 +658,77 @@ void app::showErase() { showReboot(); } +//----------------------------------------------------------------------------- +void app::cbMqtt(char* topic, byte* payload, unsigned int length) { + // callback handling on subscribed devcontrol topic + DPRINTLN(DBG_INFO, F("app::cbMqtt")); + // subcribed topics are mTopic + "/devcontrol/#" where # issent
")); + } +} + //----------------------------------------------------------------------------- void app::showHoymiles(void) { @@ -713,7 +832,7 @@ void app::showLiveData(void) { } modHtml += F("