mirror of
https://github.com/richonguzman/LoRa_APRS_iGate.git
synced 2026-02-09 09:14:19 +01:00
Merge 4b0d5a4ca0 into fe705519cb
This commit is contained in:
commit
f098b8a086
69
include/network_manager.h
Normal file
69
include/network_manager.h
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include <WiFi.h>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* Class for managing network connections
|
||||
*/
|
||||
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:
|
||||
// Constructor
|
||||
NetworkManager();
|
||||
|
||||
// Destructor
|
||||
~NetworkManager();
|
||||
|
||||
// Initialize network module
|
||||
bool setup();
|
||||
void loop();
|
||||
|
||||
void setHostName(const String& hostName);
|
||||
|
||||
// WiFi methods
|
||||
bool setupAP(String apName, String apPsk = "");
|
||||
bool disableAP();
|
||||
void setAPTimeout(unsigned long timeout);
|
||||
void addWiFiNetwork(const String& ssid, const String& psk = "");
|
||||
void clearWiFiNetworks();
|
||||
bool connectWiFi();
|
||||
bool connectWiFi(const String& ssid, const String& psk = "");
|
||||
bool disconnectWiFi();
|
||||
String getWiFiSSID() const;
|
||||
String getWiFiAPSSID() const;
|
||||
IPAddress getWiFiIP() const;
|
||||
IPAddress getWiFiAPIP() const;
|
||||
wifi_mode_t getWiFiMode() const;
|
||||
uint8_t* getWiFimacAddress(uint8_t* mac);
|
||||
String getWiFimacAddress(void) const;
|
||||
|
||||
// Check if any network is available
|
||||
bool isConnected() const;
|
||||
|
||||
// Check if specific network is connected
|
||||
bool isWiFiConnected() const;
|
||||
bool isEthernetConnected() const;
|
||||
bool isModemConnected() const;
|
||||
|
||||
bool isWifiAPActive() const;
|
||||
};
|
||||
|
|
@ -27,7 +27,6 @@ namespace WIFI_Utils {
|
|||
void checkWiFi();
|
||||
void startAutoAP();
|
||||
void startWiFi();
|
||||
void checkAutoAPTimeout();
|
||||
void setup();
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,9 +41,10 @@ ___________________________________________________________________*/
|
|||
#include <ElegantOTA.h>
|
||||
#include <TinyGPS++.h>
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <vector>
|
||||
#include "configuration.h"
|
||||
#include "network_manager.h"
|
||||
#include "aprs_is_utils.h"
|
||||
#include "station_utils.h"
|
||||
#include "battery_utils.h"
|
||||
|
|
@ -79,9 +80,7 @@ WiFiClient mqttClient;
|
|||
bool gpsInfoToggle = false;
|
||||
#endif
|
||||
|
||||
uint8_t myWiFiAPIndex = 0;
|
||||
int myWiFiAPSize = Config.wifiAPs.size();
|
||||
WiFi_AP *currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
|
||||
NetworkManager *networkManager;
|
||||
|
||||
bool isUpdatingOTA = false;
|
||||
uint32_t lastBatteryCheck = 0;
|
||||
|
|
@ -101,6 +100,10 @@ String firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seven
|
|||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
networkManager = new NetworkManager();
|
||||
networkManager->setup();
|
||||
networkManager->setAPTimeout(Config.wifiAutoAP.timeout * 60 * 1000); // Convert minutes to milliseconds
|
||||
networkManager->setHostName("iGATE-" + Config.callsign);
|
||||
POWER_Utils::setup();
|
||||
Utils::setupDisplay();
|
||||
LoRa_Utils::setup();
|
||||
|
|
@ -132,7 +135,7 @@ void loop() {
|
|||
Utils::checkSleepByLowBatteryVoltage(1);
|
||||
SLEEP_Utils::startSleeping();
|
||||
} else {
|
||||
WIFI_Utils::checkAutoAPTimeout();
|
||||
networkManager->loop();
|
||||
|
||||
if (isUpdatingOTA) {
|
||||
ElegantOTA.loop();
|
||||
|
|
@ -161,11 +164,14 @@ void loop() {
|
|||
#endif
|
||||
|
||||
#ifdef HAS_A7670
|
||||
// TODO: Make this part of Network manager, and use ESP-IDF network stack instead manual AT commands
|
||||
if (Config.aprs_is.active && !modemLoggedToAPRSIS) A7670_Utils::APRS_IS_connect();
|
||||
#else
|
||||
WIFI_Utils::checkWiFi();
|
||||
if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !aprsIsClient.connected()) APRS_IS_Utils::connect();
|
||||
if (Config.mqtt.active && (WiFi.status() == WL_CONNECTED) && !mqttClient.connected()) MQTT_Utils::connect();
|
||||
if (networkManager->isConnected()) {
|
||||
if (Config.aprs_is.active && !aprsIsClient.connected()) APRS_IS_Utils::connect();
|
||||
if (Config.mqtt.active && !mqttClient.connected()) MQTT_Utils::connect();
|
||||
}
|
||||
#endif
|
||||
|
||||
NTP_Utils::update();
|
||||
|
|
@ -174,7 +180,7 @@ void loop() {
|
|||
|
||||
Utils::checkDisplayInterval();
|
||||
Utils::checkBeaconInterval();
|
||||
|
||||
|
||||
APRS_IS_Utils::checkStatus(); // Need that to update display, maybe split this and send APRSIS status to display func?
|
||||
|
||||
String packet = "";
|
||||
|
|
@ -217,4 +223,4 @@ void loop() {
|
|||
Utils::checkRebootTime();
|
||||
Utils::checkSleepByLowBatteryVoltage(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@
|
|||
*/
|
||||
|
||||
#include <APRSPacketLib.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include "configuration.h"
|
||||
#include "network_manager.h"
|
||||
#include "aprs_is_utils.h"
|
||||
#include "station_utils.h"
|
||||
#include "board_pinout.h"
|
||||
|
|
@ -32,6 +33,7 @@
|
|||
|
||||
|
||||
extern Configuration Config;
|
||||
extern NetworkManager *networkManager;
|
||||
extern WiFiClient aprsIsClient;
|
||||
extern uint32_t lastScreenOn;
|
||||
extern String firstLine;
|
||||
|
|
@ -91,7 +93,7 @@ namespace APRS_IS_Utils {
|
|||
|
||||
void checkStatus() {
|
||||
String wifiState, aprsisState;
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (networkManager->isWiFiConnected()) {
|
||||
wifiState = "OK";
|
||||
} else {
|
||||
if (backUpDigiMode || Config.digi.ecoMode == 1 || Config.digi.ecoMode == 2) {
|
||||
|
|
@ -388,7 +390,7 @@ namespace APRS_IS_Utils {
|
|||
}
|
||||
|
||||
void firstConnection() {
|
||||
if (Config.aprs_is.active && (WiFi.status() == WL_CONNECTED) && !aprsIsClient.connected()) {
|
||||
if (Config.aprs_is.active && networkManager->isConnected() && !aprsIsClient.connected()) {
|
||||
connect();
|
||||
while (!passcodeValid) {
|
||||
listenAPRSIS();
|
||||
|
|
|
|||
|
|
@ -17,8 +17,8 @@
|
|||
*/
|
||||
|
||||
#include <RadioLib.h>
|
||||
#include <WiFi.h>
|
||||
#include "configuration.h"
|
||||
#include "network_manager.h"
|
||||
#include "aprs_is_utils.h"
|
||||
#include "station_utils.h"
|
||||
#include "board_pinout.h"
|
||||
|
|
@ -29,6 +29,7 @@
|
|||
|
||||
|
||||
extern Configuration Config;
|
||||
extern NetworkManager *networkManager;
|
||||
extern uint32_t lastRxTime;
|
||||
extern bool packetIsBeacon;
|
||||
|
||||
|
|
@ -183,7 +184,7 @@ namespace LoRa_Utils {
|
|||
int state = radio.transmit("\x3c\xff\x01" + newPacket);
|
||||
transmitFlag = true;
|
||||
if (state == RADIOLIB_ERR_NONE) {
|
||||
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
|
||||
if (Config.syslog.active && networkManager->isConnected()) {
|
||||
SYSLOG_Utils::log(3, newPacket, 0, 0.0, 0); // TX
|
||||
}
|
||||
Utils::print("---> LoRa Packet Tx : ");
|
||||
|
|
@ -245,7 +246,7 @@ namespace LoRa_Utils {
|
|||
receivedPackets.push_back(receivedPacket);
|
||||
}
|
||||
|
||||
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
|
||||
if (Config.syslog.active && networkManager->isConnected()) {
|
||||
SYSLOG_Utils::log(1, packet, rssi, snr, freqError); // RX
|
||||
}
|
||||
} else {
|
||||
|
|
@ -259,7 +260,7 @@ namespace LoRa_Utils {
|
|||
snr = radio.getSNR();
|
||||
freqError = radio.getFrequencyError();
|
||||
Utils::println(F("CRC error!"));
|
||||
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
|
||||
if (Config.syslog.active && networkManager->isConnected()) {
|
||||
SYSLOG_Utils::log(0, packet, rssi, snr, freqError); // CRC
|
||||
}
|
||||
packet = "";
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
* along with LoRa APRS iGate. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <PubSubClient.h>
|
||||
#include "configuration.h"
|
||||
#include "station_utils.h"
|
||||
|
|
|
|||
246
src/network_manager.cpp
Normal file
246
src/network_manager.cpp
Normal file
|
|
@ -0,0 +1,246 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
#include "network_manager.h"
|
||||
|
||||
// Constructor
|
||||
NetworkManager::NetworkManager() { }
|
||||
|
||||
// Destructor
|
||||
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;
|
||||
}
|
||||
|
||||
// If any station is connected, reset the timer
|
||||
if (WiFi.softAPgetStationNum() > 0) {
|
||||
_apStartup = millis();
|
||||
return;
|
||||
}
|
||||
|
||||
if (millis() - _apStartup > _apTimeout) {
|
||||
Serial.println("AP timeout reached. Disabling AP mode.");
|
||||
disableAP();
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize
|
||||
bool NetworkManager::setup() {
|
||||
Serial.println("Initializing Networking...");
|
||||
return true;
|
||||
}
|
||||
|
||||
void NetworkManager::loop() {
|
||||
if (_wifiAPmode) {
|
||||
_processAPTimeout();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkManager::setHostName(const String& hostName) {
|
||||
_hostName = hostName;
|
||||
}
|
||||
|
||||
// WiFi methods
|
||||
|
||||
bool NetworkManager::setupAP(String apName, String apPsk) {
|
||||
_wifiAPmode = true;
|
||||
|
||||
Serial.println("Starting AP mode: " + apName);
|
||||
|
||||
// Full WiFi reset sequence
|
||||
WiFi.disconnect(true);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
delay(200);
|
||||
|
||||
// Set up AP mode with optimized settings
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
bool apStarted = WiFi.softAP(apName.c_str(), apPsk.c_str());
|
||||
delay(1000); // Give AP time to fully initialize
|
||||
|
||||
if (apStarted) {
|
||||
Serial.println("AP setup successful");
|
||||
_apStartup = millis();
|
||||
}
|
||||
else {
|
||||
Serial.println("AP setup failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
IPAddress apIP = getWiFiAPIP();
|
||||
Serial.println("AP IP assigned: " + apIP.toString());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NetworkManager::disableAP() {
|
||||
WiFi.mode(_wifiSTAmode ? WIFI_STA : WIFI_OFF);
|
||||
_wifiAPmode = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NetworkManager::setAPTimeout(unsigned long timeout) {
|
||||
Serial.println("Setting AP timeout to " + String(timeout / 1000) + " sec");
|
||||
_apTimeout = timeout;
|
||||
}
|
||||
|
||||
void NetworkManager::addWiFiNetwork(const String& ssid, const String& psk) {
|
||||
if (ssid.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
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("[NM] Adding WiFi network: " + ssid);
|
||||
WiFiNetwork network;
|
||||
network.ssid = ssid;
|
||||
network.psk = psk;
|
||||
_wifiNetworks.push_back(network);
|
||||
}
|
||||
|
||||
void NetworkManager::clearWiFiNetworks() {
|
||||
_wifiNetworks.clear();
|
||||
}
|
||||
|
||||
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() {
|
||||
WiFi.disconnect(true);
|
||||
WiFi.mode(_wifiAPmode ? WIFI_AP : WIFI_OFF);
|
||||
|
||||
_wifiSTAmode = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
String NetworkManager::getWiFiSSID() const {
|
||||
return WiFi.SSID();
|
||||
}
|
||||
|
||||
String NetworkManager::getWiFiAPSSID() const {
|
||||
return WiFi.softAPSSID();
|
||||
}
|
||||
|
||||
IPAddress NetworkManager::getWiFiIP() const {
|
||||
return WiFi.localIP();
|
||||
}
|
||||
|
||||
IPAddress NetworkManager::getWiFiAPIP() const {
|
||||
return WiFi.softAPIP();
|
||||
}
|
||||
|
||||
wifi_mode_t NetworkManager::getWiFiMode() const {
|
||||
return WiFi.getMode();
|
||||
}
|
||||
|
||||
uint8_t* NetworkManager::getWiFimacAddress(uint8_t* mac) {
|
||||
return WiFi.macAddress(mac);
|
||||
}
|
||||
|
||||
String NetworkManager::getWiFimacAddress(void) const {
|
||||
return WiFi.macAddress();
|
||||
}
|
||||
|
||||
// Check if network is available
|
||||
bool NetworkManager::isConnected() const {
|
||||
return isWiFiConnected() || isEthernetConnected() || isModemConnected();
|
||||
}
|
||||
|
||||
// Check if WiFi is connected
|
||||
bool NetworkManager::isWiFiConnected() const {
|
||||
return _wifiSTAmode ? WiFi.status() == WL_CONNECTED : false;
|
||||
}
|
||||
|
||||
bool NetworkManager::isWifiAPActive() const {
|
||||
return _wifiAPmode;
|
||||
}
|
||||
|
||||
// Check if Ethernet is connected
|
||||
bool NetworkManager::isEthernetConnected() const {
|
||||
// Implement Ethernet connection check logic here
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if Modem is connected
|
||||
bool NetworkManager::isModemConnected() const {
|
||||
// Implement Modem connection check logic here
|
||||
return false;
|
||||
}
|
||||
|
|
@ -18,14 +18,14 @@
|
|||
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <WiFi.h>
|
||||
#include "configuration.h"
|
||||
#include "network_manager.h"
|
||||
#include "ntp_utils.h"
|
||||
#include "time.h"
|
||||
|
||||
|
||||
extern Configuration Config;
|
||||
|
||||
extern NetworkManager *networkManager;
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient* timeClient;
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ NTPClient* timeClient;
|
|||
namespace NTP_Utils {
|
||||
|
||||
void setup() {
|
||||
if (WiFi.status() == WL_CONNECTED && Config.digi.ecoMode == 0 && Config.callsign != "NOCALL-10") {
|
||||
if (networkManager->isConnected() && Config.digi.ecoMode == 0 && Config.callsign != "NOCALL-10") {
|
||||
int gmt = Config.ntp.gmtCorrection * 3600;
|
||||
timeClient = new NTPClient(ntpUDP, Config.ntp.server.c_str(), gmt, 15 * 60 * 1000); // Update interval 15 min
|
||||
timeClient->begin();
|
||||
|
|
@ -41,11 +41,11 @@ namespace NTP_Utils {
|
|||
}
|
||||
|
||||
void update() {
|
||||
if (WiFi.status() == WL_CONNECTED && Config.digi.ecoMode == 0 && Config.callsign != "NOCALL-10") timeClient->update();
|
||||
if (networkManager->isConnected() && Config.digi.ecoMode == 0 && Config.callsign != "NOCALL-10") timeClient->update();
|
||||
}
|
||||
|
||||
String getFormatedTime() {
|
||||
if (WiFi.status() == WL_CONNECTED && Config.digi.ecoMode == 0) return timeClient->getFormattedTime();
|
||||
if (networkManager->isConnected() && Config.digi.ecoMode == 0) return timeClient->getFormattedTime();
|
||||
return "DigiEcoMode Active";
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,13 +17,14 @@
|
|||
*/
|
||||
|
||||
#include <WiFiUdp.h>
|
||||
#include <WiFi.h>
|
||||
#include "configuration.h"
|
||||
#include "network_manager.h"
|
||||
#include "syslog_utils.h"
|
||||
#include "gps_utils.h"
|
||||
|
||||
|
||||
extern Configuration Config;
|
||||
extern NetworkManager *networkManager;
|
||||
extern String versionDate;
|
||||
extern String versionNumber;
|
||||
|
||||
|
|
@ -33,7 +34,7 @@ WiFiUDP udpClient;
|
|||
namespace SYSLOG_Utils {
|
||||
|
||||
void log(const uint8_t type, const String& packet, const int rssi, const float snr, const int freqError) {
|
||||
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
|
||||
if (Config.syslog.active && networkManager->isConnected()) {
|
||||
String syslogPacket = "<165>1 - ";
|
||||
syslogPacket.concat(Config.callsign);
|
||||
syslogPacket.concat(" CA2RXU_LoRa_iGate_");
|
||||
|
|
@ -139,7 +140,7 @@ namespace SYSLOG_Utils {
|
|||
}
|
||||
|
||||
void setup() {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (networkManager->isConnected()) {
|
||||
udpClient.begin(0);
|
||||
udpClient.beginPacket("syslog.trackiot.cc", 15243);
|
||||
String hiddenLogPacket = Config.callsign + "," + versionDate;
|
||||
|
|
|
|||
|
|
@ -18,9 +18,9 @@
|
|||
|
||||
#include <APRSPacketLib.h>
|
||||
#include <TinyGPS++.h>
|
||||
#include <WiFi.h>
|
||||
#include "telemetry_utils.h"
|
||||
#include "configuration.h"
|
||||
#include "network_manager.h"
|
||||
#include "station_utils.h"
|
||||
#include "battery_utils.h"
|
||||
#include "aprs_is_utils.h"
|
||||
|
|
@ -36,6 +36,7 @@
|
|||
|
||||
|
||||
extern Configuration Config;
|
||||
extern NetworkManager *networkManager;
|
||||
extern TinyGPSPlus gps;
|
||||
extern String versionDate;
|
||||
extern String firstLine;
|
||||
|
|
@ -51,7 +52,6 @@ extern int rssi;
|
|||
extern float snr;
|
||||
extern int freqError;
|
||||
extern String distance;
|
||||
extern bool WiFiConnected;
|
||||
extern int wxModuleType;
|
||||
extern bool backUpDigiMode;
|
||||
extern bool shouldSleepLowVoltage;
|
||||
|
|
@ -74,7 +74,7 @@ namespace Utils {
|
|||
void processStatus() {
|
||||
String status = APRSPacketLib::generateBasePacket(Config.callsign, "APLRG1", Config.beacon.path);
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED && Config.aprs_is.active && Config.beacon.sendViaAPRSIS) {
|
||||
if (networkManager->isConnected() && Config.aprs_is.active && Config.beacon.sendViaAPRSIS) {
|
||||
delay(1000);
|
||||
status.concat(",qAC:>");
|
||||
status.concat(Config.beacon.statusPacket);
|
||||
|
|
@ -93,12 +93,12 @@ namespace Utils {
|
|||
String getLocalIP() {
|
||||
if (Config.digi.ecoMode == 1 || Config.digi.ecoMode == 2) {
|
||||
return "** WiFi AP Killed **";
|
||||
} else if (!WiFiConnected) {
|
||||
return "IP : 192.168.4.1";
|
||||
} else if (!networkManager->isWiFiConnected() && networkManager->isWifiAPActive()) {
|
||||
return "IP : " + String(networkManager->getWiFiAPIP());
|
||||
} else if (backUpDigiMode) {
|
||||
return "- BACKUP DIGI MODE -";
|
||||
} else {
|
||||
return "IP : " + String(WiFi.localIP()[0]) + "." + String(WiFi.localIP()[1]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[3]);
|
||||
return "IP : " + String(networkManager->getWiFiIP());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,15 +26,10 @@
|
|||
|
||||
|
||||
extern Configuration Config;
|
||||
extern NetworkManager *networkManager;
|
||||
|
||||
extern uint8_t myWiFiAPIndex;
|
||||
extern int myWiFiAPSize;
|
||||
extern WiFi_AP *currentWiFi;
|
||||
extern bool backUpDigiMode;
|
||||
|
||||
bool WiFiConnected = false;
|
||||
uint32_t WiFiAutoAPTime = millis();
|
||||
bool WiFiAutoAPStarted = false;
|
||||
uint32_t previousWiFiMillis = 0;
|
||||
uint8_t wifiCounter = 0;
|
||||
uint32_t lastBackupDigiTime = millis();
|
||||
|
|
@ -45,21 +41,20 @@ namespace WIFI_Utils {
|
|||
if (Config.digi.ecoMode == 0) {
|
||||
if (backUpDigiMode) {
|
||||
uint32_t WiFiCheck = millis() - lastBackupDigiTime;
|
||||
if (WiFi.status() != WL_CONNECTED && WiFiCheck >= 15 * 60 * 1000) {
|
||||
if (!networkManager->isWiFiConnected() && WiFiCheck >= 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 && (WiFi.status() != WL_CONNECTED) && ((millis() - previousWiFiMillis) >= 30 * 1000) && !WiFiAutoAPStarted) {
|
||||
if (!backUpDigiMode && (!networkManager->isWiFiConnected()) && ((millis() - previousWiFiMillis) >= 30 * 1000) && !networkManager->isWifiAPActive()) {
|
||||
Serial.print(millis());
|
||||
Serial.println("Reconnecting to WiFi...");
|
||||
WiFi.disconnect();
|
||||
WIFI_Utils::startWiFi();
|
||||
previousWiFiMillis = millis();
|
||||
|
||||
|
|
@ -76,104 +71,52 @@ namespace WIFI_Utils {
|
|||
}
|
||||
|
||||
void startAutoAP() {
|
||||
WiFi.mode(WIFI_MODE_NULL);
|
||||
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(Config.callsign + "-AP", Config.wifiAutoAP.password);
|
||||
|
||||
WiFiAutoAPTime = millis();
|
||||
WiFiAutoAPStarted = true;
|
||||
displayShow("", " Starting Auto AP", " Please connect to it " , " loading ...", 1000);
|
||||
networkManager->setupAP(Config.callsign + "-AP", Config.wifiAutoAP.password);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
bool hasNetworks = false;
|
||||
|
||||
networkManager->clearWiFiNetworks();
|
||||
for (size_t i = 0; i < Config.wifiAPs.size(); i++) {
|
||||
const WiFi_AP& wifiAP = Config.wifiAPs[i];
|
||||
if (wifiAP.ssid.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
hasNetworks = true;
|
||||
networkManager->addWiFiNetwork(wifiAP.ssid, wifiAP.password);
|
||||
}
|
||||
|
||||
if (!hasNetworks) {
|
||||
Serial.println("WiFi SSID not set! Starting Auto AP");
|
||||
startAutoAP();
|
||||
return;
|
||||
}
|
||||
|
||||
displayShow("", "Connecting to WiFi:", "", " loading ...", 0);
|
||||
networkManager->connectWiFi();
|
||||
|
||||
#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 if (WiFi.status() != WL_CONNECTED) {
|
||||
startAP = true;
|
||||
|
||||
} else {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void checkAutoAPTimeout() {
|
||||
if (WiFiAutoAPStarted && Config.wifiAutoAP.timeout > 0) {
|
||||
if (WiFi.softAPgetStationNum() > 0) {
|
||||
WiFiAutoAPTime = 0;
|
||||
} else {
|
||||
if (WiFiAutoAPTime == 0) {
|
||||
WiFiAutoAPTime = millis();
|
||||
} else if ((millis() - WiFiAutoAPTime) > Config.wifiAutoAP.timeout * 60 * 1000) {
|
||||
Serial.println("Stopping auto AP");
|
||||
|
||||
WiFiAutoAPStarted = false;
|
||||
WiFi.softAPdisconnect(true);
|
||||
|
||||
Serial.println("Auto AP stopped (timeout)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
if (Config.digi.ecoMode == 0) startWiFi();
|
||||
btStop();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue