Skip to content

Commit

Permalink
improved wifi handling and tickers, many thanks to @beegee3 #571
Browse files Browse the repository at this point in the history
fixed YieldTotal correction calculation #589
fixed serial output of power limit acknowledge #569
  • Loading branch information
lumapu committed Jan 16, 2023
1 parent 9f8371f commit c157134
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 82 deletions.
5 changes: 5 additions & 0 deletions src/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

(starting from release version `0.5.66`)

## 0.5.71
* improved wifi handling and tickers, many thanks to @beegee3 #571
* fixed YieldTotal correction calculation #589
* fixed serial output of power limit acknowledge #569

## 0.5.70
* corrected MQTT `comm_disabled` #529
* fix Prometheus and JSON endpoints (`config_override.h`) #561
Expand Down
103 changes: 78 additions & 25 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,17 @@ void app::setup() {
mSys->setup(mConfig->nrf.amplifierPower, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs);
mPayload.addListener(std::bind(&app::payloadEventListener, this, std::placeholders::_1));


#if !defined(AP_ONLY)
mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mTimestamp);
#if defined(AP_ONLY)
mInnerLoopCb = std::bind(&app::loopStandard, this);
#else
mInnerLoopCb = std::bind(&app::loopWifi, this);
#endif

mWifi.setup(mConfig, &mTimestamp);
mWifi.setup(mConfig, &mTimestamp, std::bind(&app::onWifi, this, std::placeholders::_1));
#if !defined(AP_ONLY)
everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi));
#endif

mSendTickerId = every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval);
#if !defined(AP_ONLY)
once(std::bind(&app::tickNtpUpdate, this), 2);
#endif

mSys->addInverters(&mConfig->inst);
mPayload.setup(this, mSys, &mStat, mConfig->nrf.maxRetransPerPyld, &mTimestamp);
mPayload.enableSerialDebug(mConfig->serial.debug);
Expand All @@ -59,39 +55,37 @@ void app::setup() {

// when WiFi is in client mode, then enable mqtt broker
#if !defined(AP_ONLY)
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);
mMqttEnabled = (mConfig->mqtt.broker[0] > 0);
if (mMqttEnabled) {
mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mTimestamp);
mMqtt.setSubscriptionCb(std::bind(&app::mqttSubRxCb, this, std::placeholders::_1));
}
#endif
setupLed();

mWeb.setup(this, mSys, mConfig);
mWeb.setProtection(strlen(mConfig->sys.adminPwd) != 0);
everySec(std::bind(&WebType::tickSecond, &mWeb));

mApi.setup(this, mSys, mWeb.getWebSrvPtr(), mConfig);

// Plugins
#if defined(ENA_NOKIA) || defined(ENA_SSD1306) || defined(ENA_SH1106)
mMonoDisplay.setup(mSys, &mTimestamp);
everySec(std::bind(&MonoDisplayType::tickerSecond, &mMonoDisplay));
#endif

mPubSerial.setup(mConfig, mSys, &mTimestamp);
every(std::bind(&PubSerialType::tick, &mPubSerial), mConfig->serial.interval);
//everySec(std::bind(&app::tickSerial, this));

regularTickers();
}

//-----------------------------------------------------------------------------
void app::loop(void) {
DPRINTLN(DBG_VERBOSE, F("app::loop"));
mInnerLoopCb();
}

ah::Scheduler::loop();
//-----------------------------------------------------------------------------
void app::loopStandard(void) {
mSys->Radio.loop();
mPayload.loop();

Expand Down Expand Up @@ -123,14 +117,59 @@ void app::loop(void) {
}

#if !defined(AP_ONLY)
mMqtt.loop();
if(mMqttEnabled)
mMqtt.loop();
#endif
}

//-----------------------------------------------------------------------------
void app::loopWifi(void) {
DPRINTLN(DBG_VERBOSE, F("app::loop Wifi"));

ah::Scheduler::loop();
yield();
}

//-----------------------------------------------------------------------------
void app::onWifi(bool gotIp) {
ah::Scheduler::resetTicker();
regularTickers(); // reinstall regular tickers
if (gotIp) {
mInnerLoopCb = std::bind(&app::loopStandard, this);
mSendTickerId = every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval);
mMqttReconnect = true;
once(std::bind(&app::tickNtpUpdate, this), 2);
}
else {
mInnerLoopCb = std::bind(&app::loopWifi, this);
everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi));
}
}

//-----------------------------------------------------------------------------
void app::regularTickers(void) {
everySec(std::bind(&WebType::tickSecond, &mWeb));
// Plugins
#if defined(ENA_NOKIA) || defined(ENA_SSD1306) || defined(ENA_SH1106)
everySec(std::bind(&MonoDisplayType::tickerSecond, &mMonoDisplay));
#endif
every(std::bind(&PubSerialType::tick, &mPubSerial), mConfig->serial.interval);
}

//-----------------------------------------------------------------------------
void app::tickNtpUpdate(void) {
uint32_t nxtTrig = 5; // default: check again in 5 sec
if (mWifi.getNtpTime()) {
if (mMqttReconnect && mMqttEnabled) {
mMqtt.connect();
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);
mMqttReconnect = false;
}

nxtTrig = 43200; // check again in 12 h
if((mSunrise == 0) && (mConfig->sun.lat) && (mConfig->sun.lon)) {
mCalculatedTimezoneOffset = (int8_t)((mConfig->sun.lon >= 0 ? mConfig->sun.lon + 7.5 : mConfig->sun.lon - 7.5) / 15) * 3600;
Expand All @@ -152,8 +191,8 @@ void app::tickCalcSunrise(void) {

uint32_t nxtTrig = mSunset + mConfig->sun.offsetSec + 60; // set next trigger to communication stop, +60 for safety that it is certain past communication stop
onceAt(std::bind(&app::tickCalcSunrise, this), nxtTrig);
if (mConfig->mqtt.broker[0] > 0)
mMqtt.tickerSun(mSunrise, mSunset, mConfig->sun.offsetSec, mConfig->sun.disNightCom);
if (mMqttEnabled)
tickSun();
}

//-----------------------------------------------------------------------------
Expand All @@ -174,8 +213,22 @@ void app::tickIVCommunication(void) {
if (nxtTrig != 0)
onceAt(std::bind(&app::tickIVCommunication, this), nxtTrig);
}
if (mConfig->mqtt.broker[0] > 0)
mMqtt.tickerComm(!mIVCommunicationOn);
if (mMqttEnabled)
tickComm();
}

//-----------------------------------------------------------------------------
void app::tickSun(void) {
// only used and enabled by MQTT (see setup())
if (!mMqtt.tickerSun(mSunrise, mSunset, mConfig->sun.offsetSec, mConfig->sun.disNightCom))
once(std::bind(&app::tickSun, this), 1); // MQTT not connected, retry
}

//-----------------------------------------------------------------------------
void app::tickComm(void) {
// only used and enabled by MQTT (see setup())
if (!mMqtt.tickerComm(!mIVCommunicationOn))
once(std::bind(&app::tickComm, this), 1); // MQTT not connected, retry
}

//-----------------------------------------------------------------------------
Expand Down
13 changes: 12 additions & 1 deletion src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class app : public IApp, public ah::Scheduler {

void setup(void);
void loop(void);
void loopStandard(void);
void loopWifi(void);
void onWifi(bool gotIp);
void regularTickers(void);
void handleIntr(void);
void cbMqtt(char* topic, byte* payload, unsigned int length);
void saveValues(void);
Expand Down Expand Up @@ -182,6 +186,8 @@ class app : public IApp, public ah::Scheduler {
HmSystemType *mSys;

private:
typedef std::function<void()> innerLoopCb;

void resetSystem(void);

void payloadEventListener(uint8_t cmd) {
Expand All @@ -206,6 +212,8 @@ class app : public IApp, public ah::Scheduler {
void tickNtpUpdate(void);
void tickCalcSunrise(void);
void tickIVCommunication(void);
void tickSun(void);
void tickComm(void);
void tickSend(void);
void tickMidnight(void);
/*void tickSerial(void) {
Expand All @@ -223,6 +231,8 @@ class app : public IApp, public ah::Scheduler {
DBGPRINTLN("");
}*/

innerLoopCb mInnerLoopCb;

bool mShowRebootRequest;
bool mIVCommunicationOn;

Expand All @@ -246,7 +256,8 @@ class app : public IApp, public ah::Scheduler {

// mqtt
PubMqttType mMqtt;
bool mMqttActive;
bool mMqttReconnect;
bool mMqttEnabled;

// sun
int32_t mCalculatedTimezoneOffset;
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 70
#define VERSION_PATCH 71

//-------------------------------------
typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion src/hm/hmInverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class Inverter {
rec->record[pos] = (REC_TYP)((int16_t)val) / (REC_TYP)(div);
} else if ((FLD_YT == rec->assign[pos].fieldId)
&& (config->yieldCor != 0)) {
rec->record[pos] = (REC_TYP)(val) / (REC_TYP)(div) - (REC_TYP)config->yieldCor;
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);
Expand Down
6 changes: 3 additions & 3 deletions src/hm/payload.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ class Payload : public Handler<payloadListenerType> {

if ((p->packet[12] == ActivePowerContr) && (p->packet[13] == 0x00)) {
String msg = "";
if((p->packet[10] == 0x00) && (p->packet[11] == 0x00)) {
msg = "NOT ";
if((p->packet[10] == 0x00) && (p->packet[11] == 0x00))
mApp->setMqttPowerLimitAck(iv);
}
else
msg = "NOT ";
DPRINTLN(DBG_INFO, F("Inverter ") + String(iv->id) + F(" has ") + msg + F("accepted power limit set point ") + String(iv->powerLimit[0]) + F(" with PowerLimitControl ") + String(iv->powerLimit[1]));
}
iv->devControlCmd = Init;
Expand Down
65 changes: 18 additions & 47 deletions src/publisher/pubMqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class PubMqtt {
PubMqtt() {
mRxCnt = 0;
mTxCnt = 0;
mEnReconnect = false;
mSubscriptionCb = NULL;
mIvAvail = true;
memset(mLastIvState, 0xff, MAX_NUM_INVERTERS);
Expand All @@ -51,13 +50,6 @@ class PubMqtt {

snprintf(mLwtTopic, MQTT_TOPIC_LEN + 5, "%s/mqtt", mCfgMqtt->topic);

#if defined(ESP8266)
mHWifiCon = WiFi.onStationModeGotIP(std::bind(&PubMqtt::onWifiConnect, this, std::placeholders::_1));
mHWifiDiscon = WiFi.onStationModeDisconnected(std::bind(&PubMqtt::onWifiDisconnect, this, std::placeholders::_1));
#else
WiFi.onEvent(std::bind(&PubMqtt::onWiFiEvent, this, std::placeholders::_1));
#endif

if((strlen(mCfgMqtt->user) > 0) && (strlen(mCfgMqtt->pwd) > 0))
mClient.setCredentials(mCfgMqtt->user, mCfgMqtt->pwd);
mClient.setClientId(mDevName); // TODO: add mac?
Expand All @@ -74,6 +66,11 @@ class PubMqtt {
#endif
}

void connect() {
if(!mClient.connected())
mClient.connect();
}

void tickerSecond() {
if(0 == mCfgMqtt->interval) // no fixed interval, publish once new data were received (from inverter)
sendIvData();
Expand All @@ -94,27 +91,32 @@ class PubMqtt {
publish("uptime", val);
publish("wifi_rssi", String(WiFi.RSSI()).c_str());
publish("free_heap", String(ESP.getFreeHeap()).c_str());

if(!mClient.connected()) {
if(mEnReconnect)
mClient.connect();
}
}

void tickerSun(uint32_t sunrise, uint32_t sunset, uint32_t offs, bool disNightCom) {
bool tickerSun(uint32_t sunrise, uint32_t sunset, uint32_t offs, bool disNightCom) {
if (!mClient.connected())
return false;

publish("sunrise", String(sunrise).c_str(), true);
publish("sunset", String(sunset).c_str(), true);
publish("comm_start", String(sunrise - offs).c_str(), true);
publish("comm_stop", String(sunset + offs).c_str(), true);
publish("dis_night_comm", ((disNightCom) ? "true" : "false"), true);

return true;
}

void tickerComm(bool disabled) {
bool tickerComm(bool disabled) {
if (!mClient.connected())
return false;

publish("comm_disabled", ((disabled) ? "true" : "false"), true);
publish("comm_dis_ts", String(*mUtcTimestamp).c_str(), true);

if(disabled && (mCfgMqtt->rstValsCommStop))
zeroAllInverters();

return true;
}

void tickerMidnight() {
Expand Down Expand Up @@ -237,39 +239,8 @@ class PubMqtt {
}

private:
#if defined(ESP8266)
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
DPRINTLN(DBG_VERBOSE, F("MQTT connecting"));
mClient.connect();
mEnReconnect = true;
}

void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
mEnReconnect = false;
}

#else
void onWiFiEvent(WiFiEvent_t event) {
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
DPRINTLN(DBG_VERBOSE, F("MQTT connecting"));
mClient.connect();
mEnReconnect = true;
break;

case SYSTEM_EVENT_STA_DISCONNECTED:
mEnReconnect = false;
break;

default:
break;
}
}
#endif

void onConnect(bool sessionPreset) {
DPRINTLN(DBG_INFO, F("MQTT connected"));
mEnReconnect = true;

if(mExeOnce) {
publish("version", mVersion, true);
Expand All @@ -290,6 +261,7 @@ class PubMqtt {
switch (reason) {
case espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED:
DBGPRINTLN(F("TCP disconnect"));
connect();
break;
case espMqttClientTypes::DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION:
DBGPRINTLN(F("wrong protocol version"));
Expand Down Expand Up @@ -590,7 +562,6 @@ class PubMqtt {
uint32_t *mUtcTimestamp;
uint32_t mRxCnt, mTxCnt;
std::queue<uint8_t> mSendList;
bool mEnReconnect;
subscriptionCb mSubscriptionCb;
bool mIvAvail; // shows if at least one inverter is available
uint8_t mLastIvState[MAX_NUM_INVERTERS];
Expand Down
Loading

0 comments on commit c157134

Please sign in to comment.