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
## 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

2
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 {

63
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;
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();
}
} else {
if(mConnected) {
mConnected = false;
mReconnect = true;
mCnt = 50; // try to reconnect in 5 sec
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:

14
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;

Loading…
Cancel
Save