Skip to content

Commit

Permalink
network.h, network.cpp: disconnect MQTT when changing its params
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo35 committed Nov 18, 2024
1 parent 2b97b72 commit 992b726
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions SmartEVSE-3/include/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ class MQTTclient_t {
default_opts.qos = 0;
default_opts.retain = false;
}
void disconnect(void) { mg_mqtt_disconnect(s_conn, &default_opts); };
#else
void connect(void);
void disconnect(void) { esp_mqtt_client_stop(client); };
#endif
void publish(const String &topic, const int32_t &payload, bool retained, int qos) { publish(topic, String(payload), retained, qos); };
void publish(const String &topic, const String &payload, bool retained, int qos);
Expand Down
10 changes: 7 additions & 3 deletions SmartEVSE-3/src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,9 +1037,6 @@ static void fn_http_server(struct mg_connection *c, int ev, void *ev_data) {

if(request->hasParam("mqtt_host")) {
MQTTHost = request->getParam("mqtt_host")->value();
#if MQTT_ESP == 1
MQTTclient.connect();
#endif
doc["mqtt_host"] = MQTTHost;
}

Expand Down Expand Up @@ -1072,6 +1069,13 @@ static void fn_http_server(struct mg_connection *c, int ev, void *ev_data) {
}
doc["mqtt_password_set"] = (MQTTpassword != "");
}

// disconnect mqtt so it will automatically reconnect with then new params
MQTTclient.disconnect();
#if MQTT_ESP == 1
MQTTclient.connect();
#endif

if (preferences.begin("settings", false) ) {
preferences.putString("MQTTpassword", MQTTpassword);
preferences.putString("MQTTuser", MQTTuser);
Expand Down

0 comments on commit 992b726

Please sign in to comment.