Skip to content

Commit

Permalink
Merge pull request #5 from ohAnd/develop
Browse files Browse the repository at this point in the history
optimizing dtu update and reset timer after new power set
  • Loading branch information
ohAnd authored Mar 10, 2024
2 parents fad5e2e + 87c2abd commit 173f475
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 142 deletions.
4 changes: 2 additions & 2 deletions include/dtuInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ void writeReqRealDataNew(WiFiClient *localDtuClient, unsigned long locTimeSec);
void readRespGetConfig(WiFiClient *localDtuClient);
void writeReqGetConfig(WiFiClient *localDtuClient, unsigned long locTimeSec);

void readRespCommand(WiFiClient *localDtuClient);
void writeReqCommand(WiFiClient *localDtuClient, uint8_t setPercent, unsigned long locTimeSec);
boolean readRespCommand(WiFiClient *localDtuClient);
boolean writeReqCommand(WiFiClient *localDtuClient, uint8_t setPercent, unsigned long locTimeSec);


#endif // DTUINTERFACE_H
5 changes: 4 additions & 1 deletion include/index_html.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ const char INDEX_HTML[] PROGMEM = R"=====(
<script>
let timerRemainingProgess = 0;
const waitTime = 31000;
let waitTime = 31000;
let remainingTime = waitTime;
let timerInfoUpdate = 0;
Expand Down Expand Up @@ -536,6 +536,9 @@ const char INDEX_HTML[] PROGMEM = R"=====(
if (data.firmware.selectedUpdateChannel == 0) { $("#relChanStable").addClass("selected"); $("#relChanSnapshot").removeClass("selected"); }
else { $("#relChanStable").removeClass("selected"); $("#relChanSnapshot").addClass("selected"); }
// setting timer value according to user setting
waitTime = data.dtuConnection.dtuDataCycle * 1000;
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions include/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define VERSION "1.2.0"
#define BUILDTIME "08.03.2024 - 08:34:47"
#define BUILDTIMESTAMP "1709883287"
#define VERSION "1.3.0"
#define BUILDTIME "10.03.2024 - 09:23:07"
#define BUILDTIMESTAMP "1710058987"
8 changes: 4 additions & 4 deletions include/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "1.2.0",
"versiondate": "08.03.2024 - 08:34:47",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_1.2.0.bin",
"link": "https://github.com/ohAnd/dtuGateway//releases/latest/download/dtuGateway_release_1.2.0.bin"
"version": "1.3.0",
"versiondate": "10.03.2024 - 09:23:07",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_1.3.0.bin",
"link": "https://github.com/ohAnd/dtuGateway//releases/latest/download/dtuGateway_release_1.3.0.bin"
}
20 changes: 11 additions & 9 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ void initializeEEPROM()
strcpy(userConfig.wifiSsid, "mySSID");
strcpy(userConfig.wifiPassword, "myPassword");
strcpy(userConfig.dtuHostIp, "192.168.0.254");
strcpy(userConfig.openhabHostIp, "192.168.1.30");
strcpy(userConfig.openhabHostIp, "192.168.1.100");
strcpy(userConfig.openItemPrefix, "inverter");
userConfig.selectedUpdateChannel = 0; // default - release channel
userConfig.dtuCloudPauseActive = 1;
userConfig.dtuCloudPauseTime = 40;
userConfig.dtuUpdateTime = 31;
userConfig.wifiAPstart = true;

// Mark EEPROM as initialized
Expand All @@ -64,15 +66,15 @@ void printEEPROMdata()
// Configuration has been written before
Serial.print(F("\n--------------------------------------\n"));
Serial.print(F("Configuration loaded from EEPROM:\n"));
Serial.print(F("init phase: \t"));
Serial.print(F("init phase: \t\t"));
Serial.println(userConfig.wifiAPstart);

Serial.print(F("wifi ssid: \t"));
Serial.print(F("wifi ssid: \t\t"));
Serial.println(userConfig.wifiSsid);
Serial.print(F("wifi pass: \t"));
Serial.print(F("wifi pass: \t\t"));
Serial.println(userConfig.wifiPassword);

Serial.print(F("openhab host: \t"));
Serial.print(F("openhab host: \t\t"));
Serial.println(userConfig.openhabHostIp);

Serial.print(F("openhab item prefix: \t"));
Expand All @@ -87,14 +89,14 @@ void printEEPROMdata()
Serial.print(F("cloud pause time: \t"));
Serial.println(userConfig.dtuCloudPauseTime);

Serial.print(F("update channel: \t"));
Serial.print(F("update channel: \t\t"));
Serial.println(userConfig.selectedUpdateChannel);

Serial.print(F("dtu host: \t"));
Serial.print(F("dtu host: \t\t"));
Serial.println(userConfig.dtuHostIp);
Serial.print(F("dtu ssid: \t"));
Serial.print(F("dtu ssid: \t\t"));
Serial.println(userConfig.dtuSsid);
Serial.print(F("dtu pass: \t"));
Serial.print(F("dtu pass: \t\t"));
Serial.println(userConfig.dtuPassword);
Serial.print(F("--------------------------------------\n"));
}
Expand Down
187 changes: 101 additions & 86 deletions src/dtuGateway.ino
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ boolean scanNetworksResult(int networksFound)
if (networksFound > 0)
{
Serial.print(F("\nscan for wifi networks done: "));
Serial.println(String(networksFound) + " wifi's found\n");
Serial.println(String(networksFound) + " wifi's found");
networkCount = networksFound;
foundNetworks = "[";
for (int i = 0; i < networksFound; i++)
Expand Down Expand Up @@ -348,17 +348,17 @@ void handleUpdateWifiSettings()

void handleUpdateDtuSettings()
{
String dtuHostIpUser = server.arg("dtuHostIpSend"); // retrieve message from webserver
String dtuDataCycle = server.arg("dtuDataCycleSend"); // retrieve message from webserver
String dtuHostIpUser = server.arg("dtuHostIpSend"); // retrieve message from webserver
String dtuDataCycle = server.arg("dtuDataCycleSend"); // retrieve message from webserver
String dtuCloudPause = server.arg("dtuCloudPauseSend"); // retrieve message from webserver
String dtuSSIDUser = server.arg("dtuSsidSend"); // retrieve message from webserver
String dtuPassUser = server.arg("dtuPasswordSend"); // retrieve message from webserver
String dtuSSIDUser = server.arg("dtuSsidSend"); // retrieve message from webserver
String dtuPassUser = server.arg("dtuPasswordSend"); // retrieve message from webserver
Serial.println("\nhandleUpdateDtuSettings - got dtu ip: " + dtuHostIpUser + "- got dtuDataCycle: " + dtuDataCycle + "- got dtu dtuCloudPause: " + dtuCloudPause);
Serial.println("handleUpdateDtuSettings - got dtu ssid: " + dtuSSIDUser + " - got WifiPass: " + dtuPassUser);

dtuHostIpUser.toCharArray(userConfig.dtuHostIp, sizeof(userConfig.dtuHostIp));
userConfig.dtuUpdateTime = dtuDataCycle.toInt();
if(dtuCloudPause)
if (dtuCloudPause)
userConfig.dtuCloudPauseActive = true;
else
userConfig.dtuCloudPauseActive = false;
Expand All @@ -371,8 +371,7 @@ void handleUpdateDtuSettings()
intervalMid = userConfig.dtuUpdateTime;
dtuConnection.preventCloudErrors = userConfig.dtuCloudPauseActive;
Serial.println("\nhandleUpdateDtuSettings - setting dtu cycle to:" + String(intervalMid));



String JSON = "{";
JSON = JSON + "\"dtuHostIp\": \"" + userConfig.dtuHostIp + "\",";
JSON = JSON + "\"dtuSsid\": \"" + userConfig.dtuSsid + "\",";
Expand Down Expand Up @@ -749,6 +748,42 @@ String getMessageFromOpenhab(String key)
}
}

boolean getPowerSetDataFromOpenHab()
{
// get data from openhab if connected to DTU
if (dtuConnection.dtuConnectState == DTU_STATE_CONNECTED)
{
uint8_t gotLimit;
bool conversionSuccess = false;

String openhabMessage = getMessageFromOpenhab(String(userConfig.openItemPrefix) + "_PowerLimit_Set");
if (openhabMessage.length() > 0)
{
gotLimit = openhabMessage.toInt();
// Check if the conversion was successful by comparing the string with its integer representation, to avoid wronmg interpretations of 0 after toInt by a "no number string"
conversionSuccess = (String(gotLimit) == openhabMessage);
}

if (conversionSuccess)
{
if (gotLimit < 2)
globalData.powerLimitSet = 2;
else if (gotLimit > 100)
globalData.powerLimitSet = 2;
else
globalData.powerLimitSet = gotLimit;
}
else
{
Serial.print("got wrong data for SetLimit: " + openhabMessage);
return false;
}
// Serial.print("got SetLimit: " + String(globalData.powerLimitSet) + " - current limit: " + String(globalData.powerLimit) + " %");
return true;
}
return false;
}

boolean updateValueToOpenhab()
{
boolean sendOk = postMessageToOpenhab(String(userConfig.openItemPrefix) + "Grid_U", (String)globalData.grid.voltage);
Expand Down Expand Up @@ -784,6 +819,7 @@ boolean updateValueToOpenhab()
postMessageToOpenhab(String(userConfig.openItemPrefix) + "_PowerLimit", (String)globalData.powerLimit);
postMessageToOpenhab(String(userConfig.openItemPrefix) + "_WifiRSSI", (String)globalData.dtuRssi);
}
Serial.println("\nsent values to openHAB");
return true;
}

Expand Down Expand Up @@ -1105,6 +1141,46 @@ void getSerialCommand(String cmd, String value)

// main

void getDtuDataUpdate(WiFiClient *localDtuClient)
{
if (localDtuClient->connected())
{
writeReqRealDataNew(localDtuClient, localTimeSecond);
// writeReqAppGetHistPower(&dtuClient); // only needed for sum energy daily/ total - but can lead to overflow for history data/ prevent maybe cloud update
writeReqGetConfig(localDtuClient, localTimeSecond);
// dtuClient.stop();
// dtuConnection.dtuConnectState = DTU_STATE_OFFLINE;

// check for up-to-date - last response timestamp have to not equal the current response timestamp
if ((globalData.lastRespTimestamp != globalData.respTimestamp) && (globalData.respTimestamp != 0))
{
globalData.uptodate = true;
dtuConnection.dtuErrorState = DTU_ERROR_NO_ERROR;
// sync localTimeSecond to DTU time, only if abbrevation about 3 seconds
if (abs((int(globalData.respTimestamp) - int(localTimeSecond))) > 3)
{
localTimeSecond = globalData.respTimestamp;
Serial.print(F("\n>--> synced local time with DTU time <--<\n"));
}
}
else
{
globalData.uptodate = false;
dtuConnection.dtuErrorState = DTU_ERROR_TIME_DIFF;
dtuConnection.dtuConnectState = DTU_STATE_TRY_RECONNECT;
localDtuClient->stop(); // stopping connection to DTU when response time error - try with reconnect
}
globalData.lastRespTimestamp = globalData.respTimestamp;
}
else
{
globalData.uptodate = false;
dtuConnection.dtuErrorState = DTU_ERROR_NO_ERROR;
dtuConnection.dtuConnectState = DTU_STATE_TRY_RECONNECT;
dtuClient.stop();
}
}

// get precise localtime - increment
void IRAM_ATTR timer1000MilliSeconds()
{
Expand Down Expand Up @@ -1158,16 +1234,17 @@ void loop()
WiFi.disconnect();
}

if (dtuClient.connected())
// direct request of new powerLimit
if (globalData.powerLimitSet != globalData.powerLimit && globalData.powerLimitSet != 101 && globalData.uptodate)
{
// direct request of new powerLimit
if (globalData.powerLimitSet != globalData.powerLimit && globalData.powerLimitSet != 101 && globalData.uptodate)
Serial.print("\n----- ----- set new power limit from " + String(globalData.powerLimit) + " to " + String(globalData.powerLimitSet));
if (writeReqCommand(&dtuClient, globalData.powerLimitSet, localTimeSecond))
{
writeReqCommand(&dtuClient, globalData.powerLimitSet, localTimeSecond);
Serial.print("\nsetted new power limit from " + String(globalData.powerLimit) + " to " + String(globalData.powerLimitSet) + "\n");
writeReqRealDataNew(&dtuClient, localTimeSecond);
writeReqGetConfig(&dtuClient, localTimeSecond); // get approval of setting new value
updateValueToOpenhab();
Serial.print(" --- done");
// set next normal request in 5 seconds from now on
previousMillisMid = currentMillis - (intervalMid - 5);
} else {
Serial.print(" --- error");
}
}
}
Expand All @@ -1177,7 +1254,8 @@ void loop()
{
Serial.printf("\n>>>>> %02is task - state --> ", int(interval5000ms));
Serial.print("local: " + getTimeStringByTimestamp(localTimeSecond));
Serial.print(" --- NTP: " + timeClient.getFormattedTime() + " --- currentMillis " + String(currentMillis) + " --- ");
Serial.print(" --- NTP: " + timeClient.getFormattedTime());
// Serial.print(" --- currentMillis " + String(currentMillis) + " --- ");
previousMillis5000ms = currentMillis;
// -------->
if (WiFi.status() == WL_CONNECTED)
Expand All @@ -1188,37 +1266,9 @@ void loop()
if (wifiPercent > 100)
wifiPercent = 100;
globalData.wifi_rssi_gateway = wifiPercent;
Serial.print("RSSI to AP: '" + String(WiFi.SSID()) + "': " + String(globalData.wifi_rssi_gateway) + " %");

// get data from openhab if not connected to DTU
if (dtuConnection.dtuConnectState == DTU_STATE_CONNECTED)
{
uint8_t gotLimit;
bool conversionSuccess = false;
Serial.print(" --- RSSI to AP: '" + String(WiFi.SSID()) + "': " + String(globalData.wifi_rssi_gateway) + " %");

String openhabMessage = getMessageFromOpenhab(String(userConfig.openItemPrefix) + "_PowerLimit_Set");
if (openhabMessage.length() > 0)
{
gotLimit = openhabMessage.toInt();
// Check if the conversion was successful by comparing the string with its integer representation, to avoid wronmg interpretations of 0 after toInt by a "no number string"
conversionSuccess = (String(gotLimit) == openhabMessage);
}

if (conversionSuccess)
{
if (gotLimit < 2)
globalData.powerLimitSet = 2;
else if (gotLimit > 100)
globalData.powerLimitSet = 2;
else
globalData.powerLimitSet = gotLimit;
}
else
{
Serial.print("got wrong data for SetLimit: " + openhabMessage);
}
// Serial.print("got SetLimit: " + String(globalData.powerLimitSet) + " - current limit: " + String(globalData.powerLimit) + " %");
}
getPowerSetDataFromOpenHab();
}
}

Expand Down Expand Up @@ -1249,42 +1299,7 @@ void loop()
}
}

if (dtuClient.connected())
{
writeReqRealDataNew(&dtuClient, localTimeSecond);
// writeReqAppGetHistPower(&dtuClient); // only needed for sum energy daily/ total - but can lead to overflow for history data/ prevent maybe cloud update
writeReqGetConfig(&dtuClient, localTimeSecond);
// dtuClient.stop();
// dtuConnection.dtuConnectState = DTU_STATE_OFFLINE;

// check for up-to-date - last response timestamp have to not equal the current response timestamp
if ((globalData.lastRespTimestamp != globalData.respTimestamp) && (globalData.respTimestamp != 0))
{
globalData.uptodate = true;
dtuConnection.dtuErrorState = DTU_ERROR_NO_ERROR;
// sync localTimeSecond to DTU time, only if abbrevation about 3 seconds
if (abs((int(globalData.respTimestamp) - int(localTimeSecond))) > 3)
{
localTimeSecond = globalData.respTimestamp;
Serial.print(F("\n>--> synced local time with DTU time <--<\n"));
}
}
else
{
globalData.uptodate = false;
dtuConnection.dtuErrorState = DTU_ERROR_TIME_DIFF;
dtuConnection.dtuConnectState = DTU_STATE_TRY_RECONNECT;
dtuClient.stop(); // stopping connection to DTU when response time error - try with reconnect
}
globalData.lastRespTimestamp = globalData.respTimestamp;
}
else
{
globalData.uptodate = false;
dtuConnection.dtuErrorState = DTU_ERROR_NO_ERROR;
dtuConnection.dtuConnectState = DTU_STATE_TRY_RECONNECT;
dtuClient.stop();
}
getDtuDataUpdate(&dtuClient);

if ((globalControls.getDataAuto || globalControls.getDataOnce) && !dtuConnection.dtuActiveOffToCloudUpdate && globalData.uptodate)
{
Expand Down Expand Up @@ -1322,10 +1337,10 @@ void loop()
}
else
{
Serial.print("\n\nupdate at remote: " + getTimeStringByTimestamp(globalData.respTimestamp) + " - uptodate: " + String(globalData.uptodate) + " \n");
Serial.print("\n+++ update at remote: " + getTimeStringByTimestamp(globalData.respTimestamp) + " - uptodate: " + String(globalData.uptodate) + " --- ");
Serial.print("wifi rssi: " + String(globalData.dtuRssi) + " % (DTU->Cloud) - " + String(globalData.wifi_rssi_gateway) + " % (Client->AP) \n");
Serial.print("power limit (set): " + String(globalData.powerLimit) + " % (" + String(globalData.powerLimitSet) + " %) \n");
Serial.print("inverter temp:\t " + String(globalData.inverterTemp) + " °C \n");
Serial.print("power limit (set): " + String(globalData.powerLimit) + " % (" + String(globalData.powerLimitSet) + " %) --- ");
Serial.print("inverter temp: " + String(globalData.inverterTemp) + " °C \n");

Serial.print(F(" \t |\t current |\t voltage |\t power | daily | total |\n"));
// 12341234 |1234 current |1234 voltage |1234 power1234|12341234daily 1234|12341234total 1234|
Expand Down
Loading

0 comments on commit 173f475

Please sign in to comment.