Skip to content

Commit

Permalink
fixing #59 and smaller changes
Browse files Browse the repository at this point in the history
fixing #59 and smaller changes around this topic incl. breakable change openhab item name ..._PowerLimit_Set to ..._PowerLimitSet
  • Loading branch information
ohAnd authored Oct 27, 2024
2 parents 7c90095 + 97c63c4 commit fa93864
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 49 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: "v${{ env.CURRENT_VERSIONNUMBER }}"
draft: true
prerelease: false
title: "dtuGateway v${{ env.CURRENT_VERSIONNUMBER }}"
files: |
Expand Down
6 changes: 3 additions & 3 deletions include/base/platformData.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ struct baseDataStruct

#if defined(ESP8266)
String chipType = "ESP8266";
#warning "setting chipType for ESP8266"
// #warning "setting chipType for ESP8266"
#elif CONFIG_IDF_TARGET_ESP32
String chipType = "ESP32";
#warning "setting chipType for ESP32"
// #warning "setting chipType for ESP32"
#elif CONFIG_IDF_TARGET_ESP32S2
String chipType = "ESP32 S2 (LOLIN S2 Mini)";
#warning "setting chipType for ESP32S2"
// #warning "setting chipType for ESP32S2"
#endif

const char *fwVersion = VERSION;
Expand Down
1 change: 1 addition & 0 deletions include/dtuInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct inverterData
float inverterTemp = 0;
uint8_t powerLimit = 254;
uint8_t powerLimitSet = 101; // init with not possible value for startup
boolean powerLimitSetUpdate = false;
uint32_t dtuRssi = 0;
uint32_t wifi_rssi_gateway = 0;
uint32_t respTimestamp = 1704063600; // init with start time stamp > 0
Expand Down
5 changes: 4 additions & 1 deletion include/mqttHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <PubSubClient.h>
#include <WiFiClientSecure.h>

#include <base/platformData.h>

// MQTT_CONNECTION_TIMEOUT (-4): The server didn't respond within the keep-alive time.
// MQTT_CONNECTION_LOST (-3): The network connection was broken.
// MQTT_CONNECT_FAILED (-2): The network connection failed.
Expand Down Expand Up @@ -56,14 +58,15 @@ class MQTTHandler {
MQTTHandler(const char *broker, int port, const char *user, const char *password, bool useTLS);
void setup();
void loop();
void publishDiscoveryMessage(const char *entity, const char *entityName, const char *unit, bool deleteMessage, const char *icon=NULL, const char *deviceClass=NULL);
void publishDiscoveryMessage(const char *entity, const char *entityName, const char *unit, bool deleteMessage, const char *icon=NULL, const char *deviceClass=NULL, boolean diagnostic=false);
void publishStandardData(String entity, String value);

// Setters for runtime configuration
void setBroker(const char* broker);
void setPort(int port);
void setUser(const char* user);
void setPassword(const char* password);
void setAutoDiscovery(boolean autoDiscovery);
void setUseTLS(bool useTLS);
void setConfiguration(const char *broker, int port, const char *user, const char *password, bool useTLS, const char *sensorUniqueName, const char *mainTopicPath, bool autoDiscovery, const char * ipAddress);
void setMainTopic(String mainTopicPath);
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 "2.0.189_localDev"
#define BUILDTIME "16.10.2024 - 23:11:41"
#define BUILDTIMESTAMP "1729113101"
#define VERSION "2.0.238_localDev"
#define BUILDTIME "24.10.2024 - 06:55:10"
#define BUILDTIMESTAMP "1729745710"
8 changes: 4 additions & 4 deletions include/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "2.0.189_localDev",
"versiondate": "16.10.2024 - 23:11:41",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_2.0.189_localDev.bin",
"link": "https://github.com/ohAnd/dtuGateway/releases/latest/download/dtuGateway_release_2.0.189_localDev.bin"
"version": "2.0.238_localDev",
"versiondate": "24.10.2024 - 06:55:10",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_2.0.238_localDev.bin",
"link": "https://github.com/ohAnd/dtuGateway/releases/latest/download/dtuGateway_release_2.0.238_localDev.bin"
}
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ So I decided to put this abstraction in an **ESP8266** to have a stable abstract
- set the IP to your openhab instance - data will be read with http://<your_openhab_ip>:8080/rest/items/<itemName>/state
- set the prefix ( \<openItemPrefix\> ) of your openhab items
- list of items that should be available in your openhab config
- read your given power set value from openhab with "<yourOpenItemPrefix>_PowerLimit_Set"
- read your given power set value from openhab with "<yourOpenItemPrefix>_PowerLimitSet"
- set openhab items with data from dtu:
<details>
<summary>expand to see to details</summary>
Expand Down Expand Up @@ -331,9 +331,9 @@ So I decided to put this abstraction in an **ESP8266** to have a stable abstract
- set the IP to your MQTT broker
- set the MQTT user and MQTT password
- set the main topic e.g. 'dtuGateway_12345678' for the pubished data (default: is `dtuGateway_<ESP chip id>` and has to be unique in your environment)
- [2024-06-29 still in development] ~~choosing unsecure or TLS based connection to your MQTT broker~~
- choosing unsecure or TLS based connection to your MQTT broker (only ESP32)
- to set the Power Limit from your environment
- you have to publish to `<main topic>/inverter/PowerLimit_Set` a value between 2...100 (possible range at DTU)
- you have to publish to `<main topic>/inverter/PowerLimitSet` a value between 2...100 (possible range at DTU)
- the incoming value will be checked for this interval and locally corrected to 2 or 100 if exceeds
- with retain flag, to get the last set value after restart / reconnect of the dtuGateway
- data will be published as following ('dtuGateway_12345678' is configurable in the settings):
Expand Down Expand Up @@ -363,7 +363,7 @@ So I decided to put this abstraction in an **ESP8266** to have a stable abstract
dtuGateway_12345678/inverter/Temp
dtuGateway_12345678/inverter/PowerLimit
dtuGateway_12345678/inverter/PowerLimit_Set // <-- this topic will be subscribed to get the power limit to set from your broker
dtuGateway_12345678/inverter/PowerLimitSet // <-- this topic (extended with ../set) will be subscribed to get the power limit to set from your broker
dtuGateway_12345678/inverter/WifiRSSI
```
</details>
Expand All @@ -376,9 +376,11 @@ So I decided to put this abstraction in an **ESP8266** to have a stable abstract
- if you use the default main topic e.g. `dtuGateway_<ESP chip id>` then config and state topic will be placed at the same standard HA auto discovery path, e.g. for panel 0 voltage
- config: `homeassistant/sensor/dtuGateway_12345678/pv0_U/config`
- state: `homeassistant/sensor/dtuGateway_12345678/pv0_U/state`
- set: `homeassistant/number/dtuGateway_12345678/inverter_PowerLimitSet/set`
- if you choose another location for the main topic path (let's assume 'myDTU_1') then it will looks like this on your broker, e.g.
- config: `homeassistant/sensor/dtuGateway_12345678/pv0_U/config`
- state: `myDTU_1/pv0/U` - this path will be integrated in the config message and with this HA will be informed to get the data value from right location
- set: `myDTU_1/inverter/PowerLimitSet/set`

## known bugs
- sometimes out-of-memory resets with instant reboots (rare after some hours or more often after some days)
Expand Down
10 changes: 6 additions & 4 deletions src/base/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,18 @@ void DTUwebserver::handleUpdateBindingsSettings(AsyncWebServerRequest *request)
if (userConfig.mqttActive)
{
// changing to given mqtt setting - inlcuding reset the connection
mqttHandler.setConfiguration(userConfig.mqttBrokerIpDomain, userConfig.mqttBrokerPort, userConfig.mqttBrokerUser, userConfig.mqttBrokerPassword, userConfig.mqttUseTLS, (platformData.espUniqueName).c_str(), userConfig.mqttBrokerMainTopic, userConfig.mqttHAautoDiscoveryON, ((platformData.dtuGatewayIP).toString()).c_str());

// mqttHandler.setConfiguration(userConfig.mqttBrokerIpDomain, userConfig.mqttBrokerPort, userConfig.mqttBrokerUser, userConfig.mqttBrokerPassword, userConfig.mqttUseTLS, (platformData.espUniqueName).c_str(), userConfig.mqttBrokerMainTopic, userConfig.mqttHAautoDiscoveryON, ((platformData.dtuGatewayIP).toString()).c_str());

mqttHandler.setAutoDiscovery(userConfig.mqttHAautoDiscoveryON);
Serial.println("WEB:\t\t handleUpdateBindingsSettings - HAautoDiscovery new state: " + String(userConfig.mqttHAautoDiscoveryON));
// mqttHAautoDiscoveryON going from on to off - send one time the delete messages
if (!userConfig.mqttHAautoDiscoveryON && mqttHAautoDiscoveryONlastState)
mqttHandler.requestMQTTconnectionReset(true);
else
mqttHandler.requestMQTTconnectionReset(false);

// after changing of auto discovery stop connection to initiate takeover of new settings
// mqttHandler.stopConnection();
platformData.rebootRequestedInSec = 3;
platformData.rebootRequested = true;
}

String JSON = "{";
Expand Down Expand Up @@ -562,6 +563,7 @@ void DTUwebserver::handleUpdatePowerLimit(AsyncWebServerRequest *request)
dtuGlobalData.powerLimitSet = 2;
else
dtuGlobalData.powerLimitSet = gotLimit;
dtuGlobalData.powerLimitSetUpdate = true;

Serial.println("WEB:\t\t got SetLimit: " + String(dtuGlobalData.powerLimitSet) + " - current limit: " + String(dtuGlobalData.powerLimit) + " %");

Expand Down
39 changes: 28 additions & 11 deletions src/dtuGateway.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,24 @@ int wifiTimeoutLong = WIFI_RETRY_TIME_SECONDS;
// <--- END initializing here and published over platformData.h

// blink code for status display
#if defined(ESP8266)
// #define LED_BLINK LED_BUILTIN
// defaults
#define LED_BLINK 2
#define LED_BLINK_ON LOW
#define LED_BLINK_OFF HIGH
#elif defined(ESP32)
#define LED_BLINK 2 // double occupancy with TFT display SPI DC pin

#if defined(ESP8266)
#warning "Compiling for ESP8266"
#elif CONFIG_IDF_TARGET_ESP32
#undef LED_BLINK
#undef LED_BLINK_ON
#undef LED_BLINK_OFF
#define LED_BLINK 2
#define LED_BLINK_ON HIGH
#define LED_BLINK_OFF LOW
#warning "Compiling for ESP32"
#endif


#define BLINK_NORMAL_CONNECTION 0 // 1 Hz blip - normal connection and running
#define BLINK_WAITING_NEXT_TRY_DTU 1 // 1 Hz - waiting for next try to connect to DTU
#define BLINK_WIFI_OFF 2 // 2 Hz - wifi off
Expand Down Expand Up @@ -546,14 +553,14 @@ String getMessageFromOpenhab(String key)
}
}
// get PowerSet data from openhab
uint8_t lastOpenhabLimit = 255;
// uint8_t lastOpenhabLimit = 255;
boolean getPowerSetDataFromOpenHab()
{
uint8_t gotLimit = 0;
uint8_t newLimit = 0;
bool conversionSuccess = false;

String openhabMessage = getMessageFromOpenhab(String(userConfig.openItemPrefix) + "_PowerLimit_Set");
String openhabMessage = getMessageFromOpenhab(String(userConfig.openItemPrefix) + "_PowerLimitSet");
if (openhabMessage.length() > 0)
{
gotLimit = openhabMessage.toInt();
Expand All @@ -569,19 +576,22 @@ boolean getPowerSetDataFromOpenHab()
newLimit = 2;
else
newLimit = gotLimit;
// Serial.println("getMessageFromOpenhab - got new SetLimit: " + String(newLimit) + " %");
// Serial.println("getMessageFromOpenhab - got SetLimit: " + String(newLimit) + " %");// + " - last OH limit: " + String(lastOpenhabLimit) + " %");
}
else
{
Serial.println("OPENHAB:\t\t got wrong data for SetLimit: " + openhabMessage);
return false;
}
if (lastOpenhabLimit != newLimit && lastOpenhabLimit != 255)
if (dtuGlobalData.powerLimitSet != newLimit)// && lastOpenhabLimit != 255)
{
// Serial.println("OPENHAB:\t\t got new OH Limit: " + String(dtuGlobalData.powerLimitSet) + " - last OH limit: " + String(lastOpenhabLimit) + " %");
Serial.print("OPENHAB:\t\t last OH limit: " + String(dtuGlobalData.powerLimitSet) + " %");
dtuGlobalData.powerLimitSet = newLimit;
Serial.println("OPENHAB:\t\t got new OH Limit: " + String(dtuGlobalData.powerLimitSet) + " - last OH limit: " + String(lastOpenhabLimit) + " %");
Serial.println(" -> got new OH Limit: " + String(dtuGlobalData.powerLimitSet) + " %");
dtuGlobalData.powerLimitSetUpdate = true;
}
lastOpenhabLimit = newLimit;
// lastOpenhabLimit = newLimit;
return true;
}

Expand Down Expand Up @@ -656,6 +666,7 @@ void updateValuesToMqtt(boolean haAutoDiscovery = false)
// inverter
keyValueStore["inverter_Temp"] = String(dtuGlobalData.inverterTemp).c_str();
keyValueStore["inverter_PowerLimit"] = String(dtuGlobalData.powerLimit).c_str();
keyValueStore["inverter_PowerLimitSet"] = String(dtuGlobalData.powerLimitSet).c_str();
keyValueStore["inverter_WifiRSSI"] = String(dtuGlobalData.dtuRssi).c_str();
keyValueStore["inverter_cloudPause"] = String(dtuConnection.dtuActiveOffToCloudUpdate).c_str();
keyValueStore["inverter_dtuConnectionOnline"] = String(dtuConnection.dtuConnectionOnline).c_str();
Expand Down Expand Up @@ -1178,9 +1189,15 @@ void loop()
if (lastSetting.update == true)
{
dtuGlobalData.powerLimitSet = lastSetting.setValue;
dtuGlobalData.powerLimitSetUpdate = true;
Serial.println("\nMQTT: changed powerset value to '" + String(dtuGlobalData.powerLimitSet) + "'");
}

if(dtuGlobalData.powerLimitSetUpdate) {
mqttHandler.publishStandardData("inverter_PowerLimitSet", String(dtuGlobalData.powerLimitSet));
// postMessageToOpenhab(String(userConfig.openItemPrefix) + "_PowerLimitSet", (String)dtuGlobalData.powerLimit);
dtuGlobalData.powerLimitSetUpdate = false;
}

RemoteInverterData remoteData = mqttHandler.getRemoteInverterData();
if (remoteData.updateReceived == true)
{
Expand Down
6 changes: 4 additions & 2 deletions src/dtuInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void DTUInterface::disconnect(uint8_t tgtState)
{
client->close(true);
dtuConnection.dtuConnectState = tgtState;
dtuGlobalData.dtuRssi = 0;
// dtuGlobalData.dtuRssi = 0;
Serial.println(F("DTUinterface:\t disconnect request - DTU connection closed"));
if (tgtState == DTU_STATE_STOPPED)
{
Expand Down Expand Up @@ -312,7 +312,7 @@ void DTUInterface::onDisconnect(void *arg, AsyncClient *c)
// Connection lost
Serial.println(F("DTUinterface:\t disconnected from DTU"));
dtuConnection.dtuConnectState = DTU_STATE_OFFLINE;
dtuGlobalData.dtuRssi = 0;
// dtuGlobalData.dtuRssi = 0;
DTUInterface *dtuInterface = static_cast<DTUInterface *>(arg);
if (dtuInterface)
{
Expand Down Expand Up @@ -491,6 +491,8 @@ void DTUInterface::checkingForLastDataReceived()
dtuGlobalData.pv1.current = 0;
dtuGlobalData.pv1.voltage = 0;

dtuGlobalData.dtuRssi = 0;

dtuConnection.dtuErrorState = DTU_ERROR_LAST_SEND;
dtuConnection.dtuActiveOffToCloudUpdate = false;
dtuConnection.dtuConnectState = DTU_STATE_OFFLINE;
Expand Down
Loading

0 comments on commit fa93864

Please sign in to comment.