Improve reconnect

This commit is contained in:
2025-08-28 20:29:17 +02:00
parent 936d1dba20
commit eeb317c108
4 changed files with 18 additions and 11 deletions

View File

@@ -5,7 +5,9 @@
WIFIMANAGER Network::WifiManager; WIFIMANAGER Network::WifiManager;
AsyncWebServer Network::webServer(80); AsyncWebServer Network::webServer(80);
Network::Network(std::string_view hostname, std::string_view apSsid) : hostname(hostname), apSsid(apSsid) { Network::Network(std::string_view hostname, std::string_view apSsid) {
this->hostname = std::string(hostname);
this->apSsid = std::string(apSsid);
setHostname(hostname); setHostname(hostname);
WifiManager.startBackgroundTask(apSsid.data(), ""); WifiManager.startBackgroundTask(apSsid.data(), "");
WifiManager.fallbackToSoftAp(true); WifiManager.fallbackToSoftAp(true);
@@ -23,6 +25,11 @@ Network::~Network() {
WifiManager.detachWebServer(); WifiManager.detachWebServer();
} }
void Network::reconnect() {
WifiManager.stopWifi(true);
WifiManager.startBackgroundTask(apSsid.data(), "");
}
bool Network::isConnected() const { bool Network::isConnected() const {
return WiFi.status() == WL_CONNECTED; return WiFi.status() == WL_CONNECTED;
} }
@@ -32,13 +39,14 @@ std::string Network::getHostname() const {
} }
bool Network::setHostname(const std::string_view &hostname) { bool Network::setHostname(const std::string_view &hostname) {
return WiFi.setHostname(hostname.data()); this->hostname = std::string(hostname);
return WiFi.setHostname(this->hostname.c_str());
} }
void Network::registerMDNS() { void Network::registerMDNS() {
if (!MDNS.begin(hostname.data())) { if (!MDNS.begin(this->hostname.c_str())) {
Serial.println("Error setting up MDNS responder!"); Serial.println("Error setting up MDNS responder!");
} else { } else {
Serial.printf("mDNS responder started with hostname: %s\n", hostname.data()); Serial.printf("mDNS responder started with hostname: %s\n", this->hostname.c_str());
} }
} }

View File

@@ -6,6 +6,7 @@ class Network {
public: public:
Network(std::string_view hostname, std::string_view apSsid = "Smart RGB"); Network(std::string_view hostname, std::string_view apSsid = "Smart RGB");
~Network(); ~Network();
void reconnect();
bool isConnected() const; bool isConnected() const;
std::string getHostname() const; std::string getHostname() const;
bool setHostname(const std::string_view &hostname); bool setHostname(const std::string_view &hostname);
@@ -27,6 +28,6 @@ private:
<p><small>ESP32 WiFi Manager (c) 2022-2025 by Martin Verges</small></p> <p><small>ESP32 WiFi Manager (c) 2022-2025 by Martin Verges</small></p>
</body></html> </body></html>
)html"; )html";
std::string_view hostname; std::string hostname;
std::string_view apSsid; // SSID for the fallback AP std::string apSsid; // SSID for the fallback AP
}; };

View File

@@ -44,8 +44,8 @@ void setup() {
// put your setup code here, to run once: // put your setup code here, to run once:
Serial.begin(115200); Serial.begin(115200);
Serial.println("Starting Smart RGB ESP32..."); Serial.println("Starting Smart RGB ESP32...");
initializeScheduler();
stateMachine = new StateMachine<maxNumberOfStates>(); stateMachine = new StateMachine<maxNumberOfStates>();
initializeScheduler();
appContext->pinR = pinR; appContext->pinR = pinR;
appContext->pinG = pinG; appContext->pinG = pinG;
appContext->pinB = pinB; appContext->pinB = pinB;

View File

@@ -52,12 +52,10 @@ public:
void onEnter(StateMachineBase &stateMachine) override { void onEnter(StateMachineBase &stateMachine) override {
Serial.println("Entering NetworkInitializeState"); Serial.println("Entering NetworkInitializeState");
if (appContext && appContext->network) {
delete appContext->network;
appContext->network = nullptr;
}
if (appContext && !appContext->network) { if (appContext && !appContext->network) {
appContext->network = new Network(hostName, friendlyName); appContext->network = new Network(hostName, friendlyName);
} else if (appContext && appContext->network) {
appContext->network->reconnect();
} }
} }