Skip to content

Commit

Permalink
mqtt critical fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DmytroKorniienko committed Nov 5, 2020
1 parent fee53ef commit f276d20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 6 additions & 0 deletions EmbUI/EmbUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ class EmbUI

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

String m_pref; // к сожалению они нужны, т.к. в клиент передаются указатели на уже имеющийся объект, значит на конфиг ссылку отдавать нельзя!!!
String m_host;
String m_port;
String m_user;
String m_pass;

void var(const String &key, const String &value, bool force = false);
void var_create(const String &key, const String &value);
void section_handle_add(const String &btn, buttonCallback response);
Expand Down
26 changes: 15 additions & 11 deletions EmbUI/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ extern EmbUI embui;
void EmbUI::connectToMqtt() {
LOG(println, PSTR("UI: Connecting to MQTT..."));

//String m_pref=param(FPSTR(P_m_pref));
String m_host=param(FPSTR(P_m_host));
String m_port=param(FPSTR(P_m_port));
String m_user=param(FPSTR(P_m_user));
String m_pass=param(FPSTR(P_m_pass));
m_pref=param(FPSTR(P_m_pref));
m_host=param(FPSTR(P_m_host));
m_port=param(FPSTR(P_m_port));
m_user=param(FPSTR(P_m_user));
m_pass=param(FPSTR(P_m_pass));

IPAddress ip;
bool isIP = ip.fromString(m_host);
mqttClient.setCredentials(m_user.c_str(), m_pass.c_str());
Expand All @@ -22,11 +23,13 @@ void EmbUI::connectToMqtt() {
else
mqttClient.setServer(m_host.c_str(), m_port.toInt());

mqttClient.setClientId(m_pref.isEmpty() ? mc : m_pref.c_str());

mqttClient.connect();
}

String EmbUI::id(const String &topic){
String ret = param(FPSTR(P_m_pref));
String ret = m_pref;
if (ret.isEmpty()) return topic;

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

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

0 comments on commit f276d20

Please sign in to comment.