diff --git a/lib/network/network.cpp b/lib/network/network.cpp index 92f6b27..cd5932d 100644 --- a/lib/network/network.cpp +++ b/lib/network/network.cpp @@ -5,7 +5,9 @@ WIFIMANAGER Network::WifiManager; 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); WifiManager.startBackgroundTask(apSsid.data(), ""); WifiManager.fallbackToSoftAp(true); @@ -23,6 +25,11 @@ Network::~Network() { WifiManager.detachWebServer(); } +void Network::reconnect() { + WifiManager.stopWifi(true); + WifiManager.startBackgroundTask(apSsid.data(), ""); +} + bool Network::isConnected() const { return WiFi.status() == WL_CONNECTED; } @@ -32,13 +39,14 @@ std::string Network::getHostname() const { } 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() { - if (!MDNS.begin(hostname.data())) { + if (!MDNS.begin(this->hostname.c_str())) { Serial.println("Error setting up MDNS responder!"); } 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()); } } \ No newline at end of file diff --git a/lib/network/network.h b/lib/network/network.h index 79dc231..997fb4d 100644 --- a/lib/network/network.h +++ b/lib/network/network.h @@ -6,6 +6,7 @@ class Network { public: Network(std::string_view hostname, std::string_view apSsid = "Smart RGB"); ~Network(); + void reconnect(); bool isConnected() const; std::string getHostname() const; bool setHostname(const std::string_view &hostname); @@ -27,6 +28,6 @@ private:

ESP32 WiFi Manager (c) 2022-2025 by Martin Verges

)html"; - std::string_view hostname; - std::string_view apSsid; // SSID for the fallback AP + std::string hostname; + std::string apSsid; // SSID for the fallback AP }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 136e147..82c4ae6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,8 +44,8 @@ void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println("Starting Smart RGB ESP32..."); - initializeScheduler(); stateMachine = new StateMachine(); + initializeScheduler(); appContext->pinR = pinR; appContext->pinG = pinG; appContext->pinB = pinB; diff --git a/src/states.hpp b/src/states.hpp index 8ed4c2a..0230a04 100644 --- a/src/states.hpp +++ b/src/states.hpp @@ -52,12 +52,10 @@ public: void onEnter(StateMachineBase &stateMachine) override { Serial.println("Entering NetworkInitializeState"); - if (appContext && appContext->network) { - delete appContext->network; - appContext->network = nullptr; - } if (appContext && !appContext->network) { appContext->network = new Network(hostName, friendlyName); + } else if (appContext && appContext->network) { + appContext->network->reconnect(); } }