@@ -272,7 +272,7 @@ bool to_bool(String const& s) { // thanks Chris Jester-Young from stackoverflow
272272}
273273
274274void pub (const char * topicori, const char * payload, bool retainFlag) {
275- String topic = String (mqtt_topic) + String (topicori);
275+ String topic = String (mqtt_topic) + String (gateway_name) + String ( topicori);
276276 pubMQTT (topic.c_str (), payload, retainFlag);
277277}
278278
@@ -281,7 +281,7 @@ void pub(const char* topicori, JsonObject& data) {
281281 digitalWrite (LED_RECEIVE, LED_RECEIVE_ON);
282282 logJson (data);
283283 if (client.connected ()) {
284- String topic = String (mqtt_topic) + String (topicori);
284+ String topic = String (mqtt_topic) + String (gateway_name) + String ( topicori);
285285#ifdef valueAsASubject
286286# ifdef ZgatewayPilight
287287 String value = data[" value" ];
@@ -337,7 +337,7 @@ void pub(const char* topicori, JsonObject& data) {
337337
338338void pub (const char * topicori, const char * payload) {
339339 if (client.connected ()) {
340- String topic = String (mqtt_topic) + String (topicori);
340+ String topic = String (mqtt_topic) + String (gateway_name) + String ( topicori);
341341 Log.trace (F (" Pub ack %s into: %s" CR), payload, topic.c_str ());
342342 pubMQTT (topic, payload);
343343 } else {
@@ -460,6 +460,7 @@ void logJson(JsonObject& jsondata) {
460460bool cmpToMainTopic (const char * topicOri, const char * toAdd) {
461461 char topic[mqtt_topic_max_size];
462462 strcpy (topic, mqtt_topic);
463+ strcat (topic, gateway_name);
463464 strcat (topic, toAdd);
464465 if (strstr (topicOri, topic) != NULL ) {
465466 return true ;
@@ -478,6 +479,7 @@ void connectMQTT() {
478479 Log.warning (F (" MQTT connection..." CR));
479480 char topic[mqtt_topic_max_size];
480481 strcpy (topic, mqtt_topic);
482+ strcat (topic, gateway_name);
481483 strcat (topic, will_Topic);
482484 client.setBufferSize (mqtt_max_packet_size);
483485 if (client.connect (gateway_name, mqtt_user, mqtt_pass, topic, will_QoS, will_Retain, will_Message)) {
@@ -494,6 +496,7 @@ void connectMQTT() {
494496 // Subscribing to topic
495497 char topic2[mqtt_topic_max_size];
496498 strcpy (topic2, mqtt_topic);
499+ strcat (topic2, gateway_name);
497500 strcat (topic2, subjectMQTTtoX);
498501 if (client.subscribe (topic2)) {
499502#ifdef ZgatewayRF
@@ -545,10 +548,6 @@ void callback(char* topic, byte* payload, unsigned int length) {
545548 free (p);
546549}
547550
548- void setup_parameters () {
549- strcat (mqtt_topic, gateway_name);
550- }
551-
552551void setup () {
553552 // Launch serial for debugging purposes
554553 Serial.begin (SERIAL_BAUD);
@@ -623,8 +622,6 @@ void setup() {
623622 client.setServer (mqtt_server, port);
624623#endif
625624
626- setup_parameters ();
627-
628625 client.setCallback (callback);
629626
630627 delay (1500 );
@@ -961,6 +958,29 @@ void eraseAndRestart() {
961958# endif
962959}
963960
961+ void saveMqttConfig () {
962+ Log.trace (F (" saving config" CR));
963+ DynamicJsonBuffer jsonBuffer;
964+ JsonObject& json = jsonBuffer.createObject ();
965+ json[" mqtt_server" ] = mqtt_server;
966+ json[" mqtt_port" ] = mqtt_port;
967+ json[" mqtt_user" ] = mqtt_user;
968+ json[" mqtt_pass" ] = mqtt_pass;
969+ json[" mqtt_topic" ] = mqtt_topic;
970+ json[" gateway_name" ] = gateway_name;
971+ json[" mqtt_broker_secure" ] = mqtt_secure;
972+ json[" mqtt_broker_cert" ] = mqtt_cert;
973+
974+ File configFile = SPIFFS.open (" /config.json" , " w" );
975+ if (!configFile) {
976+ Log.error (F (" failed to open config file for writing" CR));
977+ }
978+
979+ json.printTo (Serial);
980+ json.printTo (configFile);
981+ configFile.close ();
982+ }
983+
964984void setup_wifimanager (bool reset_settings) {
965985# ifdef TRIGGER_GPIO
966986 pinMode (TRIGGER_GPIO, INPUT_PULLUP);
@@ -1128,27 +1148,7 @@ void setup_wifimanager(bool reset_settings) {
11281148 }
11291149
11301150 // save the custom parameters to FS
1131- Log.trace (F (" saving config" CR));
1132- DynamicJsonBuffer jsonBuffer;
1133- JsonObject& json = jsonBuffer.createObject ();
1134- json[" mqtt_server" ] = mqtt_server;
1135- json[" mqtt_port" ] = mqtt_port;
1136- json[" mqtt_user" ] = mqtt_user;
1137- json[" mqtt_pass" ] = mqtt_pass;
1138- json[" mqtt_topic" ] = mqtt_topic;
1139- json[" gateway_name" ] = gateway_name;
1140- json[" mqtt_secure" ] = mqtt_secure;
1141- json[" mqtt_cert" ] = mqtt_cert;
1142-
1143- File configFile = SPIFFS.open (" /config.json" , " w" );
1144- if (!configFile) {
1145- Log.error (F (" failed to open config file for writing" CR));
1146- }
1147-
1148- json.printTo (Serial);
1149- json.printTo (configFile);
1150- configFile.close ();
1151- // end save
1151+ saveMqttConfig ();
11521152 }
11531153}
11541154# ifdef ESP32_ETHERNET
@@ -1809,7 +1809,102 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
18091809 stateMeasures ();
18101810 }
18111811 }
1812+
1813+ if (SYSdata.containsKey (" wifi_ssid" ) && SYSdata.containsKey (" wifi_pass" )) {
1814+ # if defined(ZgatewayBT) && defined(ESP32)
1815+ stopProcessing ();
1816+ # endif
1817+ String prev_ssid = WiFi.SSID ();
1818+ String prev_pass = WiFi.psk ();
1819+ client.disconnect ();
1820+ WiFi.disconnect (true );
1821+
1822+ Log.warning (F (" Attempting connection to new AP" CR));
1823+ WiFi.begin ((const char *)SYSdata[" wifi_ssid" ], (const char *)SYSdata[" wifi_pass" ]);
1824+ WiFi.waitForConnectResult ();
1825+
1826+ if (WiFi.status () != WL_CONNECTED) {
1827+ Log.error (F (" Failed to connect to new AP; falling back" CR));
1828+ WiFi.disconnect (true );
1829+ WiFi.begin (prev_ssid.c_str (), prev_pass.c_str ());
1830+ if (WiFi.waitForConnectResult () != WL_CONNECTED) {
1831+ # if defined(ESP8266)
1832+ ESP.reset ();
1833+ # else
1834+ ESP.restart ();
1835+ # endif
1836+ }
1837+ }
1838+ # if defined(ZgatewayBT) && defined(ESP32)
1839+ startProcessing ();
1840+ # endif
1841+ }
1842+
1843+ if (SYSdata.containsKey (" mqtt_user" ) && SYSdata.containsKey (" mqtt_pass" )) {
1844+ # if defined(ZgatewayBT) && defined(ESP32)
1845+ stopProcessing ();
1846+ # endif
1847+ client.disconnect ();
1848+ bool update_server = false ;
1849+ void * prev_client = nullptr ;
1850+
1851+ if (SYSdata.containsKey (" mqtt_server" ) &&
1852+ SYSdata.containsKey (" mqtt_port" ) &&
1853+ SYSdata.containsKey (" mqtt_secure" )) {
1854+ update_server = true ;
1855+
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+ }
1865+ }
1866+
1867+ client.setServer (SYSdata.get <const char *>(" mqtt_server" ), SYSdata.get <unsigned int >(" mqtt_port" ));
1868+ }
1869+
1870+ String prev_user = mqtt_user;
1871+ String prev_pass = mqtt_pass;
1872+ strcpy (mqtt_user, SYSdata[" mqtt_user" ]);
1873+ strcpy (mqtt_pass, SYSdata[" mqtt_pass" ]);
1874+
1875+ connectMQTT ();
1876+
1877+ if (client.connected ()) {
1878+ if (update_server) {
1879+ strcpy (mqtt_server, SYSdata[" mqtt_server" ]);
1880+ strcpy (mqtt_port, SYSdata[" mqtt_port" ]);
1881+ if (prev_client != nullptr ) {
1882+ mqtt_secure = !mqtt_secure;
1883+ delete prev_client;
1884+ }
1885+ }
1886+ # ifndef ESPWifiManualSetup
1887+ saveMqttConfig ();
1888+ # endif
1889+ } else {
1890+ if (update_server) {
1891+ if (prev_client != nullptr ) {
1892+ delete eClient;
1893+ eClient = prev_client;
1894+ }
1895+ uint16_t port = strtol (mqtt_port, NULL , 10 );
1896+ client.setServer (mqtt_server, port);
1897+ }
1898+ strcpy (mqtt_user, prev_user.c_str ());
1899+ strcpy (mqtt_pass, prev_pass.c_str ());
1900+ connectMQTT ();
1901+ }
1902+ # if defined(ZgatewayBT) && defined(ESP32)
1903+ startProcessing ();
1904+ # endif
1905+ }
18121906#endif
1907+
18131908#ifdef ZmqttDiscovery
18141909 if (SYSdata.containsKey (" discovery" )) {
18151910 if (SYSdata.is <bool >(" discovery" )) {
0 commit comments