mirror of
https://github.com/richonguzman/LoRa_APRS_iGate.git
synced 2026-03-28 16:52:33 +01:00
Network manager support more Wifi networks
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include <WiFi.h>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* Class for managing network connections
|
||||
@@ -8,13 +9,22 @@
|
||||
class NetworkManager
|
||||
{
|
||||
private:
|
||||
class WiFiNetwork {
|
||||
public:
|
||||
String ssid;
|
||||
String psk;
|
||||
};
|
||||
|
||||
bool _wifiAPmode = false;
|
||||
bool _wifiSTAmode = false;
|
||||
unsigned long _apStartup = 0;
|
||||
unsigned long _apTimeout = 0;
|
||||
|
||||
String _hostName = "";
|
||||
std::vector<WiFiNetwork> _wifiNetworks;
|
||||
|
||||
int _findWiFiNetworkIndex(const String& ssid) const;
|
||||
bool _connectWiFi(const WiFiNetwork& network);
|
||||
void _processAPTimeout();
|
||||
|
||||
public:
|
||||
@@ -34,7 +44,12 @@ public:
|
||||
bool setupAP(String apName, String apPsk = "");
|
||||
bool disableAP();
|
||||
void setAPTimeout(unsigned long timeout);
|
||||
bool connectWiFi(String ssid, String psk);
|
||||
void addWiFiNetwork(const String& ssid, const String& psk = "");
|
||||
void clearWiFiNetworks();
|
||||
bool hasWiFiNetworks() const;
|
||||
size_t getWiFiNetworkCount() const;
|
||||
bool connectWiFi();
|
||||
bool connectWiFi(const String& ssid, const String& psk = "");
|
||||
bool disconnectWiFi();
|
||||
String getWiFiSSID() const;
|
||||
String getWiFiAPSSID() const;
|
||||
|
||||
@@ -10,6 +10,58 @@ NetworkManager::~NetworkManager() { }
|
||||
|
||||
// Private methods
|
||||
|
||||
int NetworkManager::_findWiFiNetworkIndex(const String& ssid) const {
|
||||
for (size_t i = 0; i < _wifiNetworks.size(); i++) {
|
||||
if (_wifiNetworks[i].ssid == ssid) {
|
||||
return static_cast<int>(i);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool NetworkManager::_connectWiFi(const WiFiNetwork& network) {
|
||||
if (network.ssid.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_wifiSTAmode = true;
|
||||
|
||||
if (!_hostName.isEmpty()) {
|
||||
WiFi.setHostname(_hostName.c_str());
|
||||
}
|
||||
|
||||
WiFi.mode(_wifiAPmode ? WIFI_AP_STA : WIFI_STA);
|
||||
|
||||
Serial.println("[NM] Attempting to connect to WiFi: " + network.ssid);
|
||||
WiFi.begin(network.ssid.c_str(), network.psk.c_str());
|
||||
|
||||
Serial.print("[NM] Connecting ");
|
||||
|
||||
int attempts = 0;
|
||||
while (!isWiFiConnected() && attempts < 10) {
|
||||
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
|
||||
attempts++;
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
if (isWiFiConnected()) {
|
||||
Serial.println("[NM] WiFi connected! IP: " + WiFi.localIP().toString());
|
||||
return true;
|
||||
}
|
||||
|
||||
Serial.println("[NM] Failed to connect to WiFi after " + String(attempts) + " attempts. SSID: " +
|
||||
network.ssid);
|
||||
return false;
|
||||
}
|
||||
|
||||
void NetworkManager::_processAPTimeout() {
|
||||
if (!_wifiAPmode || _apTimeout == 0) {
|
||||
return;
|
||||
@@ -88,44 +140,55 @@ void NetworkManager::setAPTimeout(unsigned long timeout) {
|
||||
_apTimeout = timeout;
|
||||
}
|
||||
|
||||
bool NetworkManager::connectWiFi(String ssid, String psk) {
|
||||
_wifiSTAmode = true;
|
||||
|
||||
if (!_hostName.isEmpty()) {
|
||||
WiFi.setHostname(_hostName.c_str());
|
||||
void NetworkManager::addWiFiNetwork(const String& ssid, const String& psk) {
|
||||
if (ssid.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
WiFi.mode(_wifiAPmode ? WIFI_AP_STA : WIFI_STA);
|
||||
|
||||
Serial.println("Attempting to connect to WiFi: " + ssid);
|
||||
WiFi.begin(ssid.c_str(), psk.c_str());
|
||||
|
||||
Serial.print("Connecting ");
|
||||
|
||||
int attempts = 0;
|
||||
while (!isWiFiConnected() && attempts < 10) {
|
||||
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
|
||||
attempts++;
|
||||
int index = _findWiFiNetworkIndex(ssid);
|
||||
if (index >= 0) {
|
||||
Serial.println("[NM] Updating WiFi network: " + ssid);
|
||||
_wifiNetworks[static_cast<size_t>(index)].psk = psk;
|
||||
return;
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
if (isWiFiConnected()) {
|
||||
Serial.println("WiFi connected! IP: " + WiFi.localIP().toString());
|
||||
return true;
|
||||
Serial.println("[NM] Adding WiFi network: " + ssid);
|
||||
WiFiNetwork network;
|
||||
network.ssid = ssid;
|
||||
network.psk = psk;
|
||||
_wifiNetworks.push_back(network);
|
||||
}
|
||||
else {
|
||||
Serial.println("Failed to connect to WiFi after " + String(attempts) + " attempts. SSID: " +
|
||||
ssid);
|
||||
|
||||
void NetworkManager::clearWiFiNetworks() {
|
||||
_wifiNetworks.clear();
|
||||
}
|
||||
|
||||
bool NetworkManager::hasWiFiNetworks() const {
|
||||
return !_wifiNetworks.empty();
|
||||
}
|
||||
|
||||
size_t NetworkManager::getWiFiNetworkCount() const {
|
||||
return _wifiNetworks.size();
|
||||
}
|
||||
|
||||
bool NetworkManager::connectWiFi() {
|
||||
if (_wifiNetworks.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < _wifiNetworks.size(); i++) {
|
||||
disconnectWiFi();
|
||||
if (_connectWiFi(_wifiNetworks[i])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool NetworkManager::connectWiFi(const String& ssid, const String& psk) {
|
||||
addWiFiNetwork(ssid, psk);
|
||||
return connectWiFi();
|
||||
}
|
||||
|
||||
bool NetworkManager::disconnectWiFi() {
|
||||
|
||||
Reference in New Issue
Block a user