mirror of
https://gitlab.com/wirelos/sprocket-plugin-mqtt.git
synced 2025-12-15 14:08:20 +01:00
fix connect
This commit is contained in:
@@ -11,7 +11,7 @@ lib_deps =
|
|||||||
Hash
|
Hash
|
||||||
TaskScheduler
|
TaskScheduler
|
||||||
SPIFFS
|
SPIFFS
|
||||||
ArduinoJson
|
ArduinoJson@5.13.4
|
||||||
https://gitlab.com/wirelos/sprocket-lib.git#develop
|
https://gitlab.com/wirelos/sprocket-lib.git#develop
|
||||||
https://gitlab.com/wirelos/sprocket-network-wifi.git
|
https://gitlab.com/wirelos/sprocket-network-wifi.git
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ void MqttPlugin::activate(Scheduler *scheduler)
|
|||||||
{
|
{
|
||||||
applyConfigFromFile("/mqttConfig.json");
|
applyConfigFromFile("/mqttConfig.json");
|
||||||
client = new PubSubClient(brokerHost.c_str(), brokerPort, bind(&MqttPlugin::downstreamHandler, this, _1, _2, _3), wifiClient);
|
client = new PubSubClient(brokerHost.c_str(), brokerPort, bind(&MqttPlugin::downstreamHandler, this, _1, _2, _3), wifiClient);
|
||||||
enableConnectTask(scheduler);
|
|
||||||
|
scheduler = scheduler;
|
||||||
enableProcessTask(scheduler);
|
enableProcessTask(scheduler);
|
||||||
PRINT_MSG(Serial, "MQTT", "plugin activated");
|
PRINT_MSG(Serial, "MQTT", "plugin activated");
|
||||||
}
|
}
|
||||||
@@ -43,10 +44,17 @@ void MqttPlugin::enableConnectTask(Scheduler *scheduler)
|
|||||||
|
|
||||||
void MqttPlugin::enableProcessTask(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);
|
scheduler->addTask(processTask);
|
||||||
processTask.enable();
|
processTask.enable();
|
||||||
}
|
}
|
||||||
|
void MqttPlugin::loop() {
|
||||||
|
if(client->connected()){
|
||||||
|
client->loop();
|
||||||
|
} else {
|
||||||
|
enableConnectTask(scheduler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MqttPlugin::connect()
|
void MqttPlugin::connect()
|
||||||
{
|
{
|
||||||
@@ -57,7 +65,7 @@ void MqttPlugin::connect()
|
|||||||
if(user.length() > 0 && pass.length() > 0){
|
if(user.length() > 0 && pass.length() > 0){
|
||||||
connected = client->connect(clientName.c_str(), user.c_str(), pass.c_str());
|
connected = client->connect(clientName.c_str(), user.c_str(), pass.c_str());
|
||||||
} else {
|
} else {
|
||||||
client->connect(clientName.c_str());
|
connected = client->connect(clientName.c_str());
|
||||||
}
|
}
|
||||||
if (connected)
|
if (connected)
|
||||||
{
|
{
|
||||||
@@ -85,8 +93,8 @@ void MqttPlugin::connect()
|
|||||||
PRINT_MSG(Serial, "MQTT", "connect failed");
|
PRINT_MSG(Serial, "MQTT", "connect failed");
|
||||||
connectTries++;
|
connectTries++;
|
||||||
if(connectTries == maxConnectTries){
|
if(connectTries == maxConnectTries){
|
||||||
connectTask.disable();
|
|
||||||
}
|
}
|
||||||
|
connectTask.disable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class MqttPlugin : public Plugin
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PubSubClient *client;
|
PubSubClient *client;
|
||||||
|
Scheduler* scheduler;
|
||||||
|
|
||||||
MqttPlugin(MqttConfig cfg, bool bindUp = true, bool bindDown = true);
|
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.
|
* 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 downstreamHandler(char *topic, uint8_t *payload, unsigned int length);
|
||||||
|
|
||||||
|
virtual void loop();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user