Skip to content

Commit fee53ef

Browse files
mqtt fix
1 parent 803c9b6 commit fee53ef

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

EmbUI/EmbUI.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class EmbUI
230230
void mqtt(const String &pref, const String &host, int port, const String &user, const String &pass, void (*mqttFunction) (const String &topic, const String &payload), void (*mqttConnect) ());
231231
void mqtt(const String &host, int port, const String &user, const String &pass, void (*mqttFunction) (const String &topic, const String &payload), void (*mqttConnect) ());
232232
void mqtt(const String &host, int port, const String &user, const String &pass, void (*mqttFunction) (const String &topic, const String &payload), void (*mqttConnect) (), bool remotecontrol);
233-
void mqtt_reconnect();
233+
void mqttReconnect();
234234
void subscribe(const String &topic);
235235
void publish(const String &topic, const String &payload);
236236
void publish(const String &topic, const String &payload, bool retained);
@@ -267,7 +267,8 @@ class EmbUI
267267
void pub_mqtt(const String &key, const String &value);
268268
void mqtt_handle();
269269
void subscribeAll(bool isOnlyGetSet=true);
270-
270+
void mqtt_reconnect();
271+
271272
/**
272273
* устанавлием режим WiFi
273274
*/

EmbUI/mqtt.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,22 @@
77
extern EmbUI embui;
88

99
void EmbUI::connectToMqtt() {
10-
LOG(println, PSTR("UI: Connecting to MQTT..."));
11-
mqttClient.connect();
10+
LOG(println, PSTR("UI: Connecting to MQTT..."));
11+
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));
17+
IPAddress ip;
18+
bool isIP = ip.fromString(m_host);
19+
mqttClient.setCredentials(m_user.c_str(), m_pass.c_str());
20+
if(isIP)
21+
mqttClient.setServer(ip, m_port.toInt());
22+
else
23+
mqttClient.setServer(m_host.c_str(), m_port.toInt());
24+
25+
mqttClient.connect();
1226
}
1327

1428
String EmbUI::id(const String &topic){
@@ -134,6 +148,10 @@ void EmbUI::mqtt_handle(){
134148
mqtt_reconnect();
135149
}
136150

151+
void EmbUI::mqttReconnect(){ // принудительный реконнект, при смене чего-либо в UI
152+
sysData.mqtt_connected = false;
153+
}
154+
137155
/*
138156
* TODO: убрать этот бардак в планировщик
139157
*/

EmbUI/ui.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ void Interface::color(const String &id, const String &label){
291291
}
292292

293293
void Interface::file(const String &name, const String &action, const String &label){
294-
StaticJsonDocument<256> obj;
294+
StaticJsonDocument<256> obj;
295295
obj[FPSTR(P_html)] = FPSTR(P_file);
296296
obj[F("name")] = name;
297297
obj[F("action")] = action;

0 commit comments

Comments
 (0)