diff --git a/lib/RS485/RS485_Driver.h b/lib/RS485/RS485_Driver.h index ff410d5..d3d968e 100644 --- a/lib/RS485/RS485_Driver.h +++ b/lib/RS485/RS485_Driver.h @@ -17,6 +17,9 @@ namespace drivers public: RS485(const uint32_t baud, const SerialConfig conf); + RS485(const RS485 &) = delete; // remove copy constructors + RS485 &operator=(const RS485 &) = delete; + const bool write(const std::vector data); const bool readAll(std::vector &data); const bool readN(const uint16_t nBytes, std::vector &data); @@ -54,6 +57,9 @@ namespace drivers public: MODBUS(const uint32_t baud, const SerialConfig conf); + MODBUS(const MODBUS &) = delete; // remove copy constructors + MODBUS &operator=(const MODBUS &) = delete; + // Func 0x01 const bool readCoils(const uint8_t device, const uint16_t reg, const uint16_t num, std::vector &coils); diff --git a/src/main.cpp b/src/main.cpp index b8e03f8..a6edfe3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,12 +34,11 @@ void setup() { Serial.begin(9600); LOG_ATTACH_SERIAL(Serial); - conf.init(); + conf.init(); // read the configuration from internal flash } void loop() { - const uint8_t baseRegister(0x00); uint16_t k(0); uint8_t sensors(0); bool buzzing(false); @@ -66,10 +65,17 @@ void loop() auto mqtt = MQTTwrapper(); //////////////// NETWORK //////////////// + std::function mycallback = + [&io](const ArduinoJson::JsonDocument &doc) { + io.digitalIOWrite(0, doc["stat"].as()); + io.digitalIOWrite(15, doc["stat"].as()); + }; + + //////////////// NETWORK //////////////// /////////////// CALLBACK //////////////// Network.onEvent( - [ð, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void + [ð, &rtc, &mqtt, &buzzer, &led, &mycallback](arduino_event_id_t event, arduino_event_info_t info) -> void { eth.onEvent(event, info); // Arduino Ethernet event handler if (!eth.isConnected()) @@ -95,6 +101,7 @@ void loop() if (mqtt.connect()) { mqtt.subscribe("test/esp32-in", testAction); + mqtt.subscribe("test/esp32-functional", mycallback); break; } delay(100); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 38f9de1..dd0e191 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -1,7 +1,7 @@ #include #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; diff --git a/src/mqtt.h b/src/mqtt.h index 793333c..c19491a 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -11,6 +11,7 @@ #include #include +#include typedef std::string topic_t; typedef std::function action_t; // the actions receive a JsonObject containing the received message