Skip to content

Commit f276d20

Browse files
mqtt critical fix
1 parent fee53ef commit f276d20

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

EmbUI/EmbUI.h

+6
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ class EmbUI
203203

204204
char mc[7]; // id из последних 3 байт mac-адреса "ffffff"
205205

206+
String m_pref; // к сожалению они нужны, т.к. в клиент передаются указатели на уже имеющийся объект, значит на конфиг ссылку отдавать нельзя!!!
207+
String m_host;
208+
String m_port;
209+
String m_user;
210+
String m_pass;
211+
206212
void var(const String &key, const String &value, bool force = false);
207213
void var_create(const String &key, const String &value);
208214
void section_handle_add(const String &btn, buttonCallback response);

EmbUI/mqtt.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ extern EmbUI embui;
99
void EmbUI::connectToMqtt() {
1010
LOG(println, PSTR("UI: Connecting to MQTT..."));
1111

12-
//String m_pref=param(FPSTR(P_m_pref));
13-
String m_host=param(FPSTR(P_m_host));
14-
String m_port=param(FPSTR(P_m_port));
15-
String m_user=param(FPSTR(P_m_user));
16-
String m_pass=param(FPSTR(P_m_pass));
12+
m_pref=param(FPSTR(P_m_pref));
13+
m_host=param(FPSTR(P_m_host));
14+
m_port=param(FPSTR(P_m_port));
15+
m_user=param(FPSTR(P_m_user));
16+
m_pass=param(FPSTR(P_m_pass));
17+
1718
IPAddress ip;
1819
bool isIP = ip.fromString(m_host);
1920
mqttClient.setCredentials(m_user.c_str(), m_pass.c_str());
@@ -22,11 +23,13 @@ void EmbUI::connectToMqtt() {
2223
else
2324
mqttClient.setServer(m_host.c_str(), m_port.toInt());
2425

26+
mqttClient.setClientId(m_pref.isEmpty() ? mc : m_pref.c_str());
27+
2528
mqttClient.connect();
2629
}
2730

2831
String EmbUI::id(const String &topic){
29-
String ret = param(FPSTR(P_m_pref));
32+
String ret = m_pref;
3033
if (ret.isEmpty()) return topic;
3134

3235
ret += '/'; ret += topic;
@@ -56,11 +59,11 @@ void EmbUI::mqtt(const String &pref, const String &host, int port, const String
5659
LOG(println, PSTR("UI: MQTT host is empty - disabled!"));
5760
return; // выходим если host не задан
5861
}
59-
String m_pref=param(FPSTR(P_m_pref));
60-
String m_host=param(FPSTR(P_m_host));
61-
String m_port=param(FPSTR(P_m_port));
62-
String m_user=param(FPSTR(P_m_user));
63-
String m_pass=param(FPSTR(P_m_pass));
62+
m_pref=param(FPSTR(P_m_pref));
63+
m_host=param(FPSTR(P_m_host));
64+
m_port=param(FPSTR(P_m_port));
65+
m_user=param(FPSTR(P_m_user));
66+
m_pass=param(FPSTR(P_m_pass));
6467
IPAddress ip;
6568
bool isIP = ip.fromString(m_host);
6669

@@ -81,6 +84,7 @@ void EmbUI::mqtt(const String &pref, const String &host, int port, const String
8184
mqttClient.onUnsubscribe(onMqttUnsubscribe);
8285
mqttClient.onMessage(onMqttMessage);
8386
mqttClient.onPublish(onMqttPublish);
87+
mqttClient.setClientId(m_pref.isEmpty() ? mc : m_pref.c_str());
8488
mqttClient.setCredentials(m_user.c_str(), m_pass.c_str());
8589
if(isIP)
8690
mqttClient.setServer(ip, m_port.toInt());

0 commit comments

Comments
 (0)