mirror of
https://github.com/richonguzman/LoRa_APRS_iGate.git
synced 2026-03-23 21:55:26 +01:00
WiFi utils uses networkManager
This commit is contained in:
parent
ca567e720d
commit
1edb333b1d
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include <WiFi.h>
|
||||
#include "configuration.h"
|
||||
#include "network_manager.h"
|
||||
#include "board_pinout.h"
|
||||
#include "wifi_utils.h"
|
||||
#include "display.h"
|
||||
|
|
@ -25,6 +26,7 @@
|
|||
|
||||
|
||||
extern Configuration Config;
|
||||
extern NetworkManager *networkManager;
|
||||
|
||||
extern uint8_t myWiFiAPIndex;
|
||||
extern int myWiFiAPSize;
|
||||
|
|
@ -34,7 +36,6 @@ extern uint32_t lastServerCheck;
|
|||
|
||||
bool WiFiConnected = false;
|
||||
uint32_t WiFiAutoAPTime = millis();
|
||||
bool WiFiAutoAPStarted = false;
|
||||
uint8_t wifiCounter = 0;
|
||||
uint32_t lastBackupDigiTime = millis();
|
||||
uint32_t lastWiFiCheck = 0;
|
||||
|
|
@ -47,29 +48,27 @@ namespace WIFI_Utils {
|
|||
uint32_t currentTime = millis();
|
||||
|
||||
if (backupDigiMode) {
|
||||
if (WiFi.status() != WL_CONNECTED && ((currentTime - lastBackupDigiTime) >= 15 * 60 * 1000)) {
|
||||
if (!networkManager->isWiFiConnected() && ((currentTime - lastBackupDigiTime) >= 15 * 60 * 1000)) {
|
||||
Serial.println("*** Stopping BackUp Digi Mode ***");
|
||||
backupDigiMode = false;
|
||||
wifiCounter = 0;
|
||||
} else if (WiFi.status() == WL_CONNECTED) {
|
||||
} else if (networkManager->isWiFiConnected()) {
|
||||
Serial.println("*** WiFi Reconnect Success (Stopping Backup Digi Mode) ***");
|
||||
backupDigiMode = false;
|
||||
wifiCounter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!backupDigiMode && ((currentTime - lastWiFiCheck) >= 30 * 1000) && !WiFiAutoAPStarted) {
|
||||
if (!backupDigiMode && ((currentTime - lastWiFiCheck) >= 30 * 1000) && !networkManager->isWifiAPActive()) {
|
||||
lastWiFiCheck = currentTime;
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (networkManager->isWiFiConnected()) {
|
||||
if (Config.digi.backupDigiMode && (currentTime - lastServerCheck > 30 * 1000)) {
|
||||
Serial.println("*** Server Connection LOST → Backup Digi Mode ***");
|
||||
backupDigiMode = true;
|
||||
WiFi.disconnect();
|
||||
lastBackupDigiTime = currentTime;
|
||||
}
|
||||
} else {
|
||||
Serial.println("Reconnecting to WiFi...");
|
||||
WiFi.disconnect();
|
||||
WIFI_Utils::startWiFi();
|
||||
|
||||
if (Config.digi.backupDigiMode) wifiCounter++;
|
||||
|
|
@ -83,84 +82,57 @@ namespace WIFI_Utils {
|
|||
}
|
||||
|
||||
void startAutoAP() {
|
||||
WiFi.mode(WIFI_MODE_NULL);
|
||||
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(Config.callsign + "-AP", Config.wifiAutoAP.password);
|
||||
|
||||
displayShow("", " Starting Auto AP", " Please connect to it " , " loading ...", 1000);
|
||||
networkManager->setupAP(Config.callsign + "-AP", Config.wifiAutoAP.password);
|
||||
WiFiAutoAPTime = millis();
|
||||
WiFiAutoAPStarted = true;
|
||||
}
|
||||
|
||||
void startWiFi() {
|
||||
bool startAP = false;
|
||||
if (currentWiFi->ssid == "") {
|
||||
startAP = true;
|
||||
} else {
|
||||
uint8_t wifiCounter = 0;
|
||||
String hostName = "iGATE-" + Config.callsign;
|
||||
WiFi.setHostname(hostName.c_str());
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
delay(500);
|
||||
unsigned long start = millis();
|
||||
displayShow("", "Connecting to WiFi:", "", currentWiFi->ssid + " ...", 0);
|
||||
Serial.print("\nConnecting to WiFi '"); Serial.print(currentWiFi->ssid); Serial.print("' ");
|
||||
WiFi.begin(currentWiFi->ssid.c_str(), currentWiFi->password.c_str());
|
||||
while (WiFi.status() != WL_CONNECTED && wifiCounter < myWiFiAPSize) {
|
||||
delay(500);
|
||||
#ifdef INTERNAL_LED_PIN
|
||||
digitalWrite(INTERNAL_LED_PIN, HIGH);
|
||||
#endif
|
||||
Serial.print('.');
|
||||
delay(500);
|
||||
#ifdef INTERNAL_LED_PIN
|
||||
digitalWrite(INTERNAL_LED_PIN, LOW);
|
||||
#endif
|
||||
if ((millis() - start) > 10000){
|
||||
delay(1000);
|
||||
if (myWiFiAPIndex >= (myWiFiAPSize - 1)) {
|
||||
myWiFiAPIndex = 0;
|
||||
wifiCounter++;
|
||||
} else {
|
||||
myWiFiAPIndex++;
|
||||
}
|
||||
wifiCounter++;
|
||||
currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
|
||||
start = millis();
|
||||
Serial.print("\nConnecting to WiFi '"); Serial.print(currentWiFi->ssid); Serial.println("' ...");
|
||||
displayShow("", "Connecting to WiFi:", "", currentWiFi->ssid + " ...", 0);
|
||||
WiFi.disconnect();
|
||||
WiFi.begin(currentWiFi->ssid.c_str(), currentWiFi->password.c_str());
|
||||
}
|
||||
}
|
||||
if (currentWiFi->ssid.isEmpty()) {
|
||||
Serial.println("WiFi SSID not set! Starting Auto AP");
|
||||
startAutoAP();
|
||||
return;
|
||||
}
|
||||
|
||||
String hostName = "iGATE-" + Config.callsign;
|
||||
WiFi.setHostname(hostName.c_str());
|
||||
|
||||
// TODO: Create generic multi-SSID support in Network Manager
|
||||
while (!networkManager->isWiFiConnected()) {
|
||||
displayShow("", "Connecting to WiFi:", "", currentWiFi->ssid + " ...", 0);
|
||||
networkManager->disconnectWiFi();
|
||||
networkManager->connectWiFi(currentWiFi->ssid, currentWiFi->password);
|
||||
|
||||
if(myWiFiAPIndex >= (myWiFiAPSize - 1)) {
|
||||
break;
|
||||
}
|
||||
|
||||
myWiFiAPIndex++;
|
||||
currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
|
||||
}
|
||||
|
||||
#ifdef INTERNAL_LED_PIN
|
||||
digitalWrite(INTERNAL_LED_PIN, LOW);
|
||||
#endif
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (networkManager->isWiFiConnected()) {
|
||||
Serial.print("\nConnected as ");
|
||||
Serial.print(WiFi.localIP());
|
||||
Serial.print(networkManager->getWiFiIP());
|
||||
Serial.print(" / MAC Address: ");
|
||||
Serial.println(WiFi.macAddress());
|
||||
Serial.println(networkManager->getWiFimacAddress());
|
||||
displayShow("", " Connected!!", "" , " loading ...", 1000);
|
||||
} else {
|
||||
startAP = true;
|
||||
|
||||
Serial.println("\nNot connected to WiFi! Starting Auto AP");
|
||||
displayShow("", " WiFi Not Connected!", "" , " loading ...", 1000);
|
||||
}
|
||||
WiFiConnected = !startAP;
|
||||
if (startAP) {
|
||||
Serial.println("\nNot connected to WiFi! Starting Auto AP");
|
||||
displayShow("", " Starting Auto AP", " Please connect to it " , " loading ...", 1000);
|
||||
|
||||
startAutoAP();
|
||||
}
|
||||
|
||||
//TODO: Use network manager whenever this variable is used
|
||||
WiFiConnected = networkManager->isWiFiConnected();
|
||||
}
|
||||
|
||||
void checkAutoAPTimeout() {
|
||||
if (WiFiAutoAPStarted && Config.wifiAutoAP.timeout > 0) {
|
||||
if (networkManager->isWifiAPActive() && Config.wifiAutoAP.timeout > 0) {
|
||||
if (WiFi.softAPgetStationNum() > 0) {
|
||||
WiFiAutoAPTime = 0;
|
||||
} else {
|
||||
|
|
@ -169,8 +141,7 @@ namespace WIFI_Utils {
|
|||
} else if ((millis() - WiFiAutoAPTime) > Config.wifiAutoAP.timeout * 60 * 1000) {
|
||||
Serial.println("Stopping auto AP");
|
||||
|
||||
WiFiAutoAPStarted = false;
|
||||
WiFi.softAPdisconnect(true);
|
||||
networkManager->disableAP();
|
||||
|
||||
Serial.println("Auto AP stopped (timeout)");
|
||||
}
|
||||
|
|
@ -183,4 +154,4 @@ namespace WIFI_Utils {
|
|||
btStop();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue