LoRa_APRS_iGate/src/wifi_utils.cpp

131 lines
4.5 KiB
C++
Raw Normal View History

2025-07-15 22:28:23 +02:00
/* Copyright (C) 2025 Ricardo Guzman - CA2RXU
2026-02-25 03:33:58 +01:00
*
2025-07-15 22:28:23 +02:00
* This file is part of LoRa APRS iGate.
2026-02-25 03:33:58 +01:00
*
2025-07-15 22:28:23 +02:00
* LoRa APRS iGate is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
2026-02-25 03:33:58 +01:00
* the Free Software Foundation, either version 3 of the License, or
2025-07-15 22:28:23 +02:00
* (at your option) any later version.
2026-02-25 03:33:58 +01:00
*
2025-07-15 22:28:23 +02:00
* LoRa APRS iGate is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2026-02-25 03:33:58 +01:00
*
2025-07-15 22:28:23 +02:00
* You should have received a copy of the GNU General Public License
* along with LoRa APRS iGate. If not, see <https://www.gnu.org/licenses/>.
*/
2023-06-06 16:43:04 +02:00
#include <WiFi.h>
#include "configuration.h"
2026-03-05 23:02:30 +01:00
#include "network_manager.h"
2024-11-05 22:20:44 +01:00
#include "board_pinout.h"
2023-06-08 06:58:10 +02:00
#include "wifi_utils.h"
2023-06-06 17:21:59 +02:00
#include "display.h"
2024-02-25 18:08:24 +01:00
#include "utils.h"
2023-06-06 16:43:04 +02:00
2025-03-10 07:02:48 +01:00
2024-05-22 22:19:45 +02:00
extern Configuration Config;
2026-03-05 23:02:30 +01:00
extern NetworkManager *networkManager;
2023-06-06 16:43:04 +02:00
2024-05-22 22:19:45 +02:00
extern uint8_t myWiFiAPIndex;
extern int myWiFiAPSize;
extern WiFi_AP *currentWiFi;
2026-01-21 02:44:03 +01:00
extern bool backupDigiMode;
2026-01-21 00:56:20 +01:00
extern uint32_t lastServerCheck;
2024-05-22 17:41:08 +02:00
2024-05-22 22:19:45 +02:00
uint8_t wifiCounter = 0;
uint32_t lastBackupDigiTime = millis();
2026-01-20 13:50:09 +01:00
uint32_t lastWiFiCheck = 0;
2024-02-25 16:00:44 +01:00
2024-05-22 21:29:00 +02:00
2023-06-06 16:43:04 +02:00
namespace WIFI_Utils {
2024-02-24 14:09:05 +01:00
void checkWiFi() {
2026-01-20 04:16:24 +01:00
if (Config.digi.ecoMode != 0) return;
2026-01-20 13:50:09 +01:00
uint32_t currentTime = millis();
2026-01-20 04:16:24 +01:00
2026-01-21 02:44:03 +01:00
if (backupDigiMode) {
2026-03-05 23:02:30 +01:00
if (!networkManager->isWiFiConnected() && ((currentTime - lastBackupDigiTime) >= 15 * 60 * 1000)) {
2026-01-20 04:16:24 +01:00
Serial.println("*** Stopping BackUp Digi Mode ***");
2026-01-21 02:44:03 +01:00
backupDigiMode = false;
2026-01-20 04:16:24 +01:00
wifiCounter = 0;
2026-03-05 23:02:30 +01:00
} else if (networkManager->isWiFiConnected()) {
2026-01-20 04:16:24 +01:00
Serial.println("*** WiFi Reconnect Success (Stopping Backup Digi Mode) ***");
2026-01-21 02:44:03 +01:00
backupDigiMode = false;
2026-01-20 04:16:24 +01:00
wifiCounter = 0;
2024-05-22 21:29:00 +02:00
}
2026-01-20 04:16:24 +01:00
}
2024-05-22 21:29:00 +02:00
2026-03-05 23:02:30 +01:00
if (!backupDigiMode && ((currentTime - lastWiFiCheck) >= 30 * 1000) && !networkManager->isWifiAPActive()) {
2026-01-20 13:50:09 +01:00
lastWiFiCheck = currentTime;
2026-03-05 23:02:30 +01:00
if (networkManager->isWiFiConnected()) {
2026-02-11 05:03:54 +01:00
if (Config.digi.backupDigiMode && (currentTime - lastServerCheck > 30 * 1000)) {
2026-01-21 00:56:20 +01:00
Serial.println("*** Server Connection LOST → Backup Digi Mode ***");
2026-01-21 02:44:03 +01:00
backupDigiMode = true;
2026-01-21 00:56:20 +01:00
lastBackupDigiTime = currentTime;
2026-02-25 03:33:58 +01:00
}
2026-01-20 04:16:24 +01:00
} else {
2024-10-07 04:23:22 +02:00
Serial.println("Reconnecting to WiFi...");
2025-11-30 14:15:01 +01:00
WIFI_Utils::startWiFi();
2024-02-24 14:09:05 +01:00
2026-01-21 02:44:03 +01:00
if (Config.digi.backupDigiMode) wifiCounter++;
2024-10-07 04:23:22 +02:00
if (wifiCounter >= 2) {
Serial.println("*** Starting BackUp Digi Mode ***");
2026-01-21 02:44:03 +01:00
backupDigiMode = true;
2026-01-20 13:50:09 +01:00
lastBackupDigiTime = currentTime;
2024-10-07 04:23:22 +02:00
}
2024-05-22 21:29:00 +02:00
}
2024-05-22 17:41:08 +02:00
}
2024-05-22 21:29:00 +02:00
}
2024-05-22 17:41:08 +02:00
2024-02-25 18:02:50 +01:00
void startAutoAP() {
2026-03-05 23:02:30 +01:00
displayShow("", " Starting Auto AP", " Please connect to it " , " loading ...", 1000);
networkManager->setupAP(Config.callsign + "-AP", Config.wifiAutoAP.password);
2024-02-25 18:02:50 +01:00
}
2024-02-24 14:09:05 +01:00
void startWiFi() {
2026-03-05 23:02:30 +01:00
if (currentWiFi->ssid.isEmpty()) {
Serial.println("WiFi SSID not set! Starting Auto AP");
startAutoAP();
return;
}
// TODO: Create generic multi-SSID support in Network Manager
while (!networkManager->isWiFiConnected()) {
2024-10-25 20:22:24 +02:00
displayShow("", "Connecting to WiFi:", "", currentWiFi->ssid + " ...", 0);
2026-03-05 23:02:30 +01:00
networkManager->disconnectWiFi();
networkManager->connectWiFi(currentWiFi->ssid, currentWiFi->password);
if(myWiFiAPIndex >= (myWiFiAPSize - 1)) {
break;
2024-02-24 14:09:05 +01:00
}
2026-03-05 23:02:30 +01:00
myWiFiAPIndex++;
currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
2024-02-24 14:09:05 +01:00
}
2026-03-05 23:02:30 +01:00
2024-04-24 04:25:20 +02:00
#ifdef INTERNAL_LED_PIN
2026-01-20 13:50:09 +01:00
digitalWrite(INTERNAL_LED_PIN, LOW);
2024-02-24 14:09:05 +01:00
#endif
2026-03-05 23:02:30 +01:00
if (networkManager->isWiFiConnected()) {
2025-12-01 17:29:54 +01:00
Serial.print("\nConnected as ");
2026-03-05 23:02:30 +01:00
Serial.print(networkManager->getWiFiIP());
2024-08-13 20:48:08 +02:00
Serial.print(" / MAC Address: ");
2026-03-05 23:02:30 +01:00
Serial.println(networkManager->getWiFimacAddress());
2024-08-14 18:32:34 +02:00
displayShow("", " Connected!!", "" , " loading ...", 1000);
2026-01-20 13:50:09 +01:00
} else {
2024-02-24 14:09:05 +01:00
Serial.println("\nNot connected to WiFi! Starting Auto AP");
2024-08-14 18:32:34 +02:00
displayShow("", " WiFi Not Connected!", "" , " loading ...", 1000);
2024-02-24 14:09:05 +01:00
2024-02-25 18:02:50 +01:00
startAutoAP();
2023-07-31 00:57:31 +02:00
}
2024-01-03 02:12:10 +01:00
}
2024-02-24 14:09:05 +01:00
void setup() {
2025-04-24 14:07:29 +02:00
if (Config.digi.ecoMode == 0) startWiFi();
2024-03-07 17:46:38 +01:00
btStop();
2023-06-08 01:34:18 +02:00
}
2026-03-05 23:02:30 +01:00
}