Browse Source

wifi, code optimization #509

pull/558/head
lumapu 2 years ago
parent
commit
52423e679f
  1. 3
      src/CHANGES.md
  2. 2
      src/defines.h
  3. 63
      src/wifi/ahoywifi.cpp
  4. 14
      src/wifi/ahoywifi.h

3
src/CHANGES.md

@ -1,5 +1,8 @@
# Changelog # Changelog
## 0.5.65
* wifi, code optimization #509
## 0.5.64 ## 0.5.64
* channel name can use any character, not limited any more * channel name can use any character, not limited any more
* added `/` to MQTT topic and Inverter name * added `/` to MQTT topic and Inverter name

2
src/defines.h

@ -13,7 +13,7 @@
//------------------------------------- //-------------------------------------
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 5 #define VERSION_MINOR 5
#define VERSION_PATCH 64 #define VERSION_PATCH 65
//------------------------------------- //-------------------------------------
typedef struct { typedef struct {

63
src/wifi/ahoywifi.cpp

@ -22,9 +22,8 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp) {
mConfig = config; mConfig = config;
mUtcTimestamp = utcTimestamp; mUtcTimestamp = utcTimestamp;
mStaConn = DISCONNECTED;
mCnt = 0; mCnt = 0;
mConnected = false;
mReconnect = false;
mScanActive = false; mScanActive = false;
#if defined(ESP8266) #if defined(ESP8266)
@ -63,7 +62,7 @@ void ahoywifi::setupWifi(bool startAP = false) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void ahoywifi::tickWifiLoop() { void ahoywifi::tickWifiLoop() {
#if !defined(AP_ONLY) #if !defined(AP_ONLY)
if(mReconnect) { if(mStaConn != GOT_IP) {
if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists
mDns.processNextRequest(); mDns.processNextRequest();
if((WIFI_AP_STA == WiFi.getMode()) && !mScanActive) { if((WIFI_AP_STA == WiFi.getMode()) && !mScanActive) {
@ -79,10 +78,17 @@ void ahoywifi::tickWifiLoop() {
} }
mCnt++; mCnt++;
uint8_t timeout = 10; // seconds
if (mStaConn == CONNECTED) // connected but no ip
timeout = 20;
DBGPRINT(F("reconnect in ")); DBGPRINT(F("reconnect in "));
DBGPRINT(String((100-mCnt)/10)); DBGPRINT(String(timeout-mCnt));
DBGPRINTLN(F(" seconds")); 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(); WiFi.reconnect();
mCnt = 0; mCnt = 0;
} }
@ -128,11 +134,12 @@ void ahoywifi::setupStation(void) {
if(!WiFi.config(ip, gateway, mask, dns1, dns2)) if(!WiFi.config(ip, gateway, mask, dns1, dns2))
DPRINTLN(DBG_ERROR, F("failed to set static IP!")); 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) != "") if(String(mConfig->sys.deviceName) != "")
WiFi.hostname(mConfig->sys.deviceName); WiFi.hostname(mConfig->sys.deviceName);
WiFi.mode(WIFI_AP_STA); WiFi.mode(WIFI_AP_STA);
DBGPRINT(F("connect to network '")); DBGPRINT(F("connect to network '"));
DBGPRINT(mConfig->sys.stationSsid); DBGPRINT(mConfig->sys.stationSsid);
DBGPRINTLN(F("' ...")); DBGPRINTLN(F("' ..."));
@ -141,7 +148,7 @@ void ahoywifi::setupStation(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool ahoywifi::getNtpTime(void) { bool ahoywifi::getNtpTime(void) {
if(!mConnected) if(CONNECTED != mStaConn)
return false; return false;
IPAddress timeServer; IPAddress timeServer;
@ -234,24 +241,34 @@ void ahoywifi::getAvailNetworks(JsonObject obj) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void ahoywifi::connectionEvent(bool connected) { void ahoywifi::connectionEvent(WiFiStatus_t status) {
if (connected) { switch(status) {
if(!mConnected) { case CONNECTED:
mConnected = true; if(mStaConn != CONNECTED) {
mReconnect = false; mStaConn = CONNECTED;
DBGPRINTLN(F("\n[WiFi] Connected")); DBGPRINTLN(F("\n[WiFi] Connected"));
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
DBGPRINTLN(F("[WiFi] AP disabled")); DBGPRINTLN(F("[WiFi] AP disabled"));
mDns.stop(); mDns.stop();
} }
} else { break;
if(mConnected) {
mConnected = false; case GOT_IP:
mReconnect = true; mStaConn = GOT_IP;
mCnt = 50; // try to reconnect in 5 sec 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 setupWifi(); // reconnect with AP / Station setup
DPRINTLN(DBG_INFO, "[WiFi] Connection Lost"); DPRINTLN(DBG_INFO, "[WiFi] Connection Lost");
} }
break;
default:
break;
} }
} }
@ -260,17 +277,17 @@ void ahoywifi::connectionEvent(bool connected) {
#if defined(ESP8266) #if defined(ESP8266)
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void ahoywifi::onConnect(const WiFiEventStationModeConnected& event) { void ahoywifi::onConnect(const WiFiEventStationModeConnected& event) {
connectionEvent(true); connectionEvent(CONNECTED);
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void ahoywifi::onGotIP(const WiFiEventStationModeGotIP& event) { void ahoywifi::onGotIP(const WiFiEventStationModeGotIP& event) {
welcome(WiFi.localIP().toString() + F(" (Station)")); connectionEvent(GOT_IP);
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) { void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) {
connectionEvent(false); connectionEvent(DISCONNECTED);
} }
#else #else
@ -281,15 +298,15 @@ void ahoywifi::connectionEvent(bool connected) {
switch(event) { switch(event) {
case SYSTEM_EVENT_STA_CONNECTED: case SYSTEM_EVENT_STA_CONNECTED:
connectionEvent(true); connectionEvent(CONNECTED);
break; break;
case SYSTEM_EVENT_STA_GOT_IP: case SYSTEM_EVENT_STA_GOT_IP:
welcome(WiFi.localIP().toString() + F(" (Station)")); connectionEvent(GOT_IP);
break; break;
case SYSTEM_EVENT_STA_DISCONNECTED: case SYSTEM_EVENT_STA_DISCONNECTED:
connectionEvent(false); connectionEvent(DISCONNECTED);
break; break;
default: default:

14
src/wifi/ahoywifi.h

@ -27,11 +27,19 @@ class ahoywifi {
void getAvailNetworks(JsonObject obj); void getAvailNetworks(JsonObject obj);
private: private:
typedef enum WiFiStatus
{
DISCONNECTED = 0,
CONNECTING,
CONNECTED,
GOT_IP
} WiFiStatus_t;
void setupWifi(bool startAP); void setupWifi(bool startAP);
void setupAp(void); void setupAp(void);
void setupStation(void); void setupStation(void);
void sendNTPpacket(IPAddress& address); void sendNTPpacket(IPAddress& address);
void connectionEvent(bool connected); void connectionEvent(WiFiStatus_t status);
#if defined(ESP8266) #if defined(ESP8266)
void onConnect(const WiFiEventStationModeConnected& event); void onConnect(const WiFiEventStationModeConnected& event);
void onGotIP(const WiFiEventStationModeGotIP& event); void onGotIP(const WiFiEventStationModeGotIP& event);
@ -51,8 +59,8 @@ class ahoywifi {
WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler, wifiGotIPHandler; WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler, wifiGotIPHandler;
#endif #endif
bool mConnected, mReconnect, mDnsActive; WiFiStatus_t mStaConn;
uint8_t mCnt, mClientCnt; uint8_t mCnt;
uint32_t *mUtcTimestamp; uint32_t *mUtcTimestamp;
uint8_t mLoopCnt; uint8_t mLoopCnt;

Loading…
Cancel
Save