Skip to content

Commit

Permalink
allow MQTT port config entry to be optional (backward compatiblity)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsbyr committed Aug 11, 2023
1 parent 98980c6 commit ff098a5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
6 changes: 3 additions & 3 deletions MQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ void MQTTClient::subscriptionUpdate(char* topic, byte* message, unsigned int len
publications.erase(willTopic);
}

void MQTTClient::setup(const char* mqttServer, const char* mqttPort, const char* mqttUsername, const char* mqttPassword, const char* cid, const char* wt, const char* wm)
void MQTTClient::setup(const char* mqttServer, uint16 mqttPort, const char* mqttUsername, const char* mqttPassword, const char* cid, const char* wt, const char* wm)
{
mqttuser = mqttUsername;
mqttpw = mqttPassword;
clientId = cid;
willTopic = wt;
willMessage = wm;

mqttClient.setServer(mqttServer, atoi(mqttPort));
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(std::bind(&MQTTClient::subscriptionUpdate, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
}

Expand Down Expand Up @@ -186,6 +186,6 @@ bool MQTTClient::publish(const char* topic, const String& message, bool retain,
return published;
}
}

return false;
}
4 changes: 2 additions & 2 deletions MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class MQTTClient
void addSubscriber(const char* topic, void (*setter)(bool value));
void addSubscriber(const char* topic, void (*setter)(int value));

void setup(const char* mqttServer, const char* mqttPort, const char* mqttUsername, const char* mqttPassword,const char* clientId, const char* willTopic, const char* willMessage);
void setup(const char* mqttServer, uint16 mqttPort, const char* mqttUsername, const char* mqttPassword,const char* clientId, const char* willTopic, const char* willMessage);
void loop();

bool isConnected();
bool publish(const char* topic, const String& payload, bool retain=false, bool force=false);

private:
static const unsigned int RECONNECT_DELAY = 3000; // [ms]

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,17 @@ Example:
"wifiSSID": "WiFi-SSID",
"wifiPassphrase": "WiFi-secret",
"mqttServer": "mqtt.at.home",
"mqttUser": "leave blank if you don't need authentication",
"mqttPassword": "leave blank if you don't need authentication",
"mqttPort": "1883",
"mqttUser": "userName",
"mqttPassword": "password",
"mqttRetain": "no",
"firmwareURL": "http://webserver.at.home/firmware/SB-H20-WiFiController.bin",
"errorLanguage": "EN"
}
```

The config entry *mqttPort* can be omitted and defaults to 1883.

If *mqttUser* is omitted no authentication is used for the MQTT connection and
*mqttPassword* can also be omitted.

Expand Down
2 changes: 1 addition & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

namespace CONFIG
{
const char WIFI_VERSION[] = "1.0.6.2"; // 30.07.2023
const char WIFI_VERSION[] = "1.0.7.0"; // 11.08.2023

// WiFi parameters
const unsigned long WIFI_MAX_DISCONNECT_DURATION = 900000; // [ms] 5 min until reboot
Expand Down
12 changes: 11 additions & 1 deletion esp8266-intexsbh20.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ MQTTClient mqttClient;
MQTTPublisher mqttPublisher(mqttClient, pureSpaIO, thermometer);
String username;
String password;
String mqttPort;

unsigned long disconnectTime = 0;
LANG language = LANG::CODE;
Expand Down Expand Up @@ -132,7 +133,16 @@ void setup()
username = config.get(CONFIG_TAG::MQTT_USER);
password = config.get(CONFIG_TAG::MQTT_PASSWORD);
}
mqttClient.setup(config.get(CONFIG_TAG::MQTT_SERVER), config.get(CONFIG_TAG::MQTT_PORT), username.c_str(), password.c_str(), pureSpaIO.getModelName(), MQTT_TOPIC::STATE, "offline");
uint16 mqttPort;
if (config.exists(CONFIG_TAG::MQTT_PORT))
{
mqttPort = atoi(config.get(CONFIG_TAG::MQTT_PORT));
}
else
{
mqttPort = 1883;
}
mqttClient.setup(config.get(CONFIG_TAG::MQTT_SERVER), mqttPort, username.c_str(), password.c_str(), pureSpaIO.getModelName(), MQTT_TOPIC::STATE, "offline");

// init NTC thermometer
thermometer.setup(22000, 3.33f, 320.f/100.f); // measured: 21990, 3.327f, 319.f/99.6f
Expand Down

0 comments on commit ff098a5

Please sign in to comment.