diff --git a/include/Config.h b/include/Config.h index 913058d..43e7dc5 100644 --- a/include/Config.h +++ b/include/Config.h @@ -18,6 +18,7 @@ struct UserConfig char openItemPrefix[32]; boolean openhabActive; char mqttBrokerIp[16]; + int mqttBrokerPort; char mqttBrokerUser[64]; char mqttBrokerPassword[64]; char mqttBrokerMainTopic[32]; diff --git a/include/index_html.h b/include/index_html.h index ee149f0..3bc861a 100644 --- a/include/index_html.h +++ b/include/index_html.h @@ -78,7 +78,7 @@ const char INDEX_HTML[] PROGMEM = R"=====(

publish all data to a specific mqtt broker and subscribing to the requested powersetting

- IP to mqtt broker: + IP/port to mqtt broker (e.g. 192.168.178.100:1883):
@@ -667,7 +667,7 @@ const char INDEX_HTML[] PROGMEM = R"=====( $('#mqttActive').prop("checked", false); $('#mqttSection').css('color', 'grey'); } - $('#mqttIP').val(mqttData.mqttIp); + $('#mqttIP').val(mqttData.mqttIp+":"+mqttData.mqttPort); $('#mqttUser').val(mqttData.mqttUser); $('#mqttPassword').val(mqttData.mqttPass); $('#mqttMainTopic').val(mqttData.mqttMainTopic); @@ -804,7 +804,14 @@ const char INDEX_HTML[] PROGMEM = R"=====( openhabActiveSend = 0; } - var mqttIpSend = $('#mqttIP').val(); + var mqttIpPortString = $('#mqttIP').val().split(":"); + + + var mqttIpSend = mqttIpPortString[0]; + var mqttPortSend = "1883"; + if(mqttIpPortString[1] != undefined && !isNaN(mqttIpPortString[1])) { + mqttPortSend = mqttIpPortString[1]; + } var mqttUserSend = $('#mqttUser').val(); var mqttPassSend = $('#mqttPassword').val(); var mqttMainTopicSend = $('#mqttMainTopic').val(); @@ -820,6 +827,7 @@ const char INDEX_HTML[] PROGMEM = R"=====( data["openhabActiveSend"] = openhabActiveSend; data["mqttIpSend"] = mqttIpSend; + data["mqttPortSend"] = mqttPortSend; data["mqttUserSend"] = mqttUserSend; data["mqttPassSend"] = mqttPassSend; data["mqttMainTopicSend"] = mqttMainTopicSend; @@ -834,7 +842,7 @@ const char INDEX_HTML[] PROGMEM = R"=====( urlEncodedDataPairs.push( `${encodeURIComponent(name)}=${encodeURIComponent(value)}`, ); - console.log("push: " + name); + console.log("push: " + name + " - value: " + value); } // Combine the pairs into a single string and replace all %-encoded spaces to @@ -851,9 +859,9 @@ const char INDEX_HTML[] PROGMEM = R"=====( strResult = JSON.parse(xmlHttp.responseText); console.log("got from server: " + strResult); - console.log("got from server - strResult.dtuHostIp: " + strResult.openhabHostIp + " - cmp with: " + openhabHostIp); + console.log("got from server - strResult.dtuHostIp: " + strResult.openhabHostIp + " - cmp with: " + openhabHostIpSend); - if (strResult.dtuHostIp == dtuHostIpSend && strResult.dtuSsid == dtuSsidSend && strResult.dtuPassword == dtuPasswordSend) { + if (strResult.openhabHostIp == openhabHostIpSend && strResult.mqttBrokerIp == mqttIpSend && strResult.mqttBrokerUser == mqttUserSend) { console.log("check saved data - OK"); alert("bindings Settings change\n__________________________________\n\nYour settings were successfully changed.\n\nChanges will be applied."); } else { diff --git a/include/version.h b/include/version.h index b22ea27..0b7be1f 100644 --- a/include/version.h +++ b/include/version.h @@ -1,3 +1,3 @@ #define VERSION "1.5.0" -#define BUILDTIME "09.05.2024 - 23:08:36" -#define BUILDTIMESTAMP "1715288916" \ No newline at end of file +#define BUILDTIME "18.05.2024 - 21:34:20" +#define BUILDTIMESTAMP "1716060860" \ No newline at end of file diff --git a/include/version.json b/include/version.json index 8dd7cb2..26958e3 100644 --- a/include/version.json +++ b/include/version.json @@ -1,6 +1,6 @@ { "version": "1.5.0", - "versiondate": "09.05.2024 - 23:08:36", + "versiondate": "18.05.2024 - 21:34:20", "linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_1.5.0.bin", "link": "https://github.com/ohAnd/dtuGateway//releases/latest/download/dtuGateway_release_1.5.0.bin" } \ No newline at end of file diff --git a/src/Config.cpp b/src/Config.cpp index 042acf3..e365629 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -42,6 +42,7 @@ void initializeEEPROM() userConfig.openhabActive = 0; strcpy(userConfig.mqttBrokerIp, "192.168.1.100"); + userConfig.mqttBrokerPort = 1883; strcpy(userConfig.mqttBrokerUser, "dtuuser"); strcpy(userConfig.mqttBrokerPassword, "dtupass"); strcpy(userConfig.mqttBrokerMainTopic, "dtu1"); @@ -93,6 +94,8 @@ void printEEPROMdata() Serial.print(F("mqtt host: \t\t")); Serial.println(userConfig.mqttBrokerIp); + Serial.print(F("mqtt port: \t\t")); + Serial.println(userConfig.mqttBrokerPort); Serial.print(F("mqtt user: \t\t")); Serial.println(userConfig.mqttBrokerUser); Serial.print(F("mqtt pass: \t\t")); diff --git a/src/dtuGateway.ino b/src/dtuGateway.ino index cae90b6..a277098 100644 --- a/src/dtuGateway.ino +++ b/src/dtuGateway.ino @@ -300,6 +300,7 @@ void handleInfojson() JSON = JSON + "\"mqttConnection\": {"; JSON = JSON + "\"mqttActive\": " + userConfig.mqttActive + ","; JSON = JSON + "\"mqttIp\": \"" + String(userConfig.mqttBrokerIp) + "\","; + JSON = JSON + "\"mqttPort\": " + String(userConfig.mqttBrokerPort) + ","; JSON = JSON + "\"mqttUser\": \"" + String(userConfig.mqttBrokerUser) + "\","; JSON = JSON + "\"mqttPass\": \"" + String(userConfig.mqttBrokerPassword) + "\","; JSON = JSON + "\"mqttMainTopic\": \"" + String(userConfig.mqttBrokerMainTopic) + "\""; @@ -404,14 +405,15 @@ void handleUpdateDtuSettings() void handleUpdateBindingsSettings() { String openhabHostIpUser = server.arg("openhabHostIpSend"); // retrieve message from webserver - String openhabPrefix = server.arg("openhabPrefixSend"); // retrieve message from webserver - String openhabActive = server.arg("openhabActiveSend"); // retrieve message from webserver + String openhabPrefix = server.arg("openhabPrefixSend"); + String openhabActive = server.arg("openhabActiveSend"); - String mqttIP = server.arg("mqttIpSend"); // retrieve message from webserver - String mqttUser = server.arg("mqttUserSend"); // retrieve message from webserver - String mqttPass = server.arg("mqttPassSend"); // retrieve message from webserver - String mqttMainTopic = server.arg("mqttMainTopicSend"); // retrieve message from webserver - String mqttActive = server.arg("mqttActiveSend"); // retrieve message from webserver + String mqttIP = server.arg("mqttIpSend"); + String mqttPort = server.arg("mqttPortSend"); + String mqttUser = server.arg("mqttUserSend"); + String mqttPass = server.arg("mqttPassSend"); + String mqttMainTopic = server.arg("mqttMainTopicSend"); + String mqttActive = server.arg("mqttActiveSend"); openhabHostIpUser.toCharArray(userConfig.openhabHostIp, sizeof(userConfig.openhabHostIp)); openhabPrefix.toCharArray(userConfig.openItemPrefix, sizeof(userConfig.openItemPrefix)); @@ -422,6 +424,7 @@ void handleUpdateBindingsSettings() userConfig.openhabActive = false; mqttIP.toCharArray(userConfig.mqttBrokerIp, sizeof(userConfig.mqttBrokerIp)); + userConfig.mqttBrokerPort = mqttPort.toInt(); mqttUser.toCharArray(userConfig.mqttBrokerUser, sizeof(userConfig.mqttBrokerUser)); mqttPass.toCharArray(userConfig.mqttBrokerPassword, sizeof(userConfig.mqttBrokerPassword)); mqttMainTopic.toCharArray(userConfig.mqttBrokerMainTopic, sizeof(userConfig.mqttBrokerMainTopic)); @@ -434,15 +437,19 @@ void handleUpdateBindingsSettings() saveConfigToEEPROM(); delay(500); + // reintialize mqtt with new settings + initMqttClient(); + String JSON = "{"; JSON = JSON + "\"openhabActive\": " + userConfig.openhabActive + ","; JSON = JSON + "\"openhabHostIp\": \"" + userConfig.openhabHostIp + "\","; JSON = JSON + "\"openItemPrefix\": \"" + userConfig.openItemPrefix + "\","; JSON = JSON + "\"mqttActive\": " + userConfig.mqttActive + ","; JSON = JSON + "\"mqttBrokerIp\": \"" + userConfig.mqttBrokerIp + "\","; + JSON = JSON + "\"mqttBrokerPort\": " + String(userConfig.mqttBrokerPort) + ","; JSON = JSON + "\"mqttBrokerUser\": \"" + userConfig.mqttBrokerUser + "\","; JSON = JSON + "\"mqttBrokerPassword\": \"" + userConfig.mqttBrokerPassword + "\","; - JSON = JSON + "\"mqttBrokerMainTopic\": \"" + userConfig.mqttBrokerMainTopic; + JSON = JSON + "\"mqttBrokerMainTopic\": \"" + userConfig.mqttBrokerMainTopic+ "\""; JSON = JSON + "}"; server.send(200, "application/json", JSON); @@ -867,8 +874,8 @@ boolean updateValueToOpenhab() void initMqttClient() { - mqttClient.setServer(userConfig.mqttBrokerIp, 1883); - Serial.print("\ninitialized MQTT client ... to broker: " + String(userConfig.mqttBrokerIp) + "\n"); + mqttClient.setServer(userConfig.mqttBrokerIp, userConfig.mqttBrokerPort); + Serial.print("\ninitialized MQTT client ... to broker: " + String(userConfig.mqttBrokerIp) + ":" + String(userConfig.mqttBrokerPort) + "\n"); } void reconnectMqttClient()