Skip to content

Commit

Permalink
Setmqtt setting optional (#1021)
Browse files Browse the repository at this point in the history
* Define MQTTsetMQTT for configuring MQTT over MQTT

* Enable MQTTsetMQTT per default on all ESP except RFBridge

Co-authored-by: Dave <[email protected]>
  • Loading branch information
1technophile and dkneisz authored Jul 14, 2021
1 parent c44ab5a commit ebe17c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
11 changes: 11 additions & 0 deletions docs/use/gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m
"mqtt_secure": "false"
}'
```
::: info
By default this function is not available on the pre built binary of RFBridge, in order to have less code size and enable to have OTA update working properly. So as to enable it remove from the rf bridge env:
```
build_flags = '-UMQTTsetMQTT'
```
Arduino boards does not have this function per default also, to add it:
```
build_flags = '-DMQTTsetMQTT'
```
:::

::: tip
Server, port, and secure_flag are only required if changing connection to another broker.
If the new connection fails the gateway will fallback to the previous connection.
Expand Down
22 changes: 12 additions & 10 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,7 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
# endif
}

# ifdef MQTTsetMQTT
if (SYSdata.containsKey("mqtt_user") && SYSdata.containsKey("mqtt_pass")) {
bool update_server = false;
bool secure_connect = SYSdata.get<bool>("mqtt_secure");
Expand All @@ -1909,19 +1910,19 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
Log.error(F("mqtt_server provided without mqtt_secure defined - ignoring command" CR));
return;
}
# if MQTT_SECURE_SELF_SIGNED
# if MQTT_SECURE_SELF_SIGNED
if (use_ss_cert) {
cert_index = SYSdata.get<uint8_t>("mqtt_cert_index");
if (cert_index >= sizeof(certs_array) / sizeof(ss_certs)) {
Log.error(F("mqtt_cert_index invalid - ignoring command" CR));
return;
}
}
# endif
# endif

# if defined(ZgatewayBT) && defined(ESP32)
# if defined(ZgatewayBT) && defined(ESP32)
stopProcessing();
# endif
# endif
client.disconnect();
update_server = true;
if (secure_connect != mqtt_secure) {
Expand All @@ -1942,9 +1943,9 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding

client.setServer(SYSdata.get<const char*>("mqtt_server"), SYSdata.get<unsigned int>("mqtt_port"));
} else {
# if defined(ZgatewayBT) && defined(ESP32)
# if defined(ZgatewayBT) && defined(ESP32)
stopProcessing();
# endif
# endif
client.disconnect();
}

Expand All @@ -1965,9 +1966,9 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
delete prev_client;
}
}
# ifndef ESPWifiManualSetup
# ifndef ESPWifiManualSetup
saveMqttConfig();
# endif
# endif
} else {
if (update_server) {
if (prev_client != nullptr) {
Expand All @@ -1985,10 +1986,11 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
}
connectMQTT();
}
# if defined(ZgatewayBT) && defined(ESP32)
# if defined(ZgatewayBT) && defined(ESP32)
startProcessing();
# endif
# endif
}
# endif
#endif

#ifdef ZmqttDiscovery
Expand Down
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ build_flags =
'-DTRACE=1'
'-DCONFIG_BT_NIMBLE_ROLE_PERIPHERAL_DISABLED'
'-DCONFIG_BT_NIMBLE_ROLE_BROADCASTER_DISABLED'
'-DMQTTsetMQTT'
;'-DCORE_DEBUG_LEVEL=4'

[com-arduino]
Expand Down Expand Up @@ -190,6 +191,7 @@ build_flags =
'-DLED_INFO=13'
'-DLED_INFO_ON=0'
'-DGateway_Name="OpenMQTTGateway_SRFB"'
'-UMQTTsetMQTT' ;We remove this function to have sufficient FLASH available for OTA, you should also use ESPWifiManualSetup to save flash memory and have OTA working
board_build.flash_mode = dout

[env:esp32dev-all-test]
Expand Down

0 comments on commit ebe17c8

Please sign in to comment.