Skip to content

Commit

Permalink
(wlan): Use time delay instead of retires to determine fallback AP start
Browse files Browse the repository at this point in the history
  • Loading branch information
Slider0007 committed Nov 13, 2024
1 parent e1cea5a commit 3764371
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
39 changes: 19 additions & 20 deletions code/components/wlan_ctrl/connect_wlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <esp_log.h>
#include <esp_netif.h>
#include <esp_netif_sntp.h>
#include <esp_timer.h>

#ifdef ENABLE_MQTT
#include "interface_mqtt.h"
Expand All @@ -34,7 +33,7 @@ static struct strWifiState {
bool connected = false;
bool connectionSucessful = false;
bool connectionSupended = false;
int64_t connectionSuspendBaseTime = 0LL;
time_t connectionSuspendBaseTime = 0LL;

int reconnectCnt = 0;
bool fallbackApActive = false;
Expand Down Expand Up @@ -107,25 +106,20 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_
esp_wifi_connect(); // Try to connect again
}

if (wifiState.connectionSucessful) { // Connection was never established (e.g. wrong password), switch to AP
if (wifiState.reconnectCnt >= WLAN_CONNECTION_RETRIES_ERROR_MSG) {
if (wifiState.connectionSucessful) {
if (wifiState.reconnectCnt >= WLAN_RECONNECT_RETRIES_ERROR_MSG) {
wifiState.reconnectCnt = 0;
LogFile.writeToFile(ESP_LOG_ERROR, TAG, "Multiple reconnect attempts failed. Retry to connect");
}
}
else {
if (wifiState.reconnectCnt >= WLAN_CONNECTION_RETRIES_INITIAL_CONNECT) {
// Fallback to AP mode if initial connection cannot be established after defined time since boot [seconds]
if (getUptime() >= WLAN_CONNECT_FALLBACK_AP_DELAY) {
wifiState.reconnectCnt = 0;
LogFile.writeToFile(ESP_LOG_ERROR, TAG, "Failed to establish connection. Start access point (fallback)");
wifiState.fallbackApActive = true;
LogFile.writeToFile(ESP_LOG_ERROR, TAG, "Failed to establish connection. Start access point (fallback)");
deinitWifi();
if (initWifiAp(wifiState.fallbackApActive) != ESP_OK) {
LogFile.writeToFile(ESP_LOG_ERROR, TAG, "Init WLAN access point failed");
}
else {
setStatusLedOff();
setStatusLed(AP_OR_OTA, 3, true);
}
initWifiAp(wifiState.fallbackApActive);
}
}
}
Expand All @@ -142,7 +136,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_
#endif //WLAN_USE_MESH_ROAMING_ACTIVATE_CLIENT_TRIGGERED_QUERIES
#endif //WLAN_USE_MESH_ROAMING

wifiState.connectionSuspendBaseTime = esp_timer_get_time();
wifiState.connectionSuspendBaseTime = getUptime();
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
wifiState.connectionSucessful = true;
Expand Down Expand Up @@ -180,18 +174,18 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_
#endif //ENABLE_MQTT
}
else if (event_id == WIFI_EVENT_AP_START) {
wifiState.connectionSuspendBaseTime = esp_timer_get_time();
wifiState.connectionSuspendBaseTime = getUptime();
}
else if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
LogFile.writeToFile(ESP_LOG_INFO, TAG, "Access point: Client connected. MAC: " + macToString(event->mac));
wifiState.connectionSuspendBaseTime = esp_timer_get_time();
wifiState.connectionSuspendBaseTime = getUptime();
}
else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
LogFile.writeToFile(ESP_LOG_INFO, TAG, "Access point: Client disconnected. MAC: " + macToString(event->mac) +
", Reason: " + std::to_string(event->reason));
wifiState.connectionSuspendBaseTime = esp_timer_get_time();
wifiState.connectionSuspendBaseTime = getUptime();
}
}

Expand All @@ -204,11 +198,11 @@ bool suspendWifiConnection(void)
{
// Set base time to actual time if connection to AP is still established
if (ConfigClass::getInstance()->get()->sectionNetwork.opmode == NETWORK_OPMODE_WLAN_AP_TIMED_OFF && getWifiIsConnected()) {
wifiState.connectionSuspendBaseTime = esp_timer_get_time();
wifiState.connectionSuspendBaseTime = getUptime();
return false;
}

if ((int)((esp_timer_get_time() - wifiState.connectionSuspendBaseTime) / 60000000) >=
if ((int)((getUptime() - wifiState.connectionSuspendBaseTime) / 60) >=
ConfigClass::getInstance()->get()->sectionNetwork.timedOffDelay) {
LogFile.writeToFile(ESP_LOG_WARN, TAG, "Suspending WLAN connection by time (parameter: Timed-Off Delay)");

Expand Down Expand Up @@ -239,7 +233,7 @@ bool resumeWifiConnection(std::string source)
(ConfigClass::getInstance()->get()->sectionNetwork.opmode == NETWORK_OPMODE_WLAN_CLIENT_TIMED_OFF ||
ConfigClass::getInstance()->get()->sectionNetwork.opmode == NETWORK_OPMODE_WLAN_AP_TIMED_OFF))
{
wifiState.connectionSuspendBaseTime = esp_timer_get_time();
wifiState.connectionSuspendBaseTime = getUptime();
wifiState.connectionSupended = false;
LogFile.writeToFile(ESP_LOG_INFO, TAG, "Resuming WLAN connection | Source: " + source);

Expand Down Expand Up @@ -557,6 +551,11 @@ esp_err_t initWifiAp(bool _useDefaultConfig)

wifiState.initialized = true;

if (wifiState.fallbackApActive) {
setStatusLedOff();
setStatusLed(AP_OR_OTA, 3, true);
}

LogFile.writeToFile(ESP_LOG_INFO, TAG, "Init access point mode successful | SSID: " + std::string((char *)wifiConfig.ap.ssid) +
", PW: " + std::string((char *)wifiConfig.ap.password) + ", CH: " + std::to_string(wifiConfig.ap.channel) + ", IP: " + ipCfg.ipAddress);
return ESP_OK;
Expand Down
4 changes: 2 additions & 2 deletions code/include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@

// connect_wlan.cpp
//******************************
#define WLAN_CONNECTION_RETRIES_INITIAL_CONNECT 5
#define WLAN_CONNECTION_RETRIES_ERROR_MSG 10
#define WLAN_CONNECT_FALLBACK_AP_DELAY 120 // Delay in seconds after which the device fall back to AP mode if no connection can be established initially
#define WLAN_RECONNECT_RETRIES_ERROR_MSG 10 // Number of retries after error message will be shown after connection was already successfully established

/* WIFI roaming functionalities 802.11k+v (uses ca. 6kB - 8kB internal RAM; if SCAN CACHE activated: + 1kB / beacon)
PLEASE BE AWARE: The following CONFIG parameters have to to be set in
Expand Down

0 comments on commit 3764371

Please sign in to comment.