Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 Automatic Network Mode Switching with Arduino Sketch Troubleshooting #1716

Open
jaaptesla opened this issue Feb 26, 2024 · 7 comments
Open

Comments

@jaaptesla
Copy link

jaaptesla commented Feb 26, 2024

---esp32---

when esp is connected to client network and client network goes offline
esp goes back in AP mode that is fine so far.

But when it is in AP mode
i would like that the esp checks [periodically] in the background to see if client network is back online.
as soon as client network is online , go back to STA mode and close AP mode.

now i have to reset my ESP the whole time.

i tried to put that into my arduino sketch but it only goes into AP mode.

#include <WiFiUdp.h>
#include <time.h>
#include <WiFiManager.h>

my code
const unsigned long checkClientInterval = 20000; // Check client network every 20 seconds
const unsigned long wifiCheckInterval = 10000; // Check WiFi status every 10 seconds
const unsigned long reconnectInterval = 300000; // 5 minutes

my code

unsigned long lastCheckClientTime = 0;
unsigned long lastBackgroundCheckTime = 0;
unsigned long lastWifiCheckTime = 0;
unsigned long lastReconnectTime = 0;
bool wasConnected = false;

void turnOffAPMode() {
Serial.println("Turning off AP mode...");
WiFi.softAPdisconnect(true);
}

my code
}

my code
}
}
my code
}

my code
}

my code
}
}
return false;
}

my code

Serial.println(buf);
}

bool isClientNetworkConnected() {
return WiFi.status() == WL_CONNECTED;
}

void checkClientNetwork() {
if (!isClientNetworkConnected()) {
if (wasConnected) {
WiFi.disconnect();
wasConnected = false;
}
WiFiManager wifiManager;
wifiManager.autoConnect("GPS-Broadcast");
} else {
turnOffAPMode();
wasConnected = true;
}
}

void checkBackgroundClientNetwork() {
if (!isClientNetworkConnected() && millis() - lastBackgroundCheckTime >= checkClientInterval) {
lastBackgroundCheckTime = millis();
checkClientNetwork();
}
}

void reconnectWiFi() {
if (millis() - lastReconnectTime >= reconnectInterval) {
lastReconnectTime = millis();
Serial.println("Attempting to reconnect to WiFi...");
WiFi.reconnect();
}
}

void setup() {
Serial.begin(115200);

my code

checkClientNetwork(); // Initial check
}

void loop() {
unsigned long currentMillis = millis();

// Check WiFi status and attempt to reconnect if necessary
if (currentMillis - lastWifiCheckTime >= wifiCheckInterval) {
lastWifiCheckTime = currentMillis;
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected, attempting to reconnect...");
reconnectWiFi();
}
}

if (currentMillis - lastCheckClientTime >= checkClientInterval) {
lastCheckClientTime = currentMillis;
checkClientNetwork();
}

my code
}
}

my code
}
}

checkBackgroundClientNetwork(); // Check client network status in the background
}

@jaaptesla jaaptesla changed the title periodic polling in background in AP mode ESP32 Automatic Network Mode Switching with Arduino Sketch Troubleshooting Feb 26, 2024
@tablatronix
Copy link
Collaborator

or just add configportaltimout=120, restart

@jaaptesla
Copy link
Author

If there is no station network available, it will GO AP mode then after 120 then do restart.
So my AP will constantly be rebooting basically.

@jaaptesla
Copy link
Author

jaaptesla commented Feb 27, 2024

`// Global variables
bool isConnectedToStation = false;
bool isAPModeEnabled = false;

// Function declarations
void turnOnAPMode();
void turnOffAPMode();

void setup() {
    Parameters.begin();

    // Set up WiFi Station Mode
    Parameters.setWifiMode(WIFI_MODE_STA);

    // Attempt to connect to an existing network
    if (Parameters.getWifiMode() == WIFI_MODE_STA) {
        WiFi.config(Parameters.getWifiStaIP(), Parameters.getWifiStaGateway(), Parameters.getWifiStaSubnet(), 0U, 0U);
        WiFi.begin(Parameters.getWifiStaSsid(), Parameters.getWifiStaPassword());

        // Waiting for the connection with timeout
        unsigned long startTime = millis();
        while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {
            delay(500);
        }

        // Check if connected
        if (WiFi.status() == WL_CONNECTED) {
            isConnectedToStation = true;
            turnOffAPMode(); // Disable AP mode if connected to the station
        } else {
            Parameters.setWifiMode(WIFI_MODE_AP); // Switch to AP mode if connection fails
            turnOnAPMode(); // Enable AP mode if not connected to the station
        }
    }

    // Start in AP mode if not connected to the station or failed to connect
    if (Parameters.getWifiMode() == WIFI_MODE_AP) {
        WiFi.softAP(Parameters.getWifiSsid(), Parameters.getWifiPassword(), Parameters.getWifiChannel());
        isAPModeEnabled = true;
        wait_for_client(); // Wait for a client to connect
    }
}

void loop() {
    // If connected to the station and not connected to WiFi, attempt reconnection
    if (isConnectedToStation && WiFi.status() != WL_CONNECTED) {
        WiFi.begin(Parameters.getWifiStaSsid(), Parameters.getWifiStaPassword());
        unsigned long startTime = millis();
        while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {
            delay(500);
        }
        if (WiFi.status() == WL_CONNECTED) {
            isConnectedToStation = true;
        } else {
            isConnectedToStation = false;
            turnOnAPMode(); // Switch to AP mode if failed to reconnect
        }
    }

    // custom operations here:
    // doSomething();

    // Delay before next iteration
    delay(1000);
}
`


i would like to use this , but the main wifimanager code is preventing it to run it like so.
if i would customize this into the wifimanager.cpp how would you recommend me to impelent this code
and what section tablatronix ?

@tablatronix
Copy link
Collaborator

thats just an example, you cam just as easily autoconnect instead, why would your ap be down for that long ?

@real-bombinho
Copy link

real-bombinho commented Mar 9, 2024

"why would your ap be down for that long ?"

WiFi APs are not available for a huge number of reasons. Be it avoiding unnecessary stress in densely crowded areas, be it things being mobile, AP built-in timers, restrictions and what not. I never treat a WiFi as given but assume no established connection as standard.

@jaaptesla

Why not use a ticker for checking on the connection and send the WiFiManager AP (WM AP) to sleep via [wm.setEnableConfigPortal(bool);] after such a time of lost connection?

Possibly even better is to limit the Config Portal to the setup() only and use setEnableConfigPortal(false) as last instruction. This way the WM AP should not pop up on reconnection.

@tablatronix
Copy link
Collaborator

tablatronix commented Mar 10, 2024

thanks, but I didn't ask you. I asked op, also those things can all be done in user code nothing prevents you

@real-bombinho
Copy link

real-bombinho commented Mar 10, 2024

thanks, but I didn't ask you. I asked op,

Not that this matters, the potential (un)availability of APs applies to every WiFi device.

also those things can all be done in user code nothing prevents you

I thought that was what I had suggested. I have edited my prior comment to make this more obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants