From acff34e3788f42ecd190c1ee18963204da0934fe Mon Sep 17 00:00:00 2001 From: Partick Balsiger Date: Sat, 27 Apr 2019 12:53:11 +0200 Subject: [PATCH] fix connect --- platformio.ini | 2 +- src/MqttPlugin.cpp | 16 ++++++++++++---- src/MqttPlugin.h | 5 ++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index 67c3eb3..023ef2b 100755 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,7 @@ lib_deps = Hash TaskScheduler SPIFFS - ArduinoJson + ArduinoJson@5.13.4 https://gitlab.com/wirelos/sprocket-lib.git#develop https://gitlab.com/wirelos/sprocket-network-wifi.git diff --git a/src/MqttPlugin.cpp b/src/MqttPlugin.cpp index 6e5eb06..57452a6 100755 --- a/src/MqttPlugin.cpp +++ b/src/MqttPlugin.cpp @@ -29,7 +29,8 @@ void MqttPlugin::activate(Scheduler *scheduler) { applyConfigFromFile("/mqttConfig.json"); client = new PubSubClient(brokerHost.c_str(), brokerPort, bind(&MqttPlugin::downstreamHandler, this, _1, _2, _3), wifiClient); - enableConnectTask(scheduler); + + scheduler = scheduler; enableProcessTask(scheduler); PRINT_MSG(Serial, "MQTT", "plugin activated"); } @@ -43,10 +44,17 @@ void MqttPlugin::enableConnectTask(Scheduler *scheduler) void MqttPlugin::enableProcessTask(Scheduler *scheduler) { - processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, bind(&PubSubClient::loop, client)); + processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, bind(&MqttPlugin::loop, this)); scheduler->addTask(processTask); processTask.enable(); } +void MqttPlugin::loop() { + if(client->connected()){ + client->loop(); + } else { + enableConnectTask(scheduler); + } +} void MqttPlugin::connect() { @@ -57,7 +65,7 @@ void MqttPlugin::connect() if(user.length() > 0 && pass.length() > 0){ connected = client->connect(clientName.c_str(), user.c_str(), pass.c_str()); } else { - client->connect(clientName.c_str()); + connected = client->connect(clientName.c_str()); } if (connected) { @@ -85,8 +93,8 @@ void MqttPlugin::connect() PRINT_MSG(Serial, "MQTT", "connect failed"); connectTries++; if(connectTries == maxConnectTries){ - connectTask.disable(); } + connectTask.disable(); } } } diff --git a/src/MqttPlugin.h b/src/MqttPlugin.h index 59deb5f..5920c93 100755 --- a/src/MqttPlugin.h +++ b/src/MqttPlugin.h @@ -17,7 +17,8 @@ class MqttPlugin : public Plugin { public: PubSubClient *client; - + Scheduler* scheduler; + MqttPlugin(MqttConfig cfg, bool bindUp = true, bool bindDown = true); /** @@ -65,6 +66,8 @@ private: * Everything after the prefix is used as local topic and dispatched to local subscriptions. */ virtual void downstreamHandler(char *topic, uint8_t *payload, unsigned int length); + + virtual void loop(); }; #endif \ No newline at end of file