mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-22 13:26:10 +02:00
improved wifi handling and tickers, many thanks to @beegee3 #571
fixed YieldTotal correction calculation #589 fixed serial output of power limit acknowledge #569
This commit is contained in:
parent
9f8371fdef
commit
c157134da9
10 changed files with 133 additions and 82 deletions
|
@ -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
|
||||
|
|
101
src/app.cpp
101
src/app.cpp
|
@ -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);
|
||||
|
@ -59,12 +55,9 @@ 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
|
||||
|
@ -72,26 +65,27 @@ void app::setup() {
|
|||
|
||||
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();
|
||||
|
||||
|
@ -123,14 +117,59 @@ void app::loop(void) {
|
|||
}
|
||||
|
||||
#if !defined(AP_ONLY)
|
||||
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;
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
13
src/app.h
13
src/app.h
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -223,6 +231,8 @@ class app : public IApp, public ah::Scheduler {
|
|||
DBGPRINTLN("");
|
||||
}*/
|
||||
|
||||
innerLoopCb mInnerLoopCb;
|
||||
|
||||
bool mShowRebootRequest;
|
||||
bool mIVCommunicationOn;
|
||||
|
||||
|
@ -246,7 +256,8 @@ class app : public IApp, public ah::Scheduler {
|
|||
|
||||
// mqtt
|
||||
PubMqttType mMqtt;
|
||||
bool mMqttActive;
|
||||
bool mMqttReconnect;
|
||||
bool mMqttEnabled;
|
||||
|
||||
// sun
|
||||
int32_t mCalculatedTimezoneOffset;
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
//-------------------------------------
|
||||
#define VERSION_MAJOR 0
|
||||
#define VERSION_MINOR 5
|
||||
#define VERSION_PATCH 70
|
||||
#define VERSION_PATCH 71
|
||||
|
||||
//-------------------------------------
|
||||
typedef struct {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -32,7 +32,6 @@ class PubMqtt {
|
|||
PubMqtt() {
|
||||
mRxCnt = 0;
|
||||
mTxCnt = 0;
|
||||
mEnReconnect = false;
|
||||
mSubscriptionCb = NULL;
|
||||
mIvAvail = true;
|
||||
memset(mLastIvState, 0xff, MAX_NUM_INVERTERS);
|
||||
|
@ -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?
|
||||
|
@ -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();
|
||||
|
@ -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() {
|
||||
|
@ -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);
|
||||
|
@ -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"));
|
||||
|
@ -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];
|
||||
|
|
|
@ -35,8 +35,7 @@ namespace ah {
|
|||
mTimestamp = 0;
|
||||
mMax = 0;
|
||||
mPrevMillis = millis();
|
||||
for (uint8_t i = 0; i < MAX_NUM_TICKER; i++)
|
||||
mTickerInUse[i] = false;
|
||||
resetTicker();
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
|
@ -94,6 +93,11 @@ namespace ah {
|
|||
return mTimestamp;
|
||||
}
|
||||
|
||||
inline void resetTicker(void) {
|
||||
for (uint8_t i = 0; i < MAX_NUM_TICKER; i++)
|
||||
mTickerInUse[i] = false;
|
||||
}
|
||||
|
||||
void getStat(uint8_t *max) {
|
||||
*max = mMax;
|
||||
}
|
||||
|
|
|
@ -18,9 +18,10 @@ ahoywifi::ahoywifi() : mApIp(192, 168, 4, 1) {}
|
|||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp) {
|
||||
void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb) {
|
||||
mConfig = config;
|
||||
mUtcTimestamp = utcTimestamp;
|
||||
mAppWifiCb = cb;
|
||||
|
||||
mStaConn = DISCONNECTED;
|
||||
mCnt = 0;
|
||||
|
@ -256,6 +257,7 @@ void ahoywifi::connectionEvent(WiFiStatus_t status) {
|
|||
case GOT_IP:
|
||||
mStaConn = GOT_IP;
|
||||
welcome(WiFi.localIP().toString() + F(" (Station)"));
|
||||
mAppWifiCb(true);
|
||||
break;
|
||||
|
||||
case DISCONNECTED:
|
||||
|
@ -263,6 +265,7 @@ void ahoywifi::connectionEvent(WiFiStatus_t status) {
|
|||
mStaConn = DISCONNECTED;
|
||||
mCnt = 5; // try to reconnect in 5 sec
|
||||
setupWifi(); // reconnect with AP / Station setup
|
||||
mAppWifiCb(false);
|
||||
DPRINTLN(DBG_INFO, "[WiFi] Connection Lost");
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -18,9 +18,12 @@ class app;
|
|||
|
||||
class ahoywifi {
|
||||
public:
|
||||
typedef std::function<void(bool)> appWifiCb;
|
||||
|
||||
ahoywifi();
|
||||
|
||||
void setup(settings_t *config, uint32_t *utcTimestamp);
|
||||
|
||||
void setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb);
|
||||
void tickWifiLoop(void);
|
||||
bool getNtpTime(void);
|
||||
void scanAvailNetworks(void);
|
||||
|
@ -50,6 +53,7 @@ class ahoywifi {
|
|||
|
||||
|
||||
settings_t *mConfig;
|
||||
appWifiCb mAppWifiCb;
|
||||
|
||||
DNSServer mDns;
|
||||
IPAddress mApIp;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue