Skip to content

Commit fc73f03

Browse files
committed
Add mqtt_secure parameter to MQTT credentials command.
1 parent 07fe523 commit fc73f03

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

docs/use/gateway.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ If the new connection fails the gateway will fallback to the previous connection
4242
:::
4343

4444
## Change the MQTT broker credentials
45-
46-
`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m '{"mqtt_user":"user_name", "mqtt_password":"password", "mqtt_server":"host", "mqtt_port":"port"}'`
47-
45+
```
46+
mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m
47+
'{
48+
"mqtt_user": "user_name",
49+
"mqtt_password": "password",
50+
"mqtt_server": "host",
51+
"mqtt_port": "port",
52+
"mqtt_secure": "false"
53+
}'
54+
```
4855
::: tip
49-
Server and port are only required if changing connection to another broker, **only unsecured broker connections are supported**.
56+
Server, port, and secure_flag are only required if changing connection to another broker.
5057
If the new connection fails the gateway will fallback to the previous connection.
5158
:::
5259

main/main.ino

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,13 +1848,20 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
18481848
bool update_server = false;
18491849
void* prev_client = nullptr;
18501850

1851-
if (SYSdata.containsKey("mqtt_server") && SYSdata.containsKey("mqtt_port")) {
1851+
if (SYSdata.containsKey("mqtt_server") &&
1852+
SYSdata.containsKey("mqtt_port") &&
1853+
SYSdata.containsKey("mqtt_secure")) {
18521854
update_server = true;
18531855

1854-
if (mqtt_secure) {
1855-
Log.warning(F("Switching to unsecure MQTT broker" CR));
1856-
prev_client = eClient;
1857-
eClient = new WiFiClient;
1856+
if (SYSdata.get<bool>("mqtt_secure") != mqtt_secure) {
1857+
if (!mqtt_secure) {
1858+
prev_client = eClient;
1859+
eClient = new WiFiClientSecure;
1860+
} else {
1861+
Log.warning(F("Switching to unsecure MQTT broker" CR));
1862+
prev_client = eClient;
1863+
eClient = new WiFiClient;
1864+
}
18581865
}
18591866

18601867
client.setServer(SYSdata.get<const char*>("mqtt_server"), SYSdata.get<unsigned int>("mqtt_port"));
@@ -1871,8 +1878,8 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
18711878
if (update_server) {
18721879
strcpy(mqtt_server, SYSdata["mqtt_server"]);
18731880
strcpy(mqtt_port, SYSdata["mqtt_port"]);
1874-
if (mqtt_secure) {
1875-
mqtt_secure = false;
1881+
if (prev_client != nullptr) {
1882+
mqtt_secure = !mqtt_secure;
18761883
delete prev_client;
18771884
}
18781885
}

0 commit comments

Comments
 (0)