Skip to content

Commit 002e4f2

Browse files
committed
fixed mqtt issue with not reconnecting to broker; ran through formatter
1 parent 00ac4d3 commit 002e4f2

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/Flux/flxMQTTESP32.h

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
* trade secret of SparkFun Electronics Inc. It is not to be disclosed
77
* to anyone outside of this organization. Reproduction by any means
88
* whatsoever is prohibited without express written permission.
9-
*
9+
*
1010
*---------------------------------------------------------------------------------
1111
*/
12-
1312

1413
#pragma once
1514

1615
#ifdef ESP32
1716

1817
#include "flxCoreInterface.h"
1918
#include "flxFS.h"
20-
#include "flxNetwork.h"
2119
#include "flxFlux.h"
20+
#include "flxNetwork.h"
2221

2322
#include <ArduinoMqttClient.h>
2423
#include <WiFiClientSecure.h>
@@ -145,7 +144,7 @@ template <class Object, typename CLIENT> class flxMQTTESP32Base : public flxActi
145144
{
146145

147146
// if we don't have a network, or if the network is not connected, return an error
148-
if ( !_theNetwork || !_theNetwork->isConnected())
147+
if (!_theNetwork || !_theNetwork->isConnected())
149148
return false;
150149

151150
// Already connected?
@@ -198,10 +197,22 @@ template <class Object, typename CLIENT> class flxMQTTESP32Base : public flxActi
198197
// flxWriter interface method
199198
virtual void write(const char *value, bool newline)
200199
{
201-
// if we are not connected, ignore
202-
if (!connected() || !value)
200+
201+
// Should we continue?
202+
// enabled? Have a value to send?
203+
if (!_isEnabled || !value)
203204
return;
204205

206+
// If we lost the connection to the broker, try to reconnect...
207+
if (!_mqttClient.connected() || !_wifiClient.connected())
208+
{
209+
flxLog_W_(F("%s disconnected - reconnecting..."), this->name());
210+
if (!connect())
211+
return;
212+
213+
flxLog_N(F("reconnected"));
214+
}
215+
205216
// do we have a topic?
206217
if (topic().length() == 0)
207218
{
@@ -237,8 +248,8 @@ template <class Object, typename CLIENT> class flxMQTTESP32Base : public flxActi
237248
flxPropertyString<flxMQTTESP32Base> clientName;
238249

239250
// Buffer size property
240-
flxPropertyRWUint16<flxMQTTESP32Base, &flxMQTTESP32Base::get_bufferSize, &flxMQTTESP32Base::set_bufferSize> bufferSize =
241-
{0};
251+
flxPropertyRWUint16<flxMQTTESP32Base, &flxMQTTESP32Base::get_bufferSize, &flxMQTTESP32Base::set_bufferSize>
252+
bufferSize = {0};
242253

243254
// username and password properties - some brokers requires this
244255
flxPropertyString<flxMQTTESP32Base> username;
@@ -260,7 +271,7 @@ template <class Object, typename CLIENT> class flxMQTTESP32Base : public flxActi
260271

261272
class flxMQTTESP32 : public flxMQTTESP32Base<flxMQTTESP32, WiFiClient>, public flxWriter
262273
{
263-
public:
274+
public:
264275
flxMQTTESP32()
265276
{
266277
this->setName("MQTT Client", "A generic MQTT Client");
@@ -299,7 +310,7 @@ template <class Object> class flxMQTTESP32SecureCore : public flxMQTTESP32Base<O
299310
std::string tmp = _pCACert ? _pCACert : "";
300311
return tmp;
301312
}
302-
313+
303314
//---------------------------------------------------------
304315
void set_caCert(std::string theCert)
305316
{
@@ -397,7 +408,6 @@ template <class Object> class flxMQTTESP32SecureCore : public flxMQTTESP32Base<O
397408
_clientFilename = theFile;
398409
}
399410

400-
401411
//---------------------------------------------------------
402412
std::string get_clientKeyFilename(void)
403413
{
@@ -476,7 +486,7 @@ template <class Object> class flxMQTTESP32SecureCore : public flxMQTTESP32Base<O
476486
flxMQTTESP32SecureCore() : _pCACert{nullptr}, _pClientCert{nullptr}, _pClientKey{nullptr}, _fileSystem{nullptr}
477487
{
478488
flxRegister(caCertificate, "CA Certificate",
479-
"The Certificate Authority certificate. If set, the connection is secure");
489+
"The Certificate Authority certificate. If set, the connection is secure");
480490
flxRegister(clientCertificate, "Client Certificate", "The certificate for the client connection");
481491
flxRegister(clientKey, "Client Key", "The secure key used for client verification");
482492

@@ -525,28 +535,28 @@ template <class Object> class flxMQTTESP32SecureCore : public flxMQTTESP32Base<O
525535

526536
// Security certs/keys
527537
flxPropertyRWSecretString<flxMQTTESP32SecureCore, &flxMQTTESP32SecureCore::get_caCert,
528-
&flxMQTTESP32SecureCore::set_caCert>
538+
&flxMQTTESP32SecureCore::set_caCert>
529539
caCertificate;
530540

531541
flxPropertyRWSecretString<flxMQTTESP32SecureCore, &flxMQTTESP32SecureCore::get_clientCert,
532-
&flxMQTTESP32SecureCore::set_clientCert>
542+
&flxMQTTESP32SecureCore::set_clientCert>
533543
clientCertificate;
534544

535545
flxPropertyRWSecretString<flxMQTTESP32SecureCore, &flxMQTTESP32SecureCore::get_clientKey,
536-
&flxMQTTESP32SecureCore::set_clientKey>
546+
&flxMQTTESP32SecureCore::set_clientKey>
537547
clientKey;
538548

539549
// Define filename properties to access the secure keys. A filesystem must be provided to this object for it to read
540550
// the data.
541551
// Security certs/keys
542552
flxPropertyRWString<flxMQTTESP32SecureCore, &flxMQTTESP32SecureCore::get_caCertFilename,
543-
&flxMQTTESP32SecureCore::set_caCertFilename>
553+
&flxMQTTESP32SecureCore::set_caCertFilename>
544554
caCertFilename;
545555
flxPropertyRWString<flxMQTTESP32SecureCore, &flxMQTTESP32SecureCore::get_clientCertFilename,
546-
&flxMQTTESP32SecureCore::set_clientCertFilename>
556+
&flxMQTTESP32SecureCore::set_clientCertFilename>
547557
clientCertFilename;
548558
flxPropertyRWString<flxMQTTESP32SecureCore, &flxMQTTESP32SecureCore::get_clientKeyFilename,
549-
&flxMQTTESP32SecureCore::set_clientKeyFilename>
559+
&flxMQTTESP32SecureCore::set_clientKeyFilename>
550560
clientKeyFilename;
551561

552562
// We need perm version of the keys for the secure connection, so the values are stashed in allocated
@@ -563,10 +573,9 @@ template <class Object> class flxMQTTESP32SecureCore : public flxMQTTESP32Base<O
563573
std::string _keyFilename;
564574
};
565575

566-
567576
class flxMQTTESP32Secure : public flxMQTTESP32SecureCore<flxMQTTESP32Secure>, public flxWriter
568577
{
569-
public:
578+
public:
570579
flxMQTTESP32Secure()
571580
{
572581
this->setName("MQTT Secure Client", "A secure MQTT client");

0 commit comments

Comments
 (0)