Skip to content

Commit 2518345

Browse files
committed
Add reconnecting on message publish
Should fix some connection issues, as devices will reconnect when publishing heartbeat
1 parent 508511f commit 2518345

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Mokosh
2-
version=0.4.0
2+
version=0.4.1
33
author=Marcin Badurowicz
44
maintainer=Marcin Badurowicz <[email protected]>
55
sentence=Framework for IoT Arduino/ESP8266/ESP32 devices

src/Mokosh.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ void Mokosh::disableFS() {
149149
}
150150

151151
bool Mokosh::reconnect() {
152+
if (client->connected())
153+
return true;
154+
152155
uint8_t trials = 0;
153156

154157
while (!client->connected()) {
@@ -542,6 +545,10 @@ void Mokosh::publish(const char* subtopic, const char* payload) {
542545
char topic[60] = {0};
543546
sprintf(topic, "%s_%s/%s", this->prefix.c_str(), this->hostNameC, subtopic);
544547

548+
if (!this->client->connected()) {
549+
this->reconnect();
550+
}
551+
545552
this->mqtt->publish(topic, payload);
546553
}
547554

0 commit comments

Comments
 (0)