Skip to content

Commit

Permalink
allow configure mqtt broker port via web server
Browse files Browse the repository at this point in the history
  • Loading branch information
technyon committed Mar 27, 2022
1 parent a8ce3fc commit 3eaaab0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
17 changes: 13 additions & 4 deletions Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ void Network::initialize()
const char* brokerAddr = _preferences->getString(preference_mqtt_broker).c_str();
strcpy(_mqttBrokerAddr, brokerAddr);

int port = _preferences->getInt(preference_mqtt_broker_port);
if(port == 0)
{
port = 1883;
_preferences->putInt(preference_mqtt_broker_port, port);
}

Serial.print(F("MQTT Broker: "));
Serial.println(_mqttBrokerAddr);
_mqttClient.setServer(_mqttBrokerAddr, 1883);
Serial.print(_mqttBrokerAddr);
Serial.print(F(":"));
Serial.println(port);
_mqttClient.setServer(_mqttBrokerAddr, port);
_mqttClient.setCallback(Network::onMqttDataReceivedCallback);
}

Expand All @@ -58,14 +67,14 @@ bool Network::reconnect()
Serial.println("Attempting MQTT connection");
// Attempt to connect
if (_mqttClient.connect("nukiHub")) {
Serial.println("MQTT connected");
Serial.println(F("MQTT connected"));

// ... and resubscribe
_mqttClient.subscribe(mqtt_topc_lockstate_action);
}
else
{
Serial.print("MQTT connect failed, rc=");
Serial.print(F("MQTT connect failed, rc="));
Serial.println(_mqttClient.state());
_nextReconnect = millis() + 5000;
}
Expand Down
1 change: 1 addition & 0 deletions PreferencesKeys.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#define preference_mqtt_broker "mqttbroker"
#define preference_mqtt_broker_port "mqttport"
#define preference_query_interval_lockstate "lockStInterval"
#define preference_query_interval_battery "batInterval"

Expand Down
15 changes: 14 additions & 1 deletion WebCfgServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ void WebCfgServer::update()
_preferences->putString(preference_mqtt_broker, token);
configChanged = true;
}
else if(lastTokenType == TokenType::MqttPort && tokenType == TokenType::None)
{
_preferences->putInt(preference_mqtt_broker_port, String(token).toInt());
configChanged = true;
}
else if(lastTokenType == TokenType::QueryIntervalLockstate && tokenType == TokenType::None)
{
_preferences->putInt(preference_query_interval_lockstate, String(token).toInt());
Expand Down Expand Up @@ -109,10 +114,14 @@ void WebCfgServer::serveHtml(WiFiClient &client)

client.println("<FORM ACTION=method=get >");

client.print("MQTT Server: <INPUT TYPE=TEXT VALUE=\"");
client.print("MQTT Broker: <INPUT TYPE=TEXT VALUE=\"");
client.print(_preferences->getString(preference_mqtt_broker));
client.println("\" NAME=\"MQTTSERVER\" SIZE=\"25\" MAXLENGTH=\"40\"><BR>");

client.print("MQTT Broker port: <INPUT TYPE=TEXT VALUE=\"");
client.print(_preferences->getInt(preference_mqtt_broker_port));
client.println("\" NAME=\"MQTTPORT\" SIZE=\"25\" MAXLENGTH=\"40\"><BR>");

client.print("Query interval lock state (seconds): <INPUT TYPE=TEXT VALUE=\"");
client.print(_preferences->getInt(preference_query_interval_lockstate));
client.println("\" NAME=\"LSTINT\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");
Expand Down Expand Up @@ -145,6 +154,10 @@ TokenType WebCfgServer::getParameterType(char *&token)
{
return TokenType::QueryIntervalBattery;
}
if (strcmp(token, "MQTTPORT") == 0)
{
return TokenType::MqttPort;
}

return TokenType::None;
}
1 change: 1 addition & 0 deletions WebCfgServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum class TokenType
{
None,
MqttServer,
MqttPort,
QueryIntervalLockstate,
QueryIntervalBattery,
};
Expand Down

0 comments on commit 3eaaab0

Please sign in to comment.