mirror of
https://gitlab.com/wirelos/sprocket-plugin-mqtt.git
synced 2025-12-14 13:51:27 +01:00
fix connect
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ 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
|
||||
Reference in New Issue
Block a user