mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-25 14:56:11 +02:00
Merge branch 'development03' into hms
This commit is contained in:
commit
7ce78ab56e
48 changed files with 2854 additions and 1511 deletions
93
src/app.cpp
93
src/app.cpp
|
@ -21,16 +21,10 @@ void app::setup() {
|
|||
|
||||
resetSystem();
|
||||
|
||||
/*DBGPRINTLN("--- start");
|
||||
DBGPRINTLN(String(ESP.getFreeHeap()));
|
||||
DBGPRINTLN(String(ESP.getHeapFragmentation()));
|
||||
DBGPRINTLN(String(ESP.getMaxFreeBlockSize()));*/
|
||||
|
||||
|
||||
mSettings.setup();
|
||||
mSettings.getPtr(mConfig);
|
||||
DPRINT(DBG_INFO, F("Settings valid: "));
|
||||
if(mSettings.getValid())
|
||||
if (mSettings.getValid())
|
||||
DBGPRINTLN(F("true"));
|
||||
else
|
||||
DBGPRINTLN(F("false"));
|
||||
|
@ -67,7 +61,7 @@ void app::setup() {
|
|||
mMiPayload.setup(this, &mSys, &mNrfRadio, &mStat, mConfig->nrf.maxRetransPerPyld, &mTimestamp);
|
||||
mMiPayload.enableSerialDebug(mConfig->serial.debug);
|
||||
|
||||
if(!mNrfRadio.isChipConnected())
|
||||
if(!mNrfRadio.isChipConnected())
|
||||
DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring"));
|
||||
}
|
||||
if(mConfig->cmt.enabled) {
|
||||
|
@ -81,6 +75,8 @@ void app::setup() {
|
|||
DBGPRINTLN(String(ESP.getHeapFragmentation()));
|
||||
DBGPRINTLN(String(ESP.getMaxFreeBlockSize()));*/
|
||||
|
||||
//if (!mSys.Radio.isChipConnected())
|
||||
// DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring"));
|
||||
|
||||
// when WiFi is in client mode, then enable mqtt broker
|
||||
#if !defined(AP_ONLY)
|
||||
|
@ -99,18 +95,18 @@ void app::setup() {
|
|||
mApi.setup(this, &mSys, &mNrfRadio, mWeb.getWebSrvPtr(), mConfig);
|
||||
|
||||
// Plugins
|
||||
if(mConfig->plugin.display.type != 0)
|
||||
mMonoDisplay.setup(&mConfig->plugin.display, &mSys, &mTimestamp, 0xff, mVersion);
|
||||
if (mConfig->plugin.display.type != 0)
|
||||
mDisplay.setup(&mConfig->plugin.display, &mSys, &mTimestamp, 0xff, mVersion);
|
||||
|
||||
mPubSerial.setup(mConfig, &mSys, &mTimestamp);
|
||||
|
||||
regularTickers();
|
||||
|
||||
|
||||
/*DBGPRINTLN("--- end setup");
|
||||
DBGPRINTLN(String(ESP.getFreeHeap()));
|
||||
DBGPRINTLN(String(ESP.getHeapFragmentation()));
|
||||
DBGPRINTLN(String(ESP.getMaxFreeBlockSize()));*/
|
||||
// DBGPRINTLN("--- end setup");
|
||||
// DBGPRINTLN(String(ESP.getFreeHeap()));
|
||||
// DBGPRINTLN(String(ESP.getHeapFragmentation()));
|
||||
// DBGPRINTLN(String(ESP.getMaxFreeBlockSize()));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
@ -137,8 +133,8 @@ void app::loopStandard(void) {
|
|||
mStat.frmCnt++;
|
||||
|
||||
Inverter<> *iv = mSys.findInverter(&p->packet[1]);
|
||||
if(NULL != iv) {
|
||||
if(IV_HM == iv->ivGen)
|
||||
if (NULL != iv) {
|
||||
if (IV_HM == iv->ivGen)
|
||||
mPayload.add(iv, p);
|
||||
else
|
||||
mMiPayload.add(iv, p);
|
||||
|
@ -180,7 +176,7 @@ void app::loopStandard(void) {
|
|||
mHmsPayload.loop();
|
||||
#endif
|
||||
|
||||
if(mMqttEnabled)
|
||||
if (mMqttEnabled)
|
||||
mMqtt.loop();
|
||||
}
|
||||
|
||||
|
@ -194,7 +190,7 @@ void app::loopWifi(void) {
|
|||
void app::onWifi(bool gotIp) {
|
||||
DPRINTLN(DBG_DEBUG, F("onWifi"));
|
||||
ah::Scheduler::resetTicker();
|
||||
regularTickers(); // reinstall regular tickers
|
||||
regularTickers(); // reinstall regular tickers
|
||||
if (gotIp) {
|
||||
mInnerLoopCb = std::bind(&app::loopStandard, this);
|
||||
every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend");
|
||||
|
@ -203,14 +199,13 @@ void app::onWifi(bool gotIp) {
|
|||
everySec(std::bind(&CmtRadioType::tickSecond, &mCmtRadio), "tsCmt");
|
||||
#endif
|
||||
mMqttReconnect = true;
|
||||
mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers!
|
||||
mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers!
|
||||
once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2");
|
||||
if(WIFI_AP == WiFi.getMode()) {
|
||||
if (WIFI_AP == WiFi.getMode()) {
|
||||
mMqttEnabled = false;
|
||||
everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL");
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
mInnerLoopCb = std::bind(&app::loopWifi, this);
|
||||
everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL");
|
||||
}
|
||||
|
@ -221,8 +216,8 @@ void app::regularTickers(void) {
|
|||
DPRINTLN(DBG_DEBUG, F("regularTickers"));
|
||||
everySec(std::bind(&WebType::tickSecond, &mWeb), "webSc");
|
||||
// Plugins
|
||||
if(mConfig->plugin.display.type != 0)
|
||||
everySec(std::bind(&MonoDisplayType::tickerSecond, &mMonoDisplay), "disp");
|
||||
if (mConfig->plugin.display.type != 0)
|
||||
everySec(std::bind(&DisplayType::tickerSecond, &mDisplay), "disp");
|
||||
every(std::bind(&PubSerialType::tick, &mPubSerial), mConfig->serial.interval, "uart");
|
||||
}
|
||||
|
||||
|
@ -238,26 +233,26 @@ void app::tickNtpUpdate(void) {
|
|||
}
|
||||
|
||||
// only install schedulers once even if NTP wasn't successful in first loop
|
||||
if(mMqttReconnect) { // @TODO: mMqttReconnect is variable which scope has changed
|
||||
if(mConfig->inst.rstValsNotAvail)
|
||||
if (mMqttReconnect) { // @TODO: mMqttReconnect is variable which scope has changed
|
||||
if (mConfig->inst.rstValsNotAvail)
|
||||
everyMin(std::bind(&app::tickMinute, this), "tMin");
|
||||
if(mConfig->inst.rstYieldMidNight) {
|
||||
if (mConfig->inst.rstYieldMidNight) {
|
||||
uint32_t localTime = gTimezone.toLocal(mTimestamp);
|
||||
uint32_t midTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86400); // next midnight local time
|
||||
uint32_t midTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86400); // next midnight local time
|
||||
onceAt(std::bind(&app::tickMidnight, this), midTrig, "midNi");
|
||||
}
|
||||
}
|
||||
|
||||
nxtTrig = isOK ? 43200 : 60; // depending on NTP update success check again in 12 h or in 1 min
|
||||
nxtTrig = isOK ? 43200 : 60; // depending on NTP update success check again in 12 h or in 1 min
|
||||
|
||||
if((mSunrise == 0) && (mConfig->sun.lat) && (mConfig->sun.lon)) {
|
||||
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;
|
||||
tickCalcSunrise();
|
||||
}
|
||||
|
||||
// immediately start communicating
|
||||
// @TODO: leads to reboot loops? not sure #674
|
||||
if(isOK && mSendFirst) {
|
||||
if (isOK && mSendFirst) {
|
||||
mSendFirst = false;
|
||||
once(std::bind(&app::tickSend, this), 2, "senOn");
|
||||
}
|
||||
|
@ -308,17 +303,17 @@ void app::tickIVCommunication(void) {
|
|||
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, "mqSun"); // MQTT not connected, retry
|
||||
once(std::bind(&app::tickSun, this), 1, "mqSun"); // MQTT not connected, retry
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void app::tickComm(void) {
|
||||
if((!mIVCommunicationOn) && (mConfig->inst.rstValsCommStop))
|
||||
if ((!mIVCommunicationOn) && (mConfig->inst.rstValsCommStop))
|
||||
once(std::bind(&app::tickZeroValues, this), mConfig->nrf.sendInterval, "tZero");
|
||||
|
||||
if (mMqttEnabled) {
|
||||
if (!mMqtt.tickerComm(!mIVCommunicationOn))
|
||||
once(std::bind(&app::tickComm, this), 5, "mqCom"); // MQTT not connected, retry after 5s
|
||||
once(std::bind(&app::tickComm, this), 5, "mqCom"); // MQTT not connected, retry after 5s
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -329,7 +324,7 @@ void app::tickZeroValues(void) {
|
|||
for (uint8_t id = 0; id < mSys.getNumInverters(); id++) {
|
||||
iv = mSys.getInverterByPos(id);
|
||||
if (NULL == iv)
|
||||
continue; // skip to next inverter
|
||||
continue; // skip to next inverter
|
||||
|
||||
mPayload.zeroInverterValues(iv);
|
||||
}
|
||||
|
@ -344,9 +339,9 @@ void app::tickMinute(void) {
|
|||
for (uint8_t id = 0; id < mSys.getNumInverters(); id++) {
|
||||
iv = mSys.getInverterByPos(id);
|
||||
if (NULL == iv)
|
||||
continue; // skip to next inverter
|
||||
continue; // skip to next inverter
|
||||
|
||||
if(!iv->isAvailable(mTimestamp) && !iv->isProducing(mTimestamp) && iv->config->enabled)
|
||||
if (!iv->isAvailable(mTimestamp) && !iv->isProducing(mTimestamp) && iv->config->enabled)
|
||||
mPayload.zeroInverterValues(iv);
|
||||
}
|
||||
}
|
||||
|
@ -355,7 +350,7 @@ void app::tickMinute(void) {
|
|||
void app::tickMidnight(void) {
|
||||
// only triggered if 'reset values at midnight is enabled'
|
||||
uint32_t localTime = gTimezone.toLocal(mTimestamp);
|
||||
uint32_t nxtTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86400); // next midnight local time
|
||||
uint32_t nxtTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86400); // next midnight local time
|
||||
onceAt(std::bind(&app::tickMidnight, this), nxtTrig, "mid2");
|
||||
|
||||
Inverter<> *iv;
|
||||
|
@ -363,7 +358,7 @@ void app::tickMidnight(void) {
|
|||
for (uint8_t id = 0; id < mSys.getNumInverters(); id++) {
|
||||
iv = mSys.getInverterByPos(id);
|
||||
if (NULL == iv)
|
||||
continue; // skip to next inverter
|
||||
continue; // skip to next inverter
|
||||
|
||||
mPayload.zeroInverterValues(iv);
|
||||
mPayload.zeroYieldDay(iv);
|
||||
|
@ -403,8 +398,8 @@ void app::tickSend(void) {
|
|||
} while ((NULL == iv) && ((maxLoop--) > 0));
|
||||
|
||||
if (NULL != iv) {
|
||||
if(iv->config->enabled) {
|
||||
if(iv->ivGen == IV_HM)
|
||||
if (iv->config->enabled) {
|
||||
if (iv->ivGen == IV_HM)
|
||||
mPayload.ivSend(iv);
|
||||
else if(iv->ivGen == IV_MI)
|
||||
mMiPayload.ivSend(iv);
|
||||
|
@ -457,25 +452,25 @@ void app::setupLed(void) {
|
|||
* PIN ---- |<----- 3.3V
|
||||
*
|
||||
* */
|
||||
if(mConfig->led.led0 != 0xff) {
|
||||
if (mConfig->led.led0 != 0xff) {
|
||||
pinMode(mConfig->led.led0, OUTPUT);
|
||||
digitalWrite(mConfig->led.led0, HIGH); // LED off
|
||||
digitalWrite(mConfig->led.led0, HIGH); // LED off
|
||||
}
|
||||
if(mConfig->led.led1 != 0xff) {
|
||||
if (mConfig->led.led1 != 0xff) {
|
||||
pinMode(mConfig->led.led1, OUTPUT);
|
||||
digitalWrite(mConfig->led.led1, HIGH); // LED off
|
||||
digitalWrite(mConfig->led.led1, HIGH); // LED off
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void app::updateLed(void) {
|
||||
if(mConfig->led.led0 != 0xff) {
|
||||
if (mConfig->led.led0 != 0xff) {
|
||||
Inverter<> *iv = mSys.getInverterByPos(0);
|
||||
if (NULL != iv) {
|
||||
if(iv->isProducing(mTimestamp))
|
||||
digitalWrite(mConfig->led.led0, LOW); // LED on
|
||||
if (iv->isProducing(mTimestamp))
|
||||
digitalWrite(mConfig->led.led0, LOW); // LED on
|
||||
else
|
||||
digitalWrite(mConfig->led.led0, HIGH); // LED off
|
||||
digitalWrite(mConfig->led.led0, HIGH); // LED off
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue