diff --git a/CMakeLists.txt b/CMakeLists.txt index ddf7073..f07347e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ file(GLOB SRCFILES Nuki.cpp MqttTopics.h WebCfgServer.cpp + PreferencesKeys.h lib/ESP32_BLE_Arduino-1.0.1/src/*.cpp lib/ESP32_BLE_Arduino-1.0.1/src/*.h lib/WiFiManager/WiFiManager.cpp diff --git a/Network.cpp b/Network.cpp index d421323..b1dd720 100644 --- a/Network.cpp +++ b/Network.cpp @@ -3,11 +3,13 @@ #include // https://github.com/tzapu/WiFiManager #include "Arduino.h" #include "MqttTopics.h" +#include "PreferencesKeys.h" Network* nwInst; -Network::Network() -: _mqttClient(_wifiClient) +Network::Network(Preferences* preferences) +: _mqttClient(_wifiClient), + _preferences(preferences) { nwInst = this; } @@ -39,15 +41,20 @@ void Network::initialize() Serial.println("connected...yeey :)"); } - _mqttClient.setServer("192.168.0.100", 1883); + const char* brokerAddr = _preferences->getString(preference_mqtt_broker).c_str(); + strcpy(_mqttBrokerAddr, brokerAddr); + + Serial.print("MQTT Broker: "); + Serial.println(_mqttBrokerAddr); + _mqttClient.setServer(_mqttBrokerAddr, 1883); _mqttClient.setCallback(Network::onMqttDataReceivedCallback); - _mqttClient.publish("nuki/test", "OK"); } bool Network::reconnect() { - while (!_mqttClient.connected()) { + while (!_mqttClient.connected() && millis() > _nextReconnect) + { Serial.println("Attempting MQTT connection"); // Attempt to connect if (_mqttClient.connect("nukiHub")) { @@ -55,10 +62,12 @@ bool Network::reconnect() // ... and resubscribe _mqttClient.subscribe(mqtt_topc_lockstate_action); - } else { + } + else + { Serial.print("MQTT connect failed, rc="); Serial.println(_mqttClient.state()); - vTaskDelay( 5000 / portTICK_PERIOD_MS); + _nextReconnect = millis() + 5000; } } } diff --git a/Network.h b/Network.h index fdbb0a8..24dae1c 100644 --- a/Network.h +++ b/Network.h @@ -2,12 +2,14 @@ #include #include +#include #include "NukiConstants.h" class Network { public: - Network(); + explicit Network(Preferences* preferences); + virtual ~Network() = default; void initialize(); void update(); @@ -25,6 +27,10 @@ private: PubSubClient _mqttClient; WiFiClient _wifiClient; + Preferences* _preferences; + + unsigned long _nextReconnect = 0; + char _mqttBrokerAddr[100] = {0}; void (*_lockActionReceivedCallback)(const char* value) = NULL; }; diff --git a/PreferencesKeys.h b/PreferencesKeys.h new file mode 100644 index 0000000..ca4685a --- /dev/null +++ b/PreferencesKeys.h @@ -0,0 +1,3 @@ +#pragma once + +#define preference_mqtt_broker "mqttbroker" \ No newline at end of file diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index c343340..9e3e338 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -1,8 +1,10 @@ #include "WebCfgServer.h" #include +#include "PreferencesKeys.h" -WebCfgServer::WebCfgServer() -: _wifiServer(80) +WebCfgServer::WebCfgServer(Preferences* preferences) +: _wifiServer(80), + _preferences(preferences) {} @@ -13,6 +15,8 @@ void WebCfgServer::initialize() void WebCfgServer::update() { + bool configChanged = false; + // Create a client connections WiFiClient client = _wifiServer.available(); @@ -60,22 +64,26 @@ void WebCfgServer::update() if(lastTokenType == TokenType::MQTT_SERVER && tokenType == TokenType::NONE) { configChanged = true; - Serial.print("### "); - Serial.println(token); -// strcpy(_configuration->mqttServerAddress, token); + _preferences->putString(preference_mqtt_broker, token); + configChanged = true; } } lastToken = token; token = strtok(NULL, "?=&"); } -// -// if(configChanged) -// { -// _configuration->writeEeprom(); -// _enabled = false; -// } + + if(configChanged) + { + _enabled = false; + _preferences->end(); + Serial.println(F("Restarting")); + vTaskDelay( 200 / portTICK_PERIOD_MS); + ESP.restart(); + } } + vTaskDelay(200 / portTICK_PERIOD_MS); + } void WebCfgServer::serveHtml(WiFiClient &client) @@ -93,7 +101,7 @@ void WebCfgServer::serveHtml(WiFiClient &client) client.println("
"); client.print("MQTT Server: getString(preference_mqtt_broker)); client.println("\" NAME=\"MQTTSERVER\" SIZE=\"25\" MAXLENGTH=\"40\">
"); // client.print("DNS Server: +#include enum class TokenType { @@ -11,7 +12,7 @@ enum class TokenType class WebCfgServer { public: - WebCfgServer(); + WebCfgServer(Preferences* preferences); ~WebCfgServer() = default; void initialize(); @@ -23,6 +24,7 @@ private: TokenType getParameterType(char*& token); WiFiServer _wifiServer; + Preferences* _preferences; bool _enabled = true; }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index b0808cc..9b18718 100644 --- a/main.cpp +++ b/main.cpp @@ -38,8 +38,8 @@ void setup() { preferences = new Preferences(); preferences->begin("nukihub", false); - network = new Network(); - webCfgServer = new WebCfgServer(); + network = new Network(preferences); + webCfgServer = new WebCfgServer(preferences); nuki = new Nuki("Main Door", 2020001, network); network->initialize();