Make cargo STA WiFi reconnect non-blocking
This commit is contained in:
@@ -1552,9 +1552,19 @@ String staPasswords[MAX_WIFI_NETWORKS];
|
|||||||
int staPriorities[MAX_WIFI_NETWORKS];
|
int staPriorities[MAX_WIFI_NETWORKS];
|
||||||
int wifiNetworkCount = 0;
|
int wifiNetworkCount = 0;
|
||||||
String activeStaSsid = "";
|
String activeStaSsid = "";
|
||||||
|
bool staAttemptTried[MAX_WIFI_NETWORKS] = {false, false, false};
|
||||||
|
bool staConnectInProgress = false;
|
||||||
|
int activeStaAttemptIndex = -1;
|
||||||
|
unsigned long staConnectAttemptStarted = 0;
|
||||||
unsigned long lastStaReconnectAttempt = 0;
|
unsigned long lastStaReconnectAttempt = 0;
|
||||||
|
const unsigned long STA_CONNECT_TIMEOUT_MS = 8000;
|
||||||
const unsigned long STA_RECONNECT_INTERVAL_MS = 30000;
|
const unsigned long STA_RECONNECT_INTERVAL_MS = 30000;
|
||||||
|
|
||||||
|
void resetStaAttemptRound();
|
||||||
|
bool beginNextStaWifiAttempt();
|
||||||
|
void connectStaWifi();
|
||||||
|
void maintainStaWifi();
|
||||||
|
|
||||||
void loadWifiConfig() {
|
void loadWifiConfig() {
|
||||||
wifiPrefs.begin("wifi", true);
|
wifiPrefs.begin("wifi", true);
|
||||||
|
|
||||||
@@ -2119,85 +2129,110 @@ int findNextWifiIndexByPriority(bool tried[]) {
|
|||||||
return bestIndex;
|
return bestIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void resetStaAttemptRound() {
|
||||||
|
for (int i = 0; i < MAX_WIFI_NETWORKS; i++) {
|
||||||
|
staAttemptTried[i] = false;
|
||||||
|
}
|
||||||
|
staConnectInProgress = false;
|
||||||
|
activeStaAttemptIndex = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool beginNextStaWifiAttempt() {
|
||||||
|
if (wifiNetworkCount <= 0) {
|
||||||
|
Serial.println("STA WiFi not configured.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = findNextWifiIndexByPriority(staAttemptTried);
|
||||||
|
if (i < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
staAttemptTried[i] = true;
|
||||||
|
activeStaAttemptIndex = i;
|
||||||
|
activeStaSsid = "";
|
||||||
|
|
||||||
|
Serial.print("Starting non-blocking STA WiFi attempt priority ");
|
||||||
|
Serial.print(staPriorities[i]);
|
||||||
|
Serial.print(" to: ");
|
||||||
|
Serial.println(staSsids[i]);
|
||||||
|
|
||||||
|
WiFi.disconnect(false, false);
|
||||||
|
WiFi.mode(WIFI_AP_STA);
|
||||||
|
WiFi.begin(staSsids[i].c_str(), staPasswords[i].c_str());
|
||||||
|
|
||||||
|
staConnectAttemptStarted = millis();
|
||||||
|
staConnectInProgress = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void connectStaWifi() {
|
void connectStaWifi() {
|
||||||
if (wifiNetworkCount <= 0) {
|
if (wifiNetworkCount <= 0) {
|
||||||
Serial.println("STA WiFi not configured.");
|
Serial.println("STA WiFi not configured.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFi.disconnect(false);
|
resetStaAttemptRound();
|
||||||
activeStaSsid = "";
|
lastStaReconnectAttempt = millis();
|
||||||
|
|
||||||
bool tried[MAX_WIFI_NETWORKS] = {false, false, false};
|
if (!beginNextStaWifiAttempt()) {
|
||||||
|
Serial.println("No saved STA WiFi networks available. AP remains available.");
|
||||||
for (int attempt = 0; attempt < wifiNetworkCount; attempt++) {
|
|
||||||
int i = findNextWifiIndexByPriority(tried);
|
|
||||||
|
|
||||||
if (i < 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
tried[i] = true;
|
|
||||||
|
|
||||||
Serial.print("Connecting STA WiFi priority ");
|
|
||||||
Serial.print(staPriorities[i]);
|
|
||||||
Serial.print(" to: ");
|
|
||||||
Serial.println(staSsids[i]);
|
|
||||||
|
|
||||||
WiFi.disconnect(false, false);
|
|
||||||
delay(500);
|
|
||||||
WiFi.mode(WIFI_AP_STA);
|
|
||||||
delay(250);
|
|
||||||
|
|
||||||
WiFi.begin(staSsids[i].c_str(), staPasswords[i].c_str());
|
|
||||||
|
|
||||||
unsigned long start = millis();
|
|
||||||
while (WiFi.status() != WL_CONNECTED && millis() - start < 8000) {
|
|
||||||
server.handleClient();
|
|
||||||
oledLoop();
|
|
||||||
delay(100);
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
|
||||||
activeStaSsid = staSsids[i];
|
|
||||||
Serial.println("STA WiFi connected");
|
|
||||||
Serial.print("STA SSID: ");
|
|
||||||
Serial.println(activeStaSsid);
|
|
||||||
Serial.print("STA IP: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
wl_status_t status = WiFi.status();
|
|
||||||
Serial.print("STA WiFi attempt failed. Status: ");
|
|
||||||
Serial.println((int)status);
|
|
||||||
|
|
||||||
wifi_ap_record_t apInfo;
|
|
||||||
if (esp_wifi_sta_get_ap_info(&apInfo) == ESP_OK) {
|
|
||||||
Serial.print("Connected AP RSSI: ");
|
|
||||||
Serial.println(apInfo.rssi);
|
|
||||||
} else {
|
|
||||||
Serial.println("No AP association info available.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("All STA WiFi attempts failed. AP remains available.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void maintainStaWifi() {
|
void maintainStaWifi() {
|
||||||
if (wifiNetworkCount <= 0) return;
|
if (wifiNetworkCount <= 0) return;
|
||||||
|
|
||||||
if (WiFi.status() == WL_CONNECTED) return;
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
if (activeStaSsid.length() == 0) {
|
||||||
|
if (activeStaAttemptIndex >= 0 && activeStaAttemptIndex < wifiNetworkCount) {
|
||||||
|
activeStaSsid = staSsids[activeStaAttemptIndex];
|
||||||
|
} else {
|
||||||
|
activeStaSsid = WiFi.SSID();
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("STA WiFi connected");
|
||||||
|
Serial.print("STA SSID: ");
|
||||||
|
Serial.println(activeStaSsid);
|
||||||
|
Serial.print("STA IP: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
|
||||||
|
staConnectInProgress = false;
|
||||||
|
activeStaAttemptIndex = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (staConnectInProgress) {
|
||||||
|
if (millis() - staConnectAttemptStarted < STA_CONNECT_TIMEOUT_MS) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.print("STA WiFi attempt timed out. Status: ");
|
||||||
|
Serial.println((int)WiFi.status());
|
||||||
|
|
||||||
|
WiFi.disconnect(false, false);
|
||||||
|
staConnectInProgress = false;
|
||||||
|
activeStaAttemptIndex = -1;
|
||||||
|
|
||||||
|
if (beginNextStaWifiAttempt()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("All STA WiFi attempts failed. AP remains available.");
|
||||||
|
resetStaAttemptRound();
|
||||||
|
lastStaReconnectAttempt = millis();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
activeStaSsid = "";
|
||||||
|
|
||||||
if (millis() - lastStaReconnectAttempt < STA_RECONNECT_INTERVAL_MS) return;
|
if (millis() - lastStaReconnectAttempt < STA_RECONNECT_INTERVAL_MS) return;
|
||||||
|
|
||||||
|
Serial.println("STA WiFi disconnected. Trying saved networks by priority in background...");
|
||||||
|
resetStaAttemptRound();
|
||||||
lastStaReconnectAttempt = millis();
|
lastStaReconnectAttempt = millis();
|
||||||
|
beginNextStaWifiAttempt();
|
||||||
Serial.println("STA WiFi disconnected. Trying saved networks by priority...");
|
|
||||||
connectStaWifi();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void printNetworkStatus() {
|
void printNetworkStatus() {
|
||||||
@@ -4247,6 +4282,7 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
|
oledLoop();
|
||||||
|
|
||||||
maintainStaWifi();
|
maintainStaWifi();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user