fixed auto reconnect and resubscribe inside mqtt wrapper loop
This commit is contained in:
41
src/mqtt.cpp
41
src/mqtt.cpp
@@ -1,7 +1,7 @@
|
||||
#include <mqtt.h>
|
||||
|
||||
#define STACK_DEPTH 4096
|
||||
#define PRIOTITY 1
|
||||
#define PRIOTITY 0
|
||||
|
||||
MQTTwrapper::MQTTwrapper() : m_config(Config::getInstance()), m_tcp(NetworkClient()), m_client(PubSubClient(m_tcp)), m_loopHandle(NULL)
|
||||
{
|
||||
@@ -19,7 +19,7 @@ const bool MQTTwrapper::connect()
|
||||
{
|
||||
if (!m_client.connect(m_config.m_mqttClientName.c_str()))
|
||||
{
|
||||
LOG_ERROR("Unable to connect to MQTT Host", m_config.m_mqttHost.c_str());
|
||||
LOG_ERROR("MQTT unable to connect to host", m_config.m_mqttHost.c_str());
|
||||
return false;
|
||||
}
|
||||
LOG_INFO("MQTT client connected to", m_config.m_mqttHost.c_str());
|
||||
@@ -78,6 +78,11 @@ const bool MQTTwrapper::unsubscribe(topic_t topic)
|
||||
const bool MQTTwrapper::publish(topic_t topic, const ArduinoJson::JsonDocument obj)
|
||||
{
|
||||
std::string message;
|
||||
if (!m_client.connected())
|
||||
{
|
||||
LOG_ERROR("MQTT client not connected");
|
||||
return false;
|
||||
}
|
||||
if (!ArduinoJson::serializeJson(obj, message))
|
||||
{
|
||||
LOG_ERROR("MQTT failed to serialize object");
|
||||
@@ -95,8 +100,8 @@ const bool MQTTwrapper::publish(topic_t topic, const ArduinoJson::JsonDocument o
|
||||
void MQTTwrapper::callback(char *topic, uint8_t *payload, unsigned int length)
|
||||
{
|
||||
std::string pl;
|
||||
pl.resize(length+1);
|
||||
std::snprintf(pl.data(), length+1, "%s", payload);
|
||||
pl.resize(length + 1);
|
||||
std::snprintf(pl.data(), length + 1, "%s", payload);
|
||||
auto inst = getInstance();
|
||||
if (inst)
|
||||
{
|
||||
@@ -124,11 +129,31 @@ void MQTTwrapper::clientLoop(void *params)
|
||||
{
|
||||
auto client = (MQTTwrapper *)(params);
|
||||
auto loopTime = client->m_config.m_mqttLoopTime;
|
||||
LOG_INFO("Starting MQTT client loop");
|
||||
while (client->m_client.connected())
|
||||
uint8_t connectAttempt(0);
|
||||
LOG_INFO("MQTT starting client loop");
|
||||
while (connectAttempt++ < client->m_config.m_mqttRetries)
|
||||
{
|
||||
client->m_client.loop();
|
||||
vTaskDelay(pdMS_TO_TICKS(loopTime));
|
||||
while (client->m_client.connected())
|
||||
{
|
||||
client->m_client.loop();
|
||||
vTaskDelay(pdMS_TO_TICKS(loopTime));
|
||||
}
|
||||
if (client->m_client.state() <= MQTT_CONNECTED)
|
||||
{
|
||||
LOG_ERROR("MQTT disconnect reason ", client->m_client.state());
|
||||
vTaskDelay(pdMS_TO_TICKS(loopTime * 50));
|
||||
const bool ok = client->m_client.connect(client->m_config.m_mqttClientName.c_str());
|
||||
LOG_WARN("MQTT reconnected", ok ? "True" : "False");
|
||||
if (ok)
|
||||
{
|
||||
for (auto v : client->m_actionMap)
|
||||
{
|
||||
LOG_WARN("MQTT resubscribing to",v.first.c_str());
|
||||
client->m_client.subscribe(v.first.c_str());
|
||||
}
|
||||
connectAttempt = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG_ERROR("MQTT client loop terminated, disconnected");
|
||||
client->m_loopHandle = NULL;
|
||||
|
||||
Reference in New Issue
Block a user