Skip to content

Commit

Permalink
Add ability to change MQTT topic and or gateway name by MQTT command.
Browse files Browse the repository at this point in the history
This adds new commands to allow changing the MQTT topic and or gateway name from a command.
The new topic/name will be saved in flash memory and used on subsequent reboots.
Either/both parameters can be sent or combined with other commands.

Example use:
```
mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m
'{
  "mqtt_topic": "topic",
  "gateway_name: "name"
}'
```
  • Loading branch information
h2zero committed Aug 5, 2021
1 parent 702c4b7 commit 5383b3a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/use/gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ Server, port, and secure_flag are only required if changing connection to anothe
If the new connection fails the gateway will fallback to the previous connection.
:::

## Change the MQTT main topic and or gateway name
```
mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m
'{
"mqtt_topic": "topic",
"gateway_name: "name"
}'
```
::: info
This will change the subscribed and published topic/gateway_name that the gateway uses. No parameters are manditory, the current topic or gateway name will be used if not supplied.
:::

## Switching brokers and using self signed and client certificates

In the `user_config.h` file it is possible to specify multiple MQTT brokers and client certificates. These are commonly self signed and are supported by defining `MQTT_SECURE_SELF_SIGNED` as true or 1.
Expand Down
12 changes: 12 additions & 0 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,18 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
# endif
}
# endif
if (SYSdata.containsKey("mqtt_topic") || SYSdata.containsKey("gateway_name")) {
if (SYSdata.containsKey("mqtt_topic")) {
strncpy(mqtt_topic, SYSdata["mqtt_topic"], mqtt_topic_max_size);
}
if (SYSdata.containsKey("gateway_name")) {
strncpy(gateway_name, SYSdata["gateway_name"], parameters_size * 2);
}
# ifndef ESPWifiManualSetup
saveMqttConfig();
# endif
client.disconnect(); // reconnects in loop using the new topic/name
}
#endif

#ifdef ZmqttDiscovery
Expand Down

0 comments on commit 5383b3a

Please sign in to comment.