2026-01-12 19:41:47 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <SPI.h>
|
|
|
|
|
#include <WiFi.h>
|
2026-01-15 00:23:41 +01:00
|
|
|
#include <ETH.h>
|
2026-01-15 01:41:42 +01:00
|
|
|
#include <vector>
|
2026-01-12 19:41:47 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class for managing network connections
|
|
|
|
|
*/
|
|
|
|
|
class NetworkManager
|
|
|
|
|
{
|
|
|
|
|
private:
|
2026-01-15 01:41:42 +01:00
|
|
|
class WiFiNetwork {
|
|
|
|
|
public:
|
|
|
|
|
String ssid;
|
|
|
|
|
String psk;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-12 19:41:47 +01:00
|
|
|
bool _wifiAPmode = false;
|
|
|
|
|
bool _wifiSTAmode = false;
|
2026-01-15 00:23:41 +01:00
|
|
|
bool _ethernetMode = false;
|
|
|
|
|
bool _ethernetConnected = false;
|
2026-01-12 19:41:47 +01:00
|
|
|
unsigned long _apStartup = 0;
|
|
|
|
|
unsigned long _apTimeout = 0;
|
|
|
|
|
|
2026-01-15 00:03:19 +01:00
|
|
|
String _hostName = "";
|
2026-01-15 01:41:42 +01:00
|
|
|
std::vector<WiFiNetwork> _wifiNetworks;
|
2026-01-15 00:03:19 +01:00
|
|
|
|
2026-01-15 01:41:42 +01:00
|
|
|
int _findWiFiNetworkIndex(const String& ssid) const;
|
|
|
|
|
bool _connectWiFi(const WiFiNetwork& network);
|
2026-01-12 19:41:47 +01:00
|
|
|
void _processAPTimeout();
|
2026-01-15 00:23:41 +01:00
|
|
|
void _onNetworkEvent(arduino_event_id_t event, arduino_event_info_t /*info*/);
|
2026-01-12 19:41:47 +01:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// Constructor
|
|
|
|
|
NetworkManager();
|
|
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
|
~NetworkManager();
|
|
|
|
|
|
|
|
|
|
// Initialize network module
|
|
|
|
|
bool setup();
|
|
|
|
|
void loop();
|
|
|
|
|
|
2026-01-15 00:03:19 +01:00
|
|
|
void setHostName(const String& hostName);
|
|
|
|
|
|
2026-01-12 19:41:47 +01:00
|
|
|
// WiFi methods
|
|
|
|
|
bool setupAP(String apName, String apPsk = "");
|
|
|
|
|
bool disableAP();
|
|
|
|
|
void setAPTimeout(unsigned long timeout);
|
2026-01-15 01:41:42 +01:00
|
|
|
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 = "");
|
2026-01-12 19:41:47 +01:00
|
|
|
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;
|
|
|
|
|
|
2026-01-15 00:23:41 +01:00
|
|
|
// 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;
|
|
|
|
|
|
2026-01-12 19:41:47 +01:00
|
|
|
// 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;
|
|
|
|
|
};
|