diff --git a/src/app.cpp b/src/app.cpp index 4fd1a6a6..13b44fe4 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -225,8 +225,8 @@ void app::onNetwork(bool gotIp) { #if !defined(ETHERNET) if (WIFI_AP == WiFi.getMode()) { mMqttEnabled = false; - everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL"); } + everySec(std::bind(&ahoywifi::tickWifiLoop, &mWifi), "wifiL"); #endif /* !defined(ETHERNET) */ mInnerLoopCb = [this]() { this->loopStandard(); }; } else { diff --git a/src/platformio.ini b/src/platformio.ini index d80aa0c4..600615a5 100644 --- a/src/platformio.ini +++ b/src/platformio.ini @@ -108,6 +108,19 @@ build_flags = ${env.build_flags} monitor_filters = esp32_exception_decoder +[env:esp-32-s2-mini-release] +platform = espressif32@6.3.2 +board = lolin_s2_mini +build_flags = ${env.build_flags} + -DDEF_NRF_CS_PIN=12 + -DDEF_NRF_CE_PIN=3 + -DDEF_NRF_IRQ_PIN=5 + -DDEF_NRF_MISO_PIN=9 + -DDEF_NRF_MOSI_PIN=11 + -DDEF_NRF_SCLK_PIN=7 +monitor_filters = + esp32_exception_decoder + [env:opendtufusionv1] platform = espressif32@6.1.0 board = esp32-s3-devkitc-1 diff --git a/src/wifi/ahoywifi.cpp b/src/wifi/ahoywifi.cpp index 863ca88f..49db3d84 100644 --- a/src/wifi/ahoywifi.cpp +++ b/src/wifi/ahoywifi.cpp @@ -29,7 +29,8 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb) { mUtcTimestamp = utcTimestamp; mAppWifiCb = cb; - mStaConn = DISCONNECTED; + mGotDisconnect = false; + mStaConn = DISCONNECTED; mCnt = 0; mScanActive = false; mScanCnt = 0; @@ -69,12 +70,35 @@ void ahoywifi::setupWifi(bool startAP = false) { } -//----------------------------------------------------------------------------- void ahoywifi::tickWifiLoop() { + static const uint8_t DISCONN_TIMEOUT = 10; + static const uint8_t TIMEOUT = 20; + static const uint8_t SCAN_TIMEOUT = 10; #if !defined(AP_ONLY) - if(mStaConn != GOT_IP) { - if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists - if(mStaConn != IN_AP_MODE) { + uint8_t timeout = (mStaConn == DISCONNECTED) ? DISCONN_TIMEOUT : TIMEOUT; // seconds + + mCnt++; + + switch (mStaConn) { + case IN_STA_MODE: + // Nothing to do + if (mGotDisconnect) { + mStaConn = RESET; + } + return; + case IN_AP_MODE: + if (WiFi.softAPgetStationNum() == 0) { + mCnt = 0; + mDns.stop(); + WiFi.mode(WIFI_AP_STA); + mStaConn = DISCONNECTED; + } else { + mDns.processNextRequest(); + return; + } + break; + case DISCONNECTED: + if (WiFi.softAPgetStationNum() > 0) { mStaConn = IN_AP_MODE; // first time switch to AP Mode if (mScanActive) { @@ -86,56 +110,45 @@ void ahoywifi::tickWifiLoop() { WiFi.mode(WIFI_AP); mDns.start(53, "*", mApIp); mAppWifiCb(true); + mDns.processNextRequest(); + return; + } else if (!mScanActive) { + DBGPRINT(F("scanning APs with SSID ")); + DBGPRINTLN(String(mConfig->sys.stationSsid)); + mScanCnt = 0; + mCnt = 0; + mScanActive = true; +#if defined(ESP8266) + WiFi.scanNetworks(true, true, 0U, ([this]() { + if (mConfig->sys.isHidden) + return (uint8_t*)NULL; + return (uint8_t*)(mConfig->sys.stationSsid); + })()); +#else + WiFi.scanNetworks(true, true, false, 300U, 0U, ([this]() { + if (mConfig->sys.isHidden) + return (char*)NULL; + return (mConfig->sys.stationSsid); + })()); +#endif + return; + } else if(getBSSIDs()) { + // Scan ready + mStaConn = SCAN_READY; + } else { + // In case of a timeout, what do we do? + // For now we start scanning again as the original code did. + // Would be better to into PA mode + + if (isTimeout(SCAN_TIMEOUT)) { + WiFi.scanDelete(); + mScanActive = false; + } } - mDns.processNextRequest(); - return; - } - else if(mStaConn == IN_AP_MODE) { - mCnt = 0; - mDns.stop(); - WiFi.mode(WIFI_AP_STA); - mStaConn = DISCONNECTED; - } - mCnt++; - - uint8_t timeout = (mStaConn == DISCONNECTED) ? 10 : 20; // seconds - if (mStaConn == CONNECTED) // connected but no ip - timeout = 20; - - if(!mScanActive && mBSSIDList.empty() && (mStaConn == DISCONNECTED)) { // start scanning APs with the given SSID - DBGPRINT(F("scanning APs with SSID ")); - DBGPRINTLN(String(mConfig->sys.stationSsid)); - mScanCnt = 0; - mScanActive = true; - #if defined(ESP8266) - WiFi.scanNetworks(true, true, 0U, ([this] () { - if(mConfig->sys.isHidden) - return (uint8_t *)NULL; - return (uint8_t *)(mConfig->sys.stationSsid); - })()); - #else - WiFi.scanNetworks(true, true, false, 300U, 0U, ([this] () { - if(mConfig->sys.isHidden) - return (char*)NULL; - return (mConfig->sys.stationSsid); - })()); - #endif - return; - } - DBGPRINT(F("reconnect in ")); - DBGPRINT(String(timeout-mCnt)); - DBGPRINTLN(F(" seconds")); - if(mScanActive) { - getBSSIDs(); - if((!mScanActive) && (!mBSSIDList.empty())) // scan completed - if ((mCnt % timeout) < timeout - 2) - mCnt = timeout - 2; - } - if((mCnt % timeout) == 0) { // try to reconnect after x sec without connection - mStaConn = CONNECTING; - WiFi.disconnect(); - - if(mBSSIDList.size() > 0) { // get first BSSID in list + break; + case SCAN_READY: + mStaConn = CONNECTING; + mCnt = 0; DBGPRINT(F("try to connect to AP with BSSID:")); uint8_t bssid[6]; for (int j = 0; j < 6; j++) { @@ -144,17 +157,47 @@ void ahoywifi::tickWifiLoop() { DBGPRINT(" " + String(bssid[j], HEX)); } DBGPRINTLN(""); + mGotDisconnect = false; WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd, 0, &bssid[0]); - } - else - mStaConn = DISCONNECTED; - mCnt = 0; - } + break; + case CONNECTING: + if (isTimeout(TIMEOUT)) { + WiFi.disconnect(); + mStaConn = mBSSIDList.empty() ? DISCONNECTED : SCAN_READY; + } + break; + case CONNECTED: + // Connection but no IP yet + if (isTimeout(TIMEOUT) || mGotDisconnect) { + mStaConn = RESET; + } + break; + case GOT_IP: + welcome(WiFi.localIP().toString(), F(" (Station)")); + WiFi.softAPdisconnect(); + WiFi.mode(WIFI_STA); + DBGPRINTLN(F("[WiFi] AP disabled")); + delay(100); + mAppWifiCb(true); + mGotDisconnect = false; + mStaConn = IN_STA_MODE; + break; + case RESET: + mGotDisconnect = false; + 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; + default: + DBGPRINTLN(F("Unhandled status")); + break; } - #endif -} +#endif +} //----------------------------------------------------------------------------- void ahoywifi::setupAp(void) { @@ -213,7 +256,7 @@ void ahoywifi::setupStation(void) { //----------------------------------------------------------------------------- bool ahoywifi::getNtpTime(void) { - if(GOT_IP != mStaConn) + if(IN_STA_MODE != mStaConn) return false; IPAddress timeServer; @@ -314,11 +357,12 @@ bool ahoywifi::getAvailNetworks(JsonObject obj) { } //----------------------------------------------------------------------------- -void ahoywifi::getBSSIDs() { +bool ahoywifi::getBSSIDs() { + bool result = false; int n = WiFi.scanComplete(); if (n < 0) { if (++mScanCnt < 20) - return; + return false; } if(n > 0) { mBSSIDList.clear(); @@ -333,9 +377,11 @@ void ahoywifi::getBSSIDs() { } DBGPRINTLN(""); } + result = true; } mScanActive = false; WiFi.scanDelete(); + return result; } //----------------------------------------------------------------------------- @@ -346,32 +392,17 @@ void ahoywifi::connectionEvent(WiFiStatus_t status) { case CONNECTED: if(mStaConn != CONNECTED) { mStaConn = CONNECTED; + mGotDisconnect = false; DBGPRINTLN(F("\n[WiFi] Connected")); } break; case GOT_IP: mStaConn = GOT_IP; - if (mScanActive) { // maybe another scan has started - WiFi.scanDelete(); - mScanActive = false; - } - welcome(WiFi.localIP().toString(), F(" (Station)")); - WiFi.softAPdisconnect(); - WiFi.mode(WIFI_STA); - DBGPRINTLN(F("[WiFi] AP disabled")); - delay(100); - mAppWifiCb(true); break; case DISCONNECTED: - if(mStaConn != CONNECTING) { - mStaConn = DISCONNECTED; - mCnt = 5; // try to reconnect in 5 sec - setupWifi(); // reconnect with AP / Station setup - mAppWifiCb(false); - DPRINTLN(DBG_INFO, "[WiFi] Connection Lost"); - } + mGotDisconnect = true; break; default: diff --git a/src/wifi/ahoywifi.h b/src/wifi/ahoywifi.h index ef4f2b71..e44d6858 100644 --- a/src/wifi/ahoywifi.h +++ b/src/wifi/ahoywifi.h @@ -32,10 +32,13 @@ class ahoywifi { private: typedef enum WiFiStatus { DISCONNECTED = 0, + SCAN_READY, CONNECTING, CONNECTED, IN_AP_MODE, - GOT_IP + GOT_IP, + IN_STA_MODE, + RESET } WiFiStatus_t; void setupWifi(bool startAP); @@ -43,9 +46,11 @@ class ahoywifi { void setupStation(void); void sendNTPpacket(IPAddress& address); void sortRSSI(int *sort, int n); - void getBSSIDs(void); + bool getBSSIDs(void); void connectionEvent(WiFiStatus_t status); - #if defined(ESP8266) + bool isTimeout(uint8_t timeout) { return (mCnt % timeout) == 0; } + +#if defined(ESP8266) void onConnect(const WiFiEventStationModeConnected& event); void onGotIP(const WiFiEventStationModeGotIP& event); void onDisconnect(const WiFiEventStationModeDisconnected& event); @@ -71,6 +76,7 @@ class ahoywifi { uint8_t mScanCnt; bool mScanActive; + bool mGotDisconnect; std::list mBSSIDList; };