From 52423e679fc4a367096171073fda55f150d7a3df Mon Sep 17 00:00:00 2001 From: lumapu Date: Fri, 30 Dec 2022 22:26:37 +0100 Subject: [PATCH] wifi, code optimization #509 --- src/CHANGES.md | 3 ++ src/defines.h | 2 +- src/wifi/ahoywifi.cpp | 79 ++++++++++++++++++++++++++----------------- src/wifi/ahoywifi.h | 14 ++++++-- 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/CHANGES.md b/src/CHANGES.md index f005f468..cce8144f 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,8 @@ # Changelog +## 0.5.65 +* wifi, code optimization #509 + ## 0.5.64 * channel name can use any character, not limited any more * added `/` to MQTT topic and Inverter name diff --git a/src/defines.h b/src/defines.h index fbc089b2..2a04999f 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 64 +#define VERSION_PATCH 65 //------------------------------------- typedef struct { diff --git a/src/wifi/ahoywifi.cpp b/src/wifi/ahoywifi.cpp index 95eaf432..5214fe01 100644 --- a/src/wifi/ahoywifi.cpp +++ b/src/wifi/ahoywifi.cpp @@ -22,9 +22,8 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp) { mConfig = config; mUtcTimestamp = utcTimestamp; + mStaConn = DISCONNECTED; mCnt = 0; - mConnected = false; - mReconnect = false; mScanActive = false; #if defined(ESP8266) @@ -63,7 +62,7 @@ void ahoywifi::setupWifi(bool startAP = false) { //----------------------------------------------------------------------------- void ahoywifi::tickWifiLoop() { #if !defined(AP_ONLY) - if(mReconnect) { + if(mStaConn != GOT_IP) { if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists mDns.processNextRequest(); if((WIFI_AP_STA == WiFi.getMode()) && !mScanActive) { @@ -79,10 +78,17 @@ void ahoywifi::tickWifiLoop() { } mCnt++; + uint8_t timeout = 10; // seconds + + if (mStaConn == CONNECTED) // connected but no ip + timeout = 20; + DBGPRINT(F("reconnect in ")); - DBGPRINT(String((100-mCnt)/10)); + DBGPRINT(String(timeout-mCnt)); DBGPRINTLN(F(" seconds")); - if((mCnt % 10) == 0) { // try to reconnect after 10 sec without connection + if((mCnt % timeout) == 0) { // try to reconnect after x sec without connection + if(mStaConn != CONNECTED) + mStaConn = CONNECTING; WiFi.reconnect(); mCnt = 0; } @@ -128,11 +134,12 @@ void ahoywifi::setupStation(void) { if(!WiFi.config(ip, gateway, mask, dns1, dns2)) DPRINTLN(DBG_ERROR, F("failed to set static IP!")); } - mReconnect = (WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd) != WL_CONNECTED); + mStaConn = (WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd) != WL_CONNECTED) ? DISCONNECTED : CONNECTED; if(String(mConfig->sys.deviceName) != "") WiFi.hostname(mConfig->sys.deviceName); WiFi.mode(WIFI_AP_STA); + DBGPRINT(F("connect to network '")); DBGPRINT(mConfig->sys.stationSsid); DBGPRINTLN(F("' ...")); @@ -141,7 +148,7 @@ void ahoywifi::setupStation(void) { //----------------------------------------------------------------------------- bool ahoywifi::getNtpTime(void) { - if(!mConnected) + if(CONNECTED != mStaConn) return false; IPAddress timeServer; @@ -234,24 +241,34 @@ void ahoywifi::getAvailNetworks(JsonObject obj) { //----------------------------------------------------------------------------- -void ahoywifi::connectionEvent(bool connected) { - if (connected) { - if(!mConnected) { - mConnected = true; - mReconnect = false; - DBGPRINTLN(F("\n[WiFi] Connected")); - WiFi.mode(WIFI_STA); - DBGPRINTLN(F("[WiFi] AP disabled")); - mDns.stop(); - } - } else { - if(mConnected) { - mConnected = false; - mReconnect = true; - mCnt = 50; // try to reconnect in 5 sec - setupWifi(); // reconnect with AP / Station setup - DPRINTLN(DBG_INFO, "[WiFi] Connection Lost"); - } +void ahoywifi::connectionEvent(WiFiStatus_t status) { + switch(status) { + case CONNECTED: + if(mStaConn != CONNECTED) { + mStaConn = CONNECTED; + DBGPRINTLN(F("\n[WiFi] Connected")); + WiFi.mode(WIFI_STA); + DBGPRINTLN(F("[WiFi] AP disabled")); + mDns.stop(); + } + break; + + case GOT_IP: + mStaConn = GOT_IP; + welcome(WiFi.localIP().toString() + F(" (Station)")); + break; + + case DISCONNECTED: + if(mStaConn != CONNECTING) { + mStaConn = DISCONNECTED; + mCnt = 5; // try to reconnect in 5 sec + setupWifi(); // reconnect with AP / Station setup + DPRINTLN(DBG_INFO, "[WiFi] Connection Lost"); + } + break; + + default: + break; } } @@ -260,17 +277,17 @@ void ahoywifi::connectionEvent(bool connected) { #if defined(ESP8266) //------------------------------------------------------------------------- void ahoywifi::onConnect(const WiFiEventStationModeConnected& event) { - connectionEvent(true); + connectionEvent(CONNECTED); } //------------------------------------------------------------------------- void ahoywifi::onGotIP(const WiFiEventStationModeGotIP& event) { - welcome(WiFi.localIP().toString() + F(" (Station)")); + connectionEvent(GOT_IP); } //------------------------------------------------------------------------- void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) { - connectionEvent(false); + connectionEvent(DISCONNECTED); } #else @@ -281,15 +298,15 @@ void ahoywifi::connectionEvent(bool connected) { switch(event) { case SYSTEM_EVENT_STA_CONNECTED: - connectionEvent(true); + connectionEvent(CONNECTED); break; case SYSTEM_EVENT_STA_GOT_IP: - welcome(WiFi.localIP().toString() + F(" (Station)")); + connectionEvent(GOT_IP); break; case SYSTEM_EVENT_STA_DISCONNECTED: - connectionEvent(false); + connectionEvent(DISCONNECTED); break; default: diff --git a/src/wifi/ahoywifi.h b/src/wifi/ahoywifi.h index 5b3b801c..6eca48e2 100644 --- a/src/wifi/ahoywifi.h +++ b/src/wifi/ahoywifi.h @@ -27,11 +27,19 @@ class ahoywifi { void getAvailNetworks(JsonObject obj); private: + typedef enum WiFiStatus + { + DISCONNECTED = 0, + CONNECTING, + CONNECTED, + GOT_IP + } WiFiStatus_t; + void setupWifi(bool startAP); void setupAp(void); void setupStation(void); void sendNTPpacket(IPAddress& address); - void connectionEvent(bool connected); + void connectionEvent(WiFiStatus_t status); #if defined(ESP8266) void onConnect(const WiFiEventStationModeConnected& event); void onGotIP(const WiFiEventStationModeGotIP& event); @@ -51,8 +59,8 @@ class ahoywifi { WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler, wifiGotIPHandler; #endif - bool mConnected, mReconnect, mDnsActive; - uint8_t mCnt, mClientCnt; + WiFiStatus_t mStaConn; + uint8_t mCnt; uint32_t *mUtcTimestamp; uint8_t mLoopCnt;