mirror of
https://github.com/richonguzman/LoRa_APRS_iGate.git
synced 2026-03-28 16:52:33 +01:00
Network manager supports basic Ethernet
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <ETH.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,6 +18,8 @@ private:
|
|||||||
|
|
||||||
bool _wifiAPmode = false;
|
bool _wifiAPmode = false;
|
||||||
bool _wifiSTAmode = false;
|
bool _wifiSTAmode = false;
|
||||||
|
bool _ethernetMode = false;
|
||||||
|
bool _ethernetConnected = false;
|
||||||
unsigned long _apStartup = 0;
|
unsigned long _apStartup = 0;
|
||||||
unsigned long _apTimeout = 0;
|
unsigned long _apTimeout = 0;
|
||||||
|
|
||||||
@@ -26,6 +29,7 @@ private:
|
|||||||
int _findWiFiNetworkIndex(const String& ssid) const;
|
int _findWiFiNetworkIndex(const String& ssid) const;
|
||||||
bool _connectWiFi(const WiFiNetwork& network);
|
bool _connectWiFi(const WiFiNetwork& network);
|
||||||
void _processAPTimeout();
|
void _processAPTimeout();
|
||||||
|
void _onNetworkEvent(arduino_event_id_t event, arduino_event_info_t /*info*/);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
@@ -59,6 +63,13 @@ public:
|
|||||||
uint8_t* getWiFimacAddress(uint8_t* mac);
|
uint8_t* getWiFimacAddress(uint8_t* mac);
|
||||||
String getWiFimacAddress(void) const;
|
String getWiFimacAddress(void) const;
|
||||||
|
|
||||||
|
// Ethernet methods
|
||||||
|
bool ethernetConnect(eth_phy_type_t type, uint8_t phy_addr, uint8_t mdc, uint8_t mdio, int power, eth_clock_mode_t clock_mode, bool use_mac_from_efuse = false);
|
||||||
|
bool setEthernetIP(const String& staticIP, const String& gateway, const String& subnet, const String& dns1, const String& dns2);
|
||||||
|
bool ethernetDisconnect();
|
||||||
|
IPAddress getEthernetIP() const;
|
||||||
|
String getEthernetMACAddress() const;
|
||||||
|
|
||||||
// Check if any network is available
|
// Check if any network is available
|
||||||
bool isConnected() const;
|
bool isConnected() const;
|
||||||
|
|
||||||
|
|||||||
@@ -79,9 +79,44 @@ void NetworkManager::_processAPTimeout() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NetworkManager::_onNetworkEvent(arduino_event_id_t event, arduino_event_info_t /*info*/) {
|
||||||
|
switch (event) {
|
||||||
|
case ARDUINO_EVENT_ETH_START:
|
||||||
|
Serial.println("ETH Started");
|
||||||
|
if (!_hostName.isEmpty()) {
|
||||||
|
Serial.println("ETH Setting Hostname: " + _hostName);
|
||||||
|
ETH.setHostname(_hostName.c_str());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_ETH_CONNECTED:
|
||||||
|
Serial.println("ETH Connected");
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_ETH_GOT_IP:
|
||||||
|
Serial.println("ETH Got IP");
|
||||||
|
_ethernetConnected = true;
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
||||||
|
Serial.println("ETH Disconnected");
|
||||||
|
_ethernetConnected = false;
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_ETH_STOP:
|
||||||
|
Serial.println("ETH Stopped");
|
||||||
|
_ethernetConnected = false;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize
|
// Initialize
|
||||||
bool NetworkManager::setup() {
|
bool NetworkManager::setup() {
|
||||||
Serial.println("Initializing Networking...");
|
Serial.println("Initializing Networking...");
|
||||||
|
|
||||||
|
WiFi.onEvent(
|
||||||
|
[this](arduino_event_id_t event, arduino_event_info_t info) {
|
||||||
|
_onNetworkEvent(event, info);
|
||||||
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,6 +262,55 @@ String NetworkManager::getWiFimacAddress(void) const {
|
|||||||
return WiFi.macAddress();
|
return WiFi.macAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ethernet methods
|
||||||
|
bool NetworkManager::ethernetConnect(eth_phy_type_t type, uint8_t phy_addr, uint8_t mdc, uint8_t mdio, int power, eth_clock_mode_t clock_mode, bool use_mac_from_efuse) {
|
||||||
|
_ethernetMode = true;
|
||||||
|
Serial.println("Setting up Ethernet...");
|
||||||
|
|
||||||
|
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||||
|
// SDK 5.x (Arduino SDK 3.x)
|
||||||
|
#pragma message("Compiling ETH init: SDK 5.x (Arduino core 3.x)")
|
||||||
|
return ETH.begin(type, phy_addr, mdc, mdio, power, clock_mode, use_mac_from_efuse);
|
||||||
|
#else
|
||||||
|
// SDK 4.x (Arduino SDK 2.x)
|
||||||
|
#pragma message("Compiling ETH init: SDK 4.x (Arduino core 2.x)")
|
||||||
|
return ETH.begin(phy_addr, power, mdc, mdio, type, clock_mode, use_mac_from_efuse);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NetworkManager::setEthernetIP(const String& staticIP, const String& gateway, const String& subnet, const String& dns1, const String& dns2) {
|
||||||
|
if (staticIP.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress ip, gw, sn, d1, d2;
|
||||||
|
if (!ip.fromString(staticIP) || !gw.fromString(gateway) || !sn.fromString(subnet)) {
|
||||||
|
Serial.println("Invalid static IP configuration");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dns1.isEmpty() && d1.fromString(dns1)) {
|
||||||
|
if (!dns2.isEmpty() && d2.fromString(dns2)) {
|
||||||
|
ETH.config(ip, gw, sn, d1, d2);
|
||||||
|
} else {
|
||||||
|
ETH.config(ip, gw, sn, d1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ETH.config(ip, gw, sn);
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("Ethernet static IP: " + staticIP);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress NetworkManager::getEthernetIP() const {
|
||||||
|
return ETH.localIP();
|
||||||
|
}
|
||||||
|
|
||||||
|
String NetworkManager::getEthernetMACAddress() const {
|
||||||
|
return ETH.macAddress();
|
||||||
|
}
|
||||||
|
|
||||||
// Check if network is available
|
// Check if network is available
|
||||||
bool NetworkManager::isConnected() const {
|
bool NetworkManager::isConnected() const {
|
||||||
return isWiFiConnected() || isEthernetConnected() || isModemConnected();
|
return isWiFiConnected() || isEthernetConnected() || isModemConnected();
|
||||||
@@ -243,8 +327,7 @@ bool NetworkManager::isWifiAPActive() const {
|
|||||||
|
|
||||||
// Check if Ethernet is connected
|
// Check if Ethernet is connected
|
||||||
bool NetworkManager::isEthernetConnected() const {
|
bool NetworkManager::isEthernetConnected() const {
|
||||||
// Implement Ethernet connection check logic here
|
return _ethernetMode && _ethernetConnected && ETH.linkUp();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if Modem is connected
|
// Check if Modem is connected
|
||||||
|
|||||||
Reference in New Issue
Block a user