LoRa_APRS_iGate/src/wifi_utils.cpp

186 lines
6.7 KiB
C++
Raw Permalink Normal View History

2025-07-15 22:28:23 +02:00
/* Copyright (C) 2025 Ricardo Guzman - CA2RXU
*
* This file is part of LoRa APRS iGate.
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* 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"
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;
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
bool WiFiConnected = false;
uint32_t WiFiAutoAPTime = millis();
bool WiFiAutoAPStarted = false;
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-01-20 13:50:09 +01:00
if (WiFi.status() != WL_CONNECTED && ((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;
} else if (WiFi.status() == WL_CONNECTED) {
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-01-21 02:44:03 +01:00
if (!backupDigiMode && ((currentTime - lastWiFiCheck) >= 30 * 1000) && !WiFiAutoAPStarted) {
2026-01-20 13:50:09 +01:00
lastWiFiCheck = currentTime;
2026-01-20 04:16:24 +01:00
if (WiFi.status() == WL_CONNECTED) {
2026-01-21 02:44:03 +01:00
if (Config.digi.backupDigiMode && (currentTime - lastServerCheck > 60 * 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
WiFi.disconnect();
lastBackupDigiTime = currentTime;
2026-01-20 04:16:24 +01:00
}
} else {
2024-10-07 04:23:22 +02:00
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
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() {
WiFi.mode(WIFI_MODE_NULL);
WiFi.mode(WIFI_AP);
2024-10-05 13:48:08 +02:00
WiFi.softAP(Config.callsign + "-AP", Config.wifiAutoAP.password);
2024-02-25 18:02:50 +01:00
WiFiAutoAPTime = millis();
WiFiAutoAPStarted = true;
}
2024-02-24 14:09:05 +01:00
void startWiFi() {
bool startAP = false;
if (currentWiFi->ssid == "") {
startAP = true;
2024-01-03 02:12:10 +01:00
} else {
2024-03-27 19:28:27 +01:00
uint8_t wifiCounter = 0;
2024-09-02 15:40:17 +02:00
String hostName = "iGATE-" + Config.callsign;
2024-09-02 15:12:18 +02:00
WiFi.setHostname(hostName.c_str());
2024-02-24 14:09:05 +01:00
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(500);
unsigned long start = millis();
2024-10-25 20:22:24 +02:00
displayShow("", "Connecting to WiFi:", "", currentWiFi->ssid + " ...", 0);
2025-12-28 15:46:43 +01:00
Serial.print("\nConnecting to WiFi '"); Serial.print(currentWiFi->ssid); Serial.print("' ");
2024-02-24 14:09:05 +01:00
WiFi.begin(currentWiFi->ssid.c_str(), currentWiFi->password.c_str());
2026-01-20 13:50:09 +01:00
while (WiFi.status() != WL_CONNECTED && wifiCounter < myWiFiAPSize) {
2024-02-24 14:09:05 +01:00
delay(500);
2024-04-24 04:25:20 +02:00
#ifdef INTERNAL_LED_PIN
2026-01-20 13:50:09 +01:00
digitalWrite(INTERNAL_LED_PIN, HIGH);
2024-02-24 14:09:05 +01:00
#endif
Serial.print('.');
delay(500);
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
if ((millis() - start) > 10000){
delay(1000);
2026-01-20 13:50:09 +01:00
if (myWiFiAPIndex >= (myWiFiAPSize - 1)) {
2024-02-24 14:09:05 +01:00
myWiFiAPIndex = 0;
2024-03-07 17:46:38 +01:00
wifiCounter++;
2024-02-24 14:09:05 +01:00
} else {
myWiFiAPIndex++;
}
wifiCounter++;
currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
start = millis();
Serial.print("\nConnecting to WiFi '"); Serial.print(currentWiFi->ssid); Serial.println("' ...");
2024-10-25 20:22:24 +02:00
displayShow("", "Connecting to WiFi:", "", currentWiFi->ssid + " ...", 0);
2024-02-24 14:09:05 +01:00
WiFi.disconnect();
WiFi.begin(currentWiFi->ssid.c_str(), currentWiFi->password.c_str());
}
}
}
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
if (WiFi.status() == WL_CONNECTED) {
2025-12-01 17:29:54 +01:00
Serial.print("\nConnected as ");
2024-08-13 20:48:08 +02:00
Serial.print(WiFi.localIP());
Serial.print(" / MAC Address: ");
Serial.println(WiFi.macAddress());
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
startAP = true;
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
}
WiFiConnected = !startAP;
if (startAP) {
Serial.println("\nNot connected to WiFi! Starting Auto AP");
2024-08-14 18:32:34 +02:00
displayShow("", " Starting Auto AP", " Please connect to it " , " 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
2024-10-07 04:23:22 +02:00
void checkAutoAPTimeout() {
2024-10-07 03:30:44 +02:00
if (WiFiAutoAPStarted && Config.wifiAutoAP.timeout > 0) {
2024-02-24 14:09:05 +01:00
if (WiFi.softAPgetStationNum() > 0) {
WiFiAutoAPTime = 0;
} else {
if (WiFiAutoAPTime == 0) {
WiFiAutoAPTime = millis();
2024-10-07 03:30:44 +02:00
} else if ((millis() - WiFiAutoAPTime) > Config.wifiAutoAP.timeout * 60 * 1000) {
2024-02-24 14:09:05 +01:00
Serial.println("Stopping auto AP");
WiFiAutoAPStarted = false;
WiFi.softAPdisconnect(true);
Serial.println("Auto AP stopped (timeout)");
}
}
}
}
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
}
2023-06-06 16:43:04 +02:00
}