-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Comments
or just add configportaltimout=120, restart |
If there is no station network available, it will GO AP mode then after 120 then do restart. |
i would like to use this , but the main wifimanager code is preventing it to run it like so. |
thats just an example, you cam just as easily autoconnect instead, why would your ap be down for that long ? |
"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. 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. |
thanks, but I didn't ask you. I asked op, also those things can all be done in user code nothing prevents you |
Not that this matters, the potential (un)availability of APs applies to every WiFi device.
I thought that was what I had suggested. I have edited my prior comment to make this more obvious. |
---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
}
The text was updated successfully, but these errors were encountered: