mqtt wrapper first version working
This commit is contained in:
316
src/config.h
316
src/config.h
@@ -1,316 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <FFat.h>
|
||||
|
||||
#include <mutex>
|
||||
|
||||
class FSmount
|
||||
{
|
||||
public:
|
||||
FSmount()
|
||||
{
|
||||
if (!FFat.begin(false))
|
||||
{
|
||||
LOG_ERROR("Unable to mount filesystem without formatting");
|
||||
if (!FFat.begin(true))
|
||||
{
|
||||
LOG_ERROR("Formatted and mounted filesystem");
|
||||
}
|
||||
}
|
||||
|
||||
LOG_INFO("Local Filesystem Mounted Correctly");
|
||||
const auto totalBytes = FFat.totalBytes();
|
||||
const auto freeBytes = FFat.freeBytes();
|
||||
const auto usedBytes = FFat.usedBytes();
|
||||
const auto mountPoint = FFat.mountpoint();
|
||||
LOG_INFO("Local filesystem, total", totalBytes / 1024, "KB - used", usedBytes / 1024, "KB - free", freeBytes / 1024, "KB");
|
||||
LOG_INFO("Local filesystem, mountpoint", mountPoint);
|
||||
}
|
||||
|
||||
~FSmount()
|
||||
{
|
||||
FFat.end(); // unmout filesystem to avoid corruption
|
||||
LOG_INFO("Local Filesystem Unmounted Correctly");
|
||||
}
|
||||
};
|
||||
|
||||
class Config
|
||||
{
|
||||
|
||||
public:
|
||||
static Config &getInstance()
|
||||
{
|
||||
static Config instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
private:
|
||||
Config() = default;
|
||||
Config(const Config &) = delete;
|
||||
Config &operator=(const Config &) = delete;
|
||||
|
||||
public:
|
||||
void init()
|
||||
{
|
||||
FSmount mount; // scoped mount of the filesystem
|
||||
|
||||
// Initialize and mount filesystem
|
||||
LOG_INFO("Initializing Config");
|
||||
|
||||
if (!FFat.exists("/config.json"))
|
||||
{
|
||||
LOG_WARN("Initializing default config");
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
File file = FFat.open("/config.json", FILE_READ, false);
|
||||
if (!file)
|
||||
{
|
||||
LOG_ERROR("Unable to open config.json");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ArduinoJson::deserializeJson(m_configJson, file) != ArduinoJson::DeserializationError::Ok)
|
||||
{
|
||||
LOG_ERROR("Unable to load config.json");
|
||||
}
|
||||
|
||||
std::string loadedConf;
|
||||
ArduinoJson::serializeJsonPretty(m_configJson, loadedConf);
|
||||
LOG_INFO("Loaded Configuration\n", loadedConf.c_str());
|
||||
|
||||
deserialize(); // convert from json format to class members
|
||||
file.close(); // close config file before unmounting filesystem
|
||||
};
|
||||
|
||||
void updateConfig(ArduinoJson::JsonDocument &json)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
{
|
||||
FSmount mount;
|
||||
m_configJson = json;
|
||||
deserialize();
|
||||
saveConfig();
|
||||
}; // filesystem is unmounted here
|
||||
delay(500);
|
||||
esp_restart(); // configuration updates trigger a cpu restart
|
||||
}
|
||||
|
||||
void resetConfig()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
{
|
||||
FSmount mount;
|
||||
LOG_WARN("Removing config.json");
|
||||
if (!FFat.remove("/config.json"))
|
||||
{
|
||||
LOG_ERROR("Unable to remove config.json");
|
||||
}
|
||||
LOG_WARN("Configuration reset, Restarting");
|
||||
}; // filesystem is unmounted here
|
||||
delay(500);
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
private:
|
||||
void saveConfig() // write configuration to flash memory
|
||||
{
|
||||
File file = FFat.open("/config.json", FILE_WRITE, true);
|
||||
if (!file)
|
||||
{
|
||||
LOG_ERROR("Unable to open config.json for writing");
|
||||
return;
|
||||
}
|
||||
serialize(); // serialize default configuration
|
||||
if (ArduinoJson::serializeJson(m_configJson, file) == 0)
|
||||
{
|
||||
LOG_ERROR("Serialization Failed");
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
////////////// SERIALIZATION + DESERIALIZATION ///////////////
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
void serialize()
|
||||
{
|
||||
// form class members to json document
|
||||
{
|
||||
auto globals = m_configJson["globals"].to<ArduinoJson::JsonObject>();
|
||||
globals["loopDelay"] = m_globalLoopDelay;
|
||||
};
|
||||
|
||||
{
|
||||
auto ethernet = m_configJson["ethernet"].to<ArduinoJson::JsonObject>();
|
||||
ethernet["hostname"] = m_ethHostname;
|
||||
ethernet["ipAddr"] = m_ethIpAddr;
|
||||
ethernet["netmask "] = m_ethNetmask;
|
||||
ethernet["gateway "] = m_ethGateway;
|
||||
};
|
||||
|
||||
{
|
||||
auto modbus = m_configJson["modbus"].to<ArduinoJson::JsonObject>();
|
||||
modbus["relayAddr"] = m_modbusRelayAddr;
|
||||
modbus["temperatureAddr"] = m_modbusTemperatureAddr;
|
||||
modbus["senecaAddr"] = m_modbusSenecaAddr;
|
||||
modbus["flowmeterAddr"] = m_modbusFlowmeterAddr;
|
||||
modbus["tankLevelAddr"] = m_modbusTankLevelAddr;
|
||||
};
|
||||
|
||||
{
|
||||
auto temperature = m_configJson["temperature"].to<ArduinoJson::JsonObject>();
|
||||
temperature["expectedSensors"] = m_tempExpectedSensors;
|
||||
auto values = temperature["correctionValues"].to<ArduinoJson::JsonArray>();
|
||||
for (auto v : m_tempCorrectionValues)
|
||||
{
|
||||
values.add(v);
|
||||
}
|
||||
};
|
||||
|
||||
{
|
||||
auto ntp = m_configJson["ntp"].to<ArduinoJson::JsonObject>();
|
||||
ntp["pool"] = m_ntpPool;
|
||||
ntp["timezone"] = m_ntpTimezone;
|
||||
ntp["updateInterval"] = m_ntpUpdateInterval;
|
||||
ntp["retries"] = m_ntpRetries;
|
||||
};
|
||||
|
||||
{
|
||||
auto mqtt = m_configJson["mqtt"].to<ArduinoJson::JsonObject>();
|
||||
mqtt["host"] = m_mqttHost;
|
||||
mqtt["port"] = m_mqttPort;
|
||||
mqtt["loopTime"] = m_mqttLoopTime;
|
||||
mqtt["clientName"] = m_mqttClientName;
|
||||
mqtt["retries"] = m_mqttRetries;
|
||||
auto publish = mqtt["publish"].to<ArduinoJson::JsonObject>();
|
||||
for (auto v : m_mqttSubscribe)
|
||||
{
|
||||
publish[v.first] = v.second;
|
||||
}
|
||||
auto subscribe = mqtt["subscribe"].to<ArduinoJson::JsonObject>();
|
||||
for (auto v : m_mqttPublish)
|
||||
{
|
||||
subscribe[v.first] = v.second;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
void deserialize()
|
||||
{ // from json document to class members
|
||||
if (m_configJson.isNull())
|
||||
{
|
||||
LOG_ERROR("NUll config document");
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
auto globals = m_configJson["globals"];
|
||||
m_globalLoopDelay = globals["loopDelay"].as<uint16_t>();
|
||||
};
|
||||
|
||||
{
|
||||
auto ethernet = m_configJson["ethernet"];
|
||||
m_ethHostname = ethernet["hostname"].as<std::string>();
|
||||
m_ethIpAddr = ethernet["ipAddr"].as<std::string>();
|
||||
m_ethNetmask = ethernet["netmask"].as<std::string>();
|
||||
m_ethGateway = ethernet["gateway"].as<std::string>();
|
||||
};
|
||||
|
||||
{
|
||||
auto modbus = m_configJson["modbus"];
|
||||
m_modbusRelayAddr = modbus["relayAddr"].as<uint8_t>();
|
||||
m_modbusTemperatureAddr = modbus["temperatureAddr"].as<uint8_t>();
|
||||
m_modbusSenecaAddr = modbus["senecaAddr"].as<uint8_t>();
|
||||
m_modbusFlowmeterAddr = modbus["flowmeterAddr"].as<uint8_t>();
|
||||
m_modbusTankLevelAddr = modbus["tankLevelAddr"].as<uint8_t>();
|
||||
};
|
||||
|
||||
{
|
||||
auto temperature = m_configJson["temperature"];
|
||||
m_tempExpectedSensors = temperature["expectedSensors"].as<uint8_t>();
|
||||
auto values = temperature["correctionValues"].as<JsonArray>();
|
||||
m_tempCorrectionValues.reserve(values.size());
|
||||
for (auto v : values)
|
||||
{
|
||||
m_tempCorrectionValues.push_back(v.as<float>());
|
||||
}
|
||||
};
|
||||
|
||||
{
|
||||
auto ntp = m_configJson["ntp"];
|
||||
m_ntpPool = ntp["pool"].as<std::string>();
|
||||
m_ntpTimezone = ntp["timezone"].as<uint16_t>();
|
||||
m_ntpUpdateInterval = ntp["updateInterval"].as<uint16_t>();
|
||||
m_ntpRetries = ntp["retries"].as<uint8_t>();
|
||||
};
|
||||
|
||||
{
|
||||
auto mqtt = m_configJson["mqtt"];
|
||||
m_mqttHost = mqtt["host"].as<std::string>();
|
||||
m_mqttPort = mqtt["port"].as<uint16_t>();
|
||||
m_mqttLoopTime = mqtt["loopTime"].as<uint16_t>();
|
||||
m_mqttRetries = mqtt["retries"].as<uint16_t>();
|
||||
auto subscribe = mqtt["subsribe"].as<ArduinoJson::JsonObject>();
|
||||
for (auto v : subscribe)
|
||||
{
|
||||
m_mqttSubscribe[v.key().c_str()] = v.value().as<std::string>();
|
||||
}
|
||||
auto publish = mqtt["publish"].as<ArduinoJson::JsonObject>();
|
||||
for (auto v : publish)
|
||||
{
|
||||
m_mqttPublish[v.key().c_str()] = v.value().as<std::string>();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
private:
|
||||
ArduinoJson::JsonDocument m_configJson;
|
||||
std::mutex m_mutex;
|
||||
|
||||
public:
|
||||
// Globals
|
||||
std::uint16_t m_globalLoopDelay = 1000; // in milliseconds
|
||||
|
||||
// Ethernet
|
||||
std::string m_ethHostname = "ETcontroller_PRO";
|
||||
std::string m_ethIpAddr = "10.0.2.251";
|
||||
std::string m_ethNetmask = "255.255.255.0";
|
||||
std::string m_ethGateway = "10.0.2.1";
|
||||
|
||||
// MODBUS
|
||||
uint8_t m_modbusRelayAddr = 0x01;
|
||||
uint8_t m_modbusTemperatureAddr = 0xAA;
|
||||
uint8_t m_modbusSenecaAddr = 0xBB;
|
||||
uint8_t m_modbusFlowmeterAddr = 0xCC;
|
||||
uint8_t m_modbusTankLevelAddr = 0xDD;
|
||||
|
||||
// Temperature Board
|
||||
uint8_t m_tempExpectedSensors = 1;
|
||||
std::vector<float> m_tempCorrectionValues = std::vector<float>(8, 0.0f);
|
||||
|
||||
// NTP
|
||||
std::string m_ntpPool = "pool.ntp.org";
|
||||
uint16_t m_ntpTimezone = 3600; // GTM +1
|
||||
uint16_t m_ntpUpdateInterval = 3600; // every hour
|
||||
uint8_t m_ntpRetries = 5;
|
||||
|
||||
// MQTT
|
||||
std::string m_mqttHost = "10.0.2.249";
|
||||
uint16_t m_mqttPort = 1883;
|
||||
uint16_t m_mqttLoopTime = 100; // in milliseconds
|
||||
uint8_t m_mqttRetries = 5;
|
||||
std::string m_mqttClientName = "etcontrollerPRO";
|
||||
|
||||
std::map<const std::string, std::string> m_mqttSubscribe = {
|
||||
{"commands", "test/etcontroller/commands"}};
|
||||
std::map<const std::string, std::string> m_mqttPublish = {
|
||||
{"heatpump", "test/etcontroller/heatpump"},
|
||||
{"temperature", "test/etcontroller/temperatures"},
|
||||
{"irrigation", "test/etcontroller/irrigation"}};
|
||||
};
|
||||
56
src/main.cpp
56
src/main.cpp
@@ -4,7 +4,6 @@
|
||||
#include <DebugLogEnable.h>
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <PCF85063_Driver.h>
|
||||
@@ -16,32 +15,21 @@
|
||||
|
||||
#include <digitalIO.h>
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
#include <mqtt.h>
|
||||
#include "utils.h"
|
||||
|
||||
/////////////// GLOBALS ///////////////
|
||||
Config& conf = Config::getInstance();
|
||||
Config &conf = Config::getInstance();
|
||||
/////////////// GLOBALS ///////////////
|
||||
|
||||
void callback(char *topic, uint8_t *payload, unsigned int length)
|
||||
void testAction(const ArduinoJson::JsonDocument &doc)
|
||||
{
|
||||
std::string pl;
|
||||
pl.resize(length);
|
||||
std::snprintf(pl.data(), length, "%s", payload);
|
||||
LOG_INFO("Message: Topic [", topic, "], Payload [", pl.c_str(), "]");
|
||||
std::string message;
|
||||
ArduinoJson::serializeJsonPretty(doc, message);
|
||||
LOG_INFO("Received on testAction\n", message.c_str());
|
||||
}
|
||||
|
||||
void myTask(void *mqtt)
|
||||
{
|
||||
auto client = (PubSubClient *)(mqtt);
|
||||
while (client->connected())
|
||||
{
|
||||
client->loop();
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
LOG_ERROR("Mqtt Loop Ended, client disconnected");
|
||||
vTaskDelete(NULL); // delete the current task
|
||||
};
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
@@ -75,15 +63,11 @@ void loop()
|
||||
LOG_INFO("Temperature sensors connected ->", sensors);
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
// MQTT Test //
|
||||
NetworkClient tcp;
|
||||
PubSubClient mqtt(tcp);
|
||||
mqtt.setServer(conf.m_mqttHost.c_str(), conf.m_mqttPort);
|
||||
mqtt.setCallback(callback);
|
||||
auto mqtt = MQTTwrapper();
|
||||
//////////////// NETWORK ////////////////
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
/////////////// CALLBACK ////////////////
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
/////////////// CALLBACK ////////////////
|
||||
Network.onEvent(
|
||||
[ð, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void
|
||||
{
|
||||
@@ -98,7 +82,7 @@ void loop()
|
||||
{
|
||||
if (eth.getNtpTime(ntpTime) && rtc.setDatetime(drivers::PCF85063::fromEpoch(ntpTime)))
|
||||
{
|
||||
buzzer.beep(250, NOTE_F);
|
||||
//buzzer.beep(250, NOTE_F);
|
||||
led.setColor({255, 255, 0});
|
||||
const drivers::PCF85063::datetime_t dt(drivers::PCF85063::fromEpoch(ntpTime));
|
||||
LOG_INFO("NTP Time: ", drivers::PCF85063::datetime2str(dt).c_str());
|
||||
@@ -108,10 +92,9 @@ void loop()
|
||||
}
|
||||
while (mqttRetries++ < conf.m_mqttRetries)
|
||||
{
|
||||
if (!mqtt.connected() && mqtt.connect(conf.m_mqttClientName.c_str()))
|
||||
if (mqtt.connect())
|
||||
{
|
||||
mqtt.subscribe("test/esp32-in");
|
||||
xTaskCreatePinnedToCore(myTask, "mqttLoop", 4096, &mqtt, 2, NULL, 1);
|
||||
mqtt.subscribe("test/esp32-in", testAction);
|
||||
break;
|
||||
}
|
||||
delay(100);
|
||||
@@ -128,7 +111,10 @@ void loop()
|
||||
|
||||
const std::string timeStr(rtc.getTimeStr());
|
||||
LOG_INFO("Current Datetime", timeStr.c_str());
|
||||
mqtt.publish("test/esp32-out", ("[" + std::to_string(k) + "] -> " + timeStr).c_str());
|
||||
ArduinoJson::JsonDocument ts;
|
||||
ts["loopIterator"] = k;
|
||||
ts["currentTime"] = timeStr;
|
||||
mqtt.publish("test/esp32-out", ts);
|
||||
|
||||
uint8_t i(0);
|
||||
for (auto v : tmp.getTempAll())
|
||||
@@ -172,11 +158,13 @@ void loop()
|
||||
LOG_INFO("Buzzing -> ", buzzing ? "True" : "False");
|
||||
}
|
||||
|
||||
if(io.digitalIORead(9)) { // verde
|
||||
if (io.digitalIORead(9))
|
||||
{ // verde
|
||||
conf.resetConfig();
|
||||
}
|
||||
|
||||
if(io.digitalIORead(10)) { // giallo
|
||||
if (io.digitalIORead(10))
|
||||
{ // giallo
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
|
||||
136
src/mqtt.cpp
Normal file
136
src/mqtt.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include <mqtt.h>
|
||||
|
||||
#define STACK_DEPTH 4096
|
||||
#define PRIOTITY 1
|
||||
|
||||
MQTTwrapper::MQTTwrapper() : m_config(Config::getInstance()), m_tcp(NetworkClient()), m_client(PubSubClient(m_tcp)), m_loopHandle(NULL)
|
||||
{
|
||||
m_client.setServer(m_config.m_mqttHost.c_str(), m_config.m_mqttPort);
|
||||
m_client.setKeepAlive(15);
|
||||
getInstance(this);
|
||||
}
|
||||
|
||||
MQTTwrapper::~MQTTwrapper()
|
||||
{
|
||||
disconnect();
|
||||
}
|
||||
|
||||
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());
|
||||
return false;
|
||||
}
|
||||
LOG_INFO("MQTT client connected to", m_config.m_mqttHost.c_str());
|
||||
if (m_loopHandle == NULL)
|
||||
{
|
||||
xTaskCreate(clientLoop, "mqttLoop", STACK_DEPTH, this, PRIOTITY, &m_loopHandle);
|
||||
m_client.setCallback(MQTTwrapper::callback);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool MQTTwrapper::disconnect()
|
||||
{
|
||||
m_client.disconnect();
|
||||
if (m_loopHandle)
|
||||
{
|
||||
vTaskDelete(m_loopHandle); // immediate terminate loop
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool MQTTwrapper::subscribe(topic_t topic, action_t action)
|
||||
{
|
||||
if (m_actionMap.contains(topic))
|
||||
{
|
||||
LOG_WARN("MQTT was already subscribed to", topic.c_str());
|
||||
return true;
|
||||
}
|
||||
if (m_client.subscribe(topic.c_str()))
|
||||
{
|
||||
m_actionMap[topic] = action;
|
||||
LOG_INFO("MQTT subscribed to", topic.c_str());
|
||||
return true;
|
||||
}
|
||||
LOG_ERROR("MQTT unable to subscribe to", topic.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool MQTTwrapper::unsubscribe(topic_t topic)
|
||||
{
|
||||
if (!m_actionMap.contains(topic))
|
||||
{
|
||||
LOG_WARN("MQTT was NOT subscribed to", topic.c_str());
|
||||
return false;
|
||||
}
|
||||
if (m_client.unsubscribe(topic.c_str()))
|
||||
{
|
||||
LOG_INFO("MQTT unsubscribed to", topic.c_str());
|
||||
m_actionMap.erase(topic);
|
||||
return true;
|
||||
}
|
||||
LOG_ERROR("MQTT unable to unsubscribe to", topic.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool MQTTwrapper::publish(topic_t topic, const ArduinoJson::JsonDocument obj)
|
||||
{
|
||||
std::string message;
|
||||
if (!ArduinoJson::serializeJson(obj, message))
|
||||
{
|
||||
LOG_ERROR("MQTT failed to serialize object");
|
||||
return false;
|
||||
}
|
||||
if (m_client.publish(topic.c_str(), message.c_str()))
|
||||
{
|
||||
LOG_DEBUG("MQTT published topic [", topic.c_str(), "] - message [", message.c_str(), "]");
|
||||
return true;
|
||||
}
|
||||
LOG_ERROR("MQTT failed to publish topic [", topic.c_str(), "] - message [", message.c_str(), "]");
|
||||
return false;
|
||||
}
|
||||
|
||||
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);
|
||||
auto inst = getInstance();
|
||||
if (inst)
|
||||
{
|
||||
inst->onMessage(std::string(topic), pl);
|
||||
return;
|
||||
}
|
||||
LOG_ERROR("MQTT no client instance set");
|
||||
return;
|
||||
}
|
||||
|
||||
void MQTTwrapper::onMessage(const std::string topic, const std::string message)
|
||||
{
|
||||
ArduinoJson::JsonDocument obj;
|
||||
LOG_DEBUG("MQTT received topic [", topic.c_str(), "] - message [", message.c_str(), "]");
|
||||
if (ArduinoJson::deserializeJson(obj, message) == ArduinoJson::DeserializationError::Ok)
|
||||
{
|
||||
m_actionMap[topic](obj);
|
||||
return;
|
||||
}
|
||||
LOG_ERROR("MQTT failed to deserialize message\n", message.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
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())
|
||||
{
|
||||
client->m_client.loop();
|
||||
vTaskDelay(pdMS_TO_TICKS(loopTime));
|
||||
}
|
||||
LOG_ERROR("MQTT client loop terminated, disconnected");
|
||||
client->m_loopHandle = NULL;
|
||||
vTaskDelete(NULL); // delete the current task
|
||||
}
|
||||
58
src/mqtt.h
Normal file
58
src/mqtt.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <Network.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <mutex>
|
||||
|
||||
typedef std::string topic_t;
|
||||
typedef std::function<void(const ArduinoJson::JsonDocument &)> action_t; // the actions receive a JsonObject containing the received message
|
||||
typedef std::map<topic_t, action_t> action_map_t;
|
||||
|
||||
class MQTTwrapper
|
||||
{
|
||||
|
||||
private:
|
||||
static MQTTwrapper *getInstance(MQTTwrapper *inst = nullptr)
|
||||
{
|
||||
static std::unique_ptr<MQTTwrapper> m_instance;
|
||||
if (inst)
|
||||
m_instance.reset(inst);
|
||||
if (m_instance)
|
||||
return m_instance.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
MQTTwrapper();
|
||||
~MQTTwrapper();
|
||||
|
||||
const bool connect();
|
||||
const bool disconnect();
|
||||
|
||||
const bool subscribe(topic_t topic, action_t action);
|
||||
const bool unsubscribe(topic_t topic);
|
||||
|
||||
const bool publish(topic_t topic, const ArduinoJson::JsonDocument obj);
|
||||
|
||||
private:
|
||||
static void callback(char *topic, uint8_t *payload, unsigned int length); // C-style callback only to invoke onMessage
|
||||
void onMessage(const std::string topic, const std::string message);
|
||||
|
||||
// infinite loop to call the client loop method in a taskHandle
|
||||
static void clientLoop(void *params);
|
||||
|
||||
private:
|
||||
const Config &m_config;
|
||||
action_map_t m_actionMap;
|
||||
NetworkClient m_tcp;
|
||||
PubSubClient m_client;
|
||||
TaskHandle_t m_loopHandle;
|
||||
};
|
||||
Reference in New Issue
Block a user