mirror of
https://github.com/tzapu/WiFiManager.git
synced 2026-04-27 09:05:56 +03:00
[GH-ISSUE #1716] ESP32 Automatic Network Mode Switching with Arduino Sketch Troubleshooting #1454
Labels
No labels
📶 WiFi
🕸️ HTTP
Branch
DEV Help Wanted
Discussion
Documentation
ESP32
Example
Good First Issue
Hotfix
In Progress
Incomplete
Needs Feeback
Priority
QA
Question
Task
Upstream/Dependancy
bug
duplicate
enhancement
invalid
pull-request
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/WiFiManager#1454
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jaaptesla on GitHub (Feb 26, 2024).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1716
---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
}
@tablatronix commented on GitHub (Feb 27, 2024):
or just add configportaltimout=120, restart
@jaaptesla commented on GitHub (Feb 27, 2024):
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 commented on GitHub (Feb 27, 2024):
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 commented on GitHub (Feb 28, 2024):
thats just an example, you cam just as easily autoconnect instead, why would your ap be down for that long ?
@real-bombinho commented on GitHub (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 commented on GitHub (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 commented on GitHub (Mar 10, 2024):
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.