diff --git a/src/CHANGES.md b/src/CHANGES.md index b2b24f894..99cf8ac81 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,8 @@ # Development Changes +## 0.8.95 - 2024-03-17 +* fix NTP issues #1440 #1497 #1499 + ## 0.8.94 - 2024-03-16 * switched AsyncWebServer library * Ethernet version now uses same AsyncWebServer library as Wifi version diff --git a/src/app.cpp b/src/app.cpp index bad880f5d..37b0a37a4 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -59,7 +59,7 @@ void app::setup() { #endif // ETHERNET #if !defined(ETHERNET) - mWifi.setup(mConfig, &mTimestamp, std::bind(&app::onNetwork, this, std::placeholders::_1)); + mWifi.setup(mConfig, &mTimestamp, [this](bool gotIp) { this->onNetwork(gotIp); }, [this](bool gotTime) { this->onNtpUpdate(gotTime); }); #if !defined(AP_ONLY) everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL"); #endif @@ -135,7 +135,6 @@ void app::setup() { }); #endif /*ENABLE_SIMULATOR*/ - esp_task_wdt_reset(); regularTickers(); } @@ -164,7 +163,7 @@ void app::loop(void) { //----------------------------------------------------------------------------- void app::onNetwork(bool gotIp) { - DPRINTLN(DBG_DEBUG, F("onNetwork")); + DPRINTLN(DBG_INFO, F("onNetwork")); mNetworkConnected = gotIp; ah::Scheduler::resetTicker(); regularTickers(); //reinstall regular tickers @@ -205,11 +204,9 @@ void app::regularTickers(void) { #endif /*ENABLE_SIMULATOR*/ } -#if defined(ETHERNET) void app::onNtpUpdate(bool gotTime) { mNtpReceived = true; } -#endif /* defined(ETHERNET) */ //----------------------------------------------------------------------------- void app::updateNtp(void) { @@ -250,30 +247,30 @@ void app::updateNtp(void) { //----------------------------------------------------------------------------- void app::tickNtpUpdate(void) { uint32_t nxtTrig = 5; // default: check again in 5 sec - bool isOK = false; #if defined(ETHERNET) - if (!mNtpReceived) - mEth.updateNtpTime(); - else { - mNtpReceived = false; - isOK = true; - } - #else - isOK = mWifi.getNtpTime(); + if (!mNtpReceived) + mEth.updateNtpTime(); + else + mNtpReceived = false; + #else + if (!mNtpReceived) + mWifi.updateNtpTime(); + else + mNtpReceived = false; #endif - if (isOK) { - this->updateNtp(); - nxtTrig = mConfig->ntp.interval * 60; // check again in 12h - - // immediately start communicating - if (mSendFirst) { - mSendFirst = false; - once(std::bind(&app::tickSend, this), 1, "senOn"); - } - mMqttReconnect = false; + updateNtp(); + nxtTrig = mConfig->ntp.interval * 60; // check again in 12h + + // immediately start communicating + if (mSendFirst) { + mSendFirst = false; + once(std::bind(&app::tickSend, this), 1, "senOn"); } + + mMqttReconnect = false; + once(std::bind(&app::tickNtpUpdate, this), nxtTrig, "ntp"); } @@ -561,10 +558,7 @@ void app::resetSystem(void) { mSaveReboot = false; mNetworkConnected = false; - -#if defined(ETHERNET) mNtpReceived = false; -#endif } //----------------------------------------------------------------------------- diff --git a/src/app.h b/src/app.h index 4069662f4..a699bac05 100644 --- a/src/app.h +++ b/src/app.h @@ -299,7 +299,7 @@ class app : public IApp, public ah::Scheduler { #if defined(ETHERNET) mEth.updateNtpTime(); #else /* defined(ETHERNET) */ - mWifi.getNtpTime(); + mWifi.updateNtpTime(); #endif /* defined(ETHERNET) */ } else @@ -389,10 +389,8 @@ class app : public IApp, public ah::Scheduler { } void tickNtpUpdate(void); - #if defined(ETHERNET) void onNtpUpdate(bool gotTime); bool mNtpReceived = false; - #endif /* defined(ETHERNET) */ void updateNtp(void); void triggerTickSend() override { diff --git a/src/defines.h b/src/defines.h index b5e5dc6c1..752b34379 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 94 +#define VERSION_PATCH 95 //------------------------------------- typedef struct { diff --git a/src/wifi/ahoywifi.cpp b/src/wifi/ahoywifi.cpp index 69665b318..95bf0b8a3 100644 --- a/src/wifi/ahoywifi.cpp +++ b/src/wifi/ahoywifi.cpp @@ -30,10 +30,11 @@ ahoywifi::ahoywifi() : mApIp(192, 168, 4, 1) {} */ //----------------------------------------------------------------------------- -void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb) { +void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb, OnTimeCB onTimeCb) { mConfig = config; mUtcTimestamp = utcTimestamp; mAppWifiCb = cb; + mOnTimeCb = onTimeCb; mGotDisconnect = false; mStaConn = DISCONNECTED; @@ -275,7 +276,7 @@ void ahoywifi::setupStation(void) { //----------------------------------------------------------------------------- -bool ahoywifi::getNtpTime(void) { +bool ahoywifi::updateNtpTime(void) { if(IN_STA_MODE != mStaConn) return false; @@ -302,6 +303,7 @@ bool ahoywifi::getNtpTime(void) { *mUtcTimestamp = secsSince1900 - 2208988800UL; // UTC time DPRINTLN(DBG_INFO, "[NTP]: " + ah::getDateTimeStr(*mUtcTimestamp) + " UTC"); + mOnTimeCb(true); return true; } else delay(10); diff --git a/src/wifi/ahoywifi.h b/src/wifi/ahoywifi.h index e971c9979..78ec6ab11 100644 --- a/src/wifi/ahoywifi.h +++ b/src/wifi/ahoywifi.h @@ -21,13 +21,14 @@ class app; class ahoywifi { public: typedef std::function appWifiCb; + typedef std::function OnTimeCB; ahoywifi(); - void setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb); + void setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb, OnTimeCB onTimeCB); void tickWifiLoop(void); - bool getNtpTime(void); + bool updateNtpTime(void); void scanAvailNetworks(void); bool getAvailNetworks(JsonObject obj); void setStopApAllowedMode(bool allowed) { @@ -74,6 +75,7 @@ class ahoywifi { settings_t *mConfig = nullptr; appWifiCb mAppWifiCb; + OnTimeCB mOnTimeCb; DNSServer mDns; IPAddress mApIp;