Skip to content

Commit

Permalink
corrected MQTT comm_disabled lumapu#529
Browse files Browse the repository at this point in the history
fix Prometheus and JSON endpoints (`config_override.h`) lumapu#561
publish MQTT with fixed interval even if inverter is not available lumapu#542
added JSON settings upload. NOTE: settings JSON download changed, so only settings should be uploaded starting from version `0.5.70` lumapu#551
MQTT topic and inverter name have more allowed characters: `[A-Za-z0-9./#$%&=+_-]+`, thx: @mo Demman
improved potential issue with `checkTicker`, thx @cbscpe
MQTT option for reset values on midnight / not avail / communication stop lumapu#539
small fix in `tickIVCommunication` lumapu#534
add `YieldTotal` correction, eg. to have the option to zero at year start lumapu#512
  • Loading branch information
lumapu authored and modem-man-gmx committed Jan 15, 2023
1 parent 1d8100f commit 6e41a83
Show file tree
Hide file tree
Showing 16 changed files with 343 additions and 215 deletions.
8 changes: 8 additions & 0 deletions src/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

## 0.5.70
* corrected MQTT `comm_disabled` #529
* fix Prometheus and JSON endpoints (`config_override.h`) #561
* publish MQTT with fixed interval even if inverter is not available #542
* added JSON settings upload. NOTE: settings JSON download changed, so only settings should be uploaded starting from version `0.5.70` #551
* MQTT topic and inverter name have more allowed characters: `[A-Za-z0-9./#$%&=+_-]+`, thx: @Mo Demman
* improved potential issue with `checkTicker`, thx @cbscpe
* MQTT option for reset values on midnight / not avail / communication stop #539
* small fix in `tickIVCommunication` #534
* add `YieldTotal` correction, eg. to have the option to zero at year start #512

## 0.5.69
* merged SH1106 1.3" Display, thx @dAjaY85
Expand Down
14 changes: 13 additions & 1 deletion src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ void app::setup() {
if (mConfig->mqtt.broker[0] > 0) {
everySec(std::bind(&PubMqttType::tickerSecond, &mMqtt));
everyMin(std::bind(&PubMqttType::tickerMinute, &mMqtt));
uint32_t nxtTrig = mTimestamp - ((mTimestamp - 1) % 86400) + 86400; // next midnight
if(mConfig->mqtt.rstYieldMidNight)
onceAt(std::bind(&app::tickMidnight, this), nxtTrig);
mMqtt.setSubscriptionCb(std::bind(&app::mqttSubRxCb, this, std::placeholders::_1));
}
#endif
Expand Down Expand Up @@ -161,7 +164,7 @@ void app::tickIVCommunication(void) {
if (mTimestamp < (mSunrise - mConfig->sun.offsetSec)) { // current time is before communication start, set next trigger to communication start
nxtTrig = mSunrise - mConfig->sun.offsetSec;
} else {
if (mTimestamp > (mSunset + mConfig->sun.offsetSec)) { // current time is past communication stop, nothing to do. Next update will be done at midnight by tickCalcSunrise
if (mTimestamp >= (mSunset + mConfig->sun.offsetSec)) { // current time is past communication stop, nothing to do. Next update will be done at midnight by tickCalcSunrise
nxtTrig = 0;
} else { // current time lies within communication start/stop time, set next trigger to communication stop
mIVCommunicationOn = true;
Expand Down Expand Up @@ -207,6 +210,15 @@ void app::tickSend(void) {
updateLed();
}

//-----------------------------------------------------------------------------
void app::tickMidnight(void) {
// only used and enabled by MQTT (see setup())
uint32_t nxtTrig = mTimestamp - ((mTimestamp - 1) % 86400) + 86400; // next midnight
onceAt(std::bind(&app::tickMidnight, this), nxtTrig);

mMqtt.tickerMidnight();
}

//-----------------------------------------------------------------------------
void app::handleIntr(void) {
DPRINTLN(DBG_VERBOSE, F("app::handleIntr"));
Expand Down
7 changes: 6 additions & 1 deletion src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class app : public IApp, public ah::Scheduler {
return mSettings.saveSettings();
}

bool readSettings(const char *path) {
return mSettings.readSettings(path);
}

bool eraseSettings(bool eraseWifi = false) {
return mSettings.eraseSettings(eraseWifi);
}
Expand All @@ -95,7 +99,7 @@ class app : public IApp, public ah::Scheduler {
}

void setRebootFlag() {
once(std::bind(&app::tickReboot, this), 1);
once(std::bind(&app::tickReboot, this), 3);
}

const char *getVersion() {
Expand Down Expand Up @@ -203,6 +207,7 @@ class app : public IApp, public ah::Scheduler {
void tickCalcSunrise(void);
void tickIVCommunication(void);
void tickSend(void);
void tickMidnight(void);
/*void tickSerial(void) {
if(Serial.available() == 0)
return;
Expand Down
1 change: 1 addition & 0 deletions src/appInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class IApp {
public:
virtual ~IApp() {}
virtual bool saveSettings() = 0;
virtual bool readSettings(const char *path) = 0;
virtual bool eraseSettings(bool eraseWifi) = 0;
virtual void setRebootFlag() = 0;
virtual const char *getVersion() = 0;
Expand Down
31 changes: 25 additions & 6 deletions src/config/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ typedef struct {
char pwd[MQTT_PWD_LEN];
char topic[MQTT_TOPIC_LEN];
uint16_t interval;
bool rstYieldMidNight;
bool rstValsNotAvail;
bool rstValsCommStop;
} cfgMqtt_t;

typedef struct {
Expand All @@ -105,6 +108,7 @@ typedef struct {
serial_u serial;
uint16_t chMaxPwr[4];
char chName[4][MAX_NAME_LENGTH];
uint32_t yieldCor; // YieldTotal correction value
} cfgIv_t;

typedef struct {
Expand Down Expand Up @@ -155,7 +159,7 @@ class settings {
else
DPRINTLN(DBG_INFO, F(" .. done"));

readSettings();
readSettings("/settings.json");
}

// should be used before OTA
Expand Down Expand Up @@ -186,9 +190,10 @@ class settings {
#endif
}

void readSettings(void) {
bool readSettings(const char* path) {
bool success = false;
loadDefaults();
File fp = LittleFS.open("/settings.json", "r");
File fp = LittleFS.open(path, "r");
if(!fp)
DPRINTLN(DBG_WARN, F("failed to load json, using default config"));
else {
Expand All @@ -206,13 +211,15 @@ class settings {
jsonMqtt(root["mqtt"]);
jsonLed(root["led"]);
jsonInst(root["inst"]);
success = true;
}
else {
Serial.println(F("failed to parse json, using default config"));
}

fp.close();
}
return success;
}

bool saveSettings(void) {
Expand Down Expand Up @@ -299,6 +306,9 @@ class settings {
snprintf(mCfg.mqtt.pwd, MQTT_PWD_LEN, "%s", DEF_MQTT_PWD);
snprintf(mCfg.mqtt.topic, MQTT_TOPIC_LEN, "%s", DEF_MQTT_TOPIC);
mCfg.mqtt.interval = 0; // off
mCfg.mqtt.rstYieldMidNight = false;
mCfg.mqtt.rstValsNotAvail = false;
mCfg.mqtt.rstValsCommStop = false;

mCfg.led.led0 = DEF_LED0_PIN;
mCfg.led.led1 = DEF_LED1_PIN;
Expand Down Expand Up @@ -399,9 +409,16 @@ class settings {
obj[F("pwd")] = mCfg.mqtt.pwd;
obj[F("topic")] = mCfg.mqtt.topic;
obj[F("intvl")] = mCfg.mqtt.interval;
obj[F("rstMidNight")] = (bool)mCfg.mqtt.rstYieldMidNight;
obj[F("rstNotAvail")] = (bool)mCfg.mqtt.rstValsNotAvail;
obj[F("rstComStop")] = (bool)mCfg.mqtt.rstValsCommStop;

} else {
mCfg.mqtt.port = obj[F("port")];
mCfg.mqtt.interval = obj[F("intvl")];
mCfg.mqtt.rstYieldMidNight = (bool)obj["rstMidNight"];
mCfg.mqtt.rstValsNotAvail = (bool)obj["rstNotAvail"];
mCfg.mqtt.rstValsCommStop = (bool)obj["rstComStop"];
snprintf(mCfg.mqtt.broker, MQTT_ADDR_LEN, "%s", obj[F("broker")].as<const char*>());
snprintf(mCfg.mqtt.user, MQTT_USER_LEN, "%s", obj[F("user")].as<const char*>());
snprintf(mCfg.mqtt.pwd, MQTT_PWD_LEN, "%s", obj[F("pwd")].as<const char*>());
Expand Down Expand Up @@ -438,9 +455,10 @@ class settings {

void jsonIv(JsonObject obj, cfgIv_t *cfg, bool set = false) {
if(set) {
obj[F("en")] = (bool)cfg->enabled;
obj[F("name")] = cfg->name;
obj[F("sn")] = cfg->serial.u64;
obj[F("en")] = (bool)cfg->enabled;
obj[F("name")] = cfg->name;
obj[F("sn")] = cfg->serial.u64;
obj[F("yield")] = cfg->yieldCor;
for(uint8_t i = 0; i < 4; i++) {
obj[F("pwr")][i] = cfg->chMaxPwr[i];
obj[F("chName")][i] = cfg->chName[i];
Expand All @@ -449,6 +467,7 @@ class settings {
cfg->enabled = (bool)obj[F("en")];
snprintf(cfg->name, MAX_NAME_LENGTH, "%s", obj[F("name")].as<const char*>());
cfg->serial.u64 = obj[F("sn")];
cfg->yieldCor = obj[F("yield")];
for(uint8_t i = 0; i < 4; i++) {
cfg->chMaxPwr[i] = obj[F("pwr")][i];
snprintf(cfg->chName[i], MAX_NAME_LENGTH, "%s", obj[F("chName")][i].as<const char*>());
Expand Down
2 changes: 1 addition & 1 deletion src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//-------------------------------------
#define VERSION_MAJOR 0
#define VERSION_MINOR 5
#define VERSION_PATCH 69
#define VERSION_PATCH 70

//-------------------------------------
typedef struct {
Expand Down
18 changes: 15 additions & 3 deletions src/hm/hmInverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,13 @@ class Inverter {
val <<= 8;
val |= buf[ptr];
} while(++ptr != end);
if(FLD_T == rec->assign[pos].fieldId) {
if (FLD_T == rec->assign[pos].fieldId) {
// temperature is a signed value!
rec->record[pos] = (REC_TYP)((int16_t)val) / (REC_TYP)(div);
}
else {
} else if ((FLD_YT == rec->assign[pos].fieldId)
&& (config->yieldCor != 0)) {
rec->record[pos] = (REC_TYP)(val) / (REC_TYP)(div) - (REC_TYP)config->yieldCor;
} else {
if ((REC_TYP)(div) > 1)
rec->record[pos] = (REC_TYP)(val) / (REC_TYP)(div);
else
Expand Down Expand Up @@ -286,6 +288,16 @@ class Inverter {
DPRINTLN(DBG_ERROR, F("addValue: assignment not found with cmd 0x"));
}

bool setValue(uint8_t pos, record_t<> *rec, REC_TYP val) {
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:setValue"));
if(NULL == rec)
return false;
if(pos > rec->length)
return false;
rec->record[pos] = val;
return true;
}

REC_TYP getValue(uint8_t pos, record_t<> *rec) {
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getValue"));
if(NULL == rec)
Expand Down
Loading

0 comments on commit 6e41a83

Please sign in to comment.