From aa224e8c7d91ac5eb90c090949118743d6a73129 Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 26 Feb 2023 23:04:21 +0100 Subject: [PATCH] upgrad esp mqtt lib --- CMakeLists.txt | 1 - lib/espMqttClient/docs/index.md | 13 ++- .../largepayload-esp8266.ino | 20 +++- .../examples/ota-esp8266/ota-esp8266.ino | 35 +++++-- .../examples/simple-esp32/simple-esp32.ino | 21 +++- .../simple-esp8266/simple-esp8266.ino | 20 +++- .../simpleAsync-esp32/simpleAsync-esp32.ino | 21 +++- .../simpleAsync-esp8266.ino | 21 +++- .../examples/tls-esp32/tls-esp32.ino | 29 ++++-- .../examples/tls-esp8266/tls-esp8266.ino | 20 +++- lib/espMqttClient/src/MqttClient.cpp | 18 +++- lib/espMqttClient/src/MqttClient.h | 8 +- lib/espMqttClient/src/MqttClientSetup.h | 5 + lib/espMqttClient/src/Outbox.h | 4 + lib/espMqttClient/src/espMqttClient.cpp | 8 +- lib/espMqttClient/src/espMqttClient.h | 4 +- main.cpp | 4 +- networkDevices/EthLan8720Device.cpp | 8 +- networkDevices/EthLan8720Device.h | 6 +- networkDevices/W5500Device.cpp | 2 +- networkDevices/WifiDevice.cpp | 8 +- networkDevices/WifiDevice.h | 6 +- networkDevices/espMqttClientWifi.cpp | 97 ------------------- networkDevices/espMqttClientWifi.h | 69 ------------- 24 files changed, 219 insertions(+), 229 deletions(-) delete mode 100644 networkDevices/espMqttClientWifi.cpp delete mode 100644 networkDevices/espMqttClientWifi.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dab8e13..7ee604d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,6 @@ file(GLOB SRCFILES networkDevices/EthLan8720Device.cpp networkDevices/ClientSyncW5500.cpp networkDevices/espMqttClientW5500.cpp - networkDevices/espMqttClientWifi.cpp QueryCommand.h NukiWrapper.cpp NukiOpenerWrapper.cpp diff --git a/lib/espMqttClient/docs/index.md b/lib/espMqttClient/docs/index.md index 84d1122..2335054 100644 --- a/lib/espMqttClient/docs/index.md +++ b/lib/espMqttClient/docs/index.md @@ -66,7 +66,7 @@ espMqttClientAsync() ``` Instantiate a new espMqttClient or espMqttSecure object. -On ESP32, two optional parameters are available: `espMqttClient(uint8_t priority = 1, uint8_t core = 1)`. This will change the priority of the MQTT client task and the core on which it runs (higher priority = more cpu-time). +On ESP32, three optional parameters are available: `espMqttClient(bool internalTask = true, uint8_t priority = 1, uint8_t core = 1)`. By default, espMqttclient creates its own task to manage TCP. By setting `internalTask` to false, no task will be created and you will be responsible yourself to call `espMqttClient.loop()`. `priority` changes the priority of the MQTT client task and the core on which it runs (higher priority = more cpu-time). For the asynchronous version, use `espMqttClientAsync`. @@ -150,6 +150,15 @@ Set the server. - **`host`**: Host of the server, expects a null-terminated char array (c-string) - **`port`**: Port of the server +```cpp +espMqttClient& setTimeout(uint16_t timeout) +``` + +Set the timeout for packets that need acknowledgement. Defaults to 10 seconds. +When no acknowledgement has been received from the broker after sending a packet, the client will retransmit **all** the packets in the queue. + +* **`timeout`**: Timeout in seconds + #### Options for TLS connections All common options from WiFiClientSecure to setup an encrypted connection are made available. These include: @@ -324,7 +333,7 @@ Keep in mind that this may also delete any session data and therefore is not MQT void loop() ``` -This is the worker function of the MQTT client. For ESP8266 you must call this function in the Arduino loop. For ESP32 this function is only used internally and is not available in the API. +This is the worker function of the MQTT client. For ESP8266 you must call this function in the Arduino loop. For ESP32 you have to call this function yourself **only if you have disabled the internal task** (see the constructors). ```cpp const char* getClientId() const diff --git a/lib/espMqttClient/examples/largepayload-esp8266/largepayload-esp8266.ino b/lib/espMqttClient/examples/largepayload-esp8266/largepayload-esp8266.ino index aff6fac..ce4123f 100644 --- a/lib/espMqttClient/examples/largepayload-esp8266/largepayload-esp8266.ino +++ b/lib/espMqttClient/examples/largepayload-esp8266/largepayload-esp8266.ino @@ -1,4 +1,6 @@ #include +#include + #include #define WIFI_SSID "yourSSID" @@ -10,6 +12,8 @@ WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; espMqttClient mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; size_t fetchPayload(uint8_t* dest, size_t len, size_t index) { Serial.printf("filling buffer at index %zu\n", index); @@ -33,7 +37,13 @@ void connectToWiFi() { void connectToMqtt() { Serial.println("Connecting to MQTT..."); - mqttClient.connect(); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } } void onWiFiConnect(const WiFiEventStationModeGotIP& event) { @@ -56,7 +66,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); if (WiFi.isConnected()) { - connectToMqtt(); + reconnectMqtt = true; + lastReconnect = millis(); } } @@ -85,5 +96,10 @@ void setup() { } void loop() { + static uint32_t currentMillis = millis(); + mqttClient.loop(); + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } } \ No newline at end of file diff --git a/lib/espMqttClient/examples/ota-esp8266/ota-esp8266.ino b/lib/espMqttClient/examples/ota-esp8266/ota-esp8266.ino index 5152d9b..f82b36c 100644 --- a/lib/espMqttClient/examples/ota-esp8266/ota-esp8266.ino +++ b/lib/espMqttClient/examples/ota-esp8266/ota-esp8266.ino @@ -1,5 +1,7 @@ #include #include +#include + #include #define WIFI_SSID "yourSSID" @@ -13,6 +15,8 @@ WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; espMqttClient mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; bool disconnectFlag = false; bool restartFlag = false; @@ -23,7 +27,13 @@ void connectToWiFi() { void connectToMqtt() { Serial.println("Connecting to MQTT..."); - mqttClient.connect(); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } } void onWiFiConnect(const WiFiEventStationModeGotIP& event) { @@ -53,7 +63,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { } if (WiFi.isConnected()) { - connectToMqtt(); + reconnectMqtt = true; + lastReconnect = millis(); } } @@ -125,16 +136,22 @@ void setup() { } void loop() { - mqttClient.loop(); - - if (disconnectFlag) { - // it's safe to call this multiple times - mqttClient.disconnect(); - } - if (restartFlag) { Serial.println("Rebooting... See you next time!"); Serial.flush(); ESP.reset(); } + + static uint32_t currentMillis = millis(); + + mqttClient.loop(); + + if (!disconnectFlag && reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } + + if (disconnectFlag) { + // it's safe to call this multiple times + mqttClient.disconnect(); + } } \ No newline at end of file diff --git a/lib/espMqttClient/examples/simple-esp32/simple-esp32.ino b/lib/espMqttClient/examples/simple-esp32/simple-esp32.ino index 27dc6b9..4698587 100644 --- a/lib/espMqttClient/examples/simple-esp32/simple-esp32.ino +++ b/lib/espMqttClient/examples/simple-esp32/simple-esp32.ino @@ -1,4 +1,6 @@ #include +#include + #include #define WIFI_SSID "yourSSID" @@ -8,6 +10,8 @@ #define MQTT_PORT 1883 espMqttClient mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; void connectToWiFi() { Serial.println("Connecting to Wi-Fi..."); @@ -16,7 +20,13 @@ void connectToWiFi() { void connectToMqtt() { Serial.println("Connecting to MQTT..."); - mqttClient.connect(); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } } void WiFiEvent(WiFiEvent_t event) { @@ -57,7 +67,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); if (WiFi.isConnected()) { - connectToMqtt(); + reconnectMqtt = true; + lastReconnect = millis(); } } @@ -122,5 +133,9 @@ void setup() { } void loop() { - // nothing to do here + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } } diff --git a/lib/espMqttClient/examples/simple-esp8266/simple-esp8266.ino b/lib/espMqttClient/examples/simple-esp8266/simple-esp8266.ino index 39e725d..6eda510 100644 --- a/lib/espMqttClient/examples/simple-esp8266/simple-esp8266.ino +++ b/lib/espMqttClient/examples/simple-esp8266/simple-esp8266.ino @@ -1,4 +1,6 @@ #include +#include + #include #define WIFI_SSID "yourSSID" @@ -10,6 +12,8 @@ WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; espMqttClient mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; void connectToWiFi() { Serial.println("Connecting to Wi-Fi..."); @@ -18,7 +22,13 @@ void connectToWiFi() { void connectToMqtt() { Serial.println("Connecting to MQTT..."); - mqttClient.connect(); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } } void onWiFiConnect(const WiFiEventStationModeGotIP& event) { @@ -51,7 +61,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); if (WiFi.isConnected()) { - connectToMqtt(); + reconnectMqtt = true; + lastReconnect = millis(); } } @@ -117,5 +128,10 @@ void setup() { } void loop() { + static uint32_t currentMillis = millis(); + mqttClient.loop(); + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } } \ No newline at end of file diff --git a/lib/espMqttClient/examples/simpleAsync-esp32/simpleAsync-esp32.ino b/lib/espMqttClient/examples/simpleAsync-esp32/simpleAsync-esp32.ino index a556789..afd2445 100644 --- a/lib/espMqttClient/examples/simpleAsync-esp32/simpleAsync-esp32.ino +++ b/lib/espMqttClient/examples/simpleAsync-esp32/simpleAsync-esp32.ino @@ -1,4 +1,6 @@ #include +#include + #include #define WIFI_SSID "yourSSID" @@ -8,6 +10,8 @@ #define MQTT_PORT 1883 espMqttClientAsync mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; void connectToWiFi() { Serial.println("Connecting to Wi-Fi..."); @@ -16,7 +20,13 @@ void connectToWiFi() { void connectToMqtt() { Serial.println("Connecting to MQTT..."); - mqttClient.connect(); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } } void WiFiEvent(WiFiEvent_t event) { @@ -57,7 +67,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); if (WiFi.isConnected()) { - connectToMqtt(); + reconnectMqtt = true; + lastReconnect = millis(); } } @@ -122,5 +133,9 @@ void setup() { } void loop() { - // nothing to do here + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } } diff --git a/lib/espMqttClient/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino b/lib/espMqttClient/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino index 76c20a9..08c88b1 100644 --- a/lib/espMqttClient/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino +++ b/lib/espMqttClient/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino @@ -1,4 +1,6 @@ #include +#include + #include #define WIFI_SSID "yourSSID" @@ -10,6 +12,8 @@ WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; espMqttClientAsync mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; void connectToWiFi() { Serial.println("Connecting to Wi-Fi..."); @@ -18,7 +22,13 @@ void connectToWiFi() { void connectToMqtt() { Serial.println("Connecting to MQTT..."); - mqttClient.connect(); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } } void onWiFiConnect(const WiFiEventStationModeGotIP& event) { @@ -51,7 +61,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); if (WiFi.isConnected()) { - connectToMqtt(); + reconnectMqtt = true; + lastReconnect = millis(); } } @@ -117,5 +128,9 @@ void setup() { } void loop() { - // nothing to do here + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } } \ No newline at end of file diff --git a/lib/espMqttClient/examples/tls-esp32/tls-esp32.ino b/lib/espMqttClient/examples/tls-esp32/tls-esp32.ino index 551d241..b759aff 100644 --- a/lib/espMqttClient/examples/tls-esp32/tls-esp32.ino +++ b/lib/espMqttClient/examples/tls-esp32/tls-esp32.ino @@ -1,4 +1,6 @@ #include +#include + #include #define WIFI_SSID "yourSSID" @@ -15,6 +17,8 @@ const char rootCA[] = \ "-----END CERTIFICATE-----\n"; espMqttClientSecure mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; void connectToWiFi() { Serial.println("Connecting to Wi-Fi..."); @@ -23,7 +27,13 @@ void connectToWiFi() { void connectToMqtt() { Serial.println("Connecting to MQTT..."); - mqttClient.connect(); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } } void WiFiEvent(WiFiEvent_t event) { @@ -61,7 +71,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); if (WiFi.isConnected()) { - connectToMqtt(); + reconnectMqtt = true; + lastReconnect = millis(); } } @@ -130,15 +141,21 @@ void setup() { } void loop() { + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } + static uint32_t lastMillis = 0; - if (millis() - lastMillis > 5000) { - lastMillis = millis(); + if (currentMillis - lastMillis > 5000) { + lastMillis = currentMillis; Serial.printf("heap: %u\n", ESP.getFreeHeap()); } static uint32_t millisDisconnect = 0; - if (millis() - millisDisconnect > 60000) { - millisDisconnect = millis(); + if (currentMillis - millisDisconnect > 60000) { + millisDisconnect = currentMillis; mqttClient.disconnect(); } } diff --git a/lib/espMqttClient/examples/tls-esp8266/tls-esp8266.ino b/lib/espMqttClient/examples/tls-esp8266/tls-esp8266.ino index b196a8c..5be5b11 100644 --- a/lib/espMqttClient/examples/tls-esp8266/tls-esp8266.ino +++ b/lib/espMqttClient/examples/tls-esp8266/tls-esp8266.ino @@ -1,4 +1,6 @@ #include +#include + #include #define WIFI_SSID "yourSSID" @@ -13,6 +15,8 @@ const uint8_t fingerprint[] = {0xee, 0xbc, 0x4b, 0xf8, 0x57, 0xe3, 0xd3, 0xe4, 0 WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; espMqttClientSecure mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; void connectToWiFi() { Serial.println("Connecting to Wi-Fi..."); @@ -21,7 +25,13 @@ void connectToWiFi() { void connectToMqtt() { Serial.println("Connecting to MQTT..."); - mqttClient.connect(); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } } void onWiFiConnect(const WiFiEventStationModeGotIP& event) { @@ -54,7 +64,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { Serial.printf("Disconnected from MQTT: %u.\n", static_cast(reason)); if (WiFi.isConnected()) { - connectToMqtt(); + reconnectMqtt = true; + lastReconnect = millis(); } } @@ -121,5 +132,10 @@ void setup() { } void loop() { + static uint32_t currentMillis = millis(); + mqttClient.loop(); + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + connectToMqtt(); + } } \ No newline at end of file diff --git a/lib/espMqttClient/src/MqttClient.cpp b/lib/espMqttClient/src/MqttClient.cpp index 9868c27..af7f1f9 100644 --- a/lib/espMqttClient/src/MqttClient.cpp +++ b/lib/espMqttClient/src/MqttClient.cpp @@ -42,6 +42,7 @@ MqttClient::MqttClient() , _willPayloadLength(0) , _willQos(0) , _willRetain(false) +, _timeout(10000) , _state(State::disconnected) , _generatedClientId{0} , _packetId(0) @@ -373,6 +374,9 @@ int MqttClient::_sendPacket() { EMC_SEMAPHORE_GIVE(); return -1; } + // handle with care! millis() returns unsigned 32 bit, token is void* + static_assert(sizeof(uint32_t) <= sizeof(void*), "the size of uint32_t must be smaller than or equal to the size of a pointer"); + packet->token = reinterpret_cast(millis()); _lastClientActivity = millis(); _bytesSent += written; emc_log_i("tx %zu/%zu (%02x)", _bytesSent, packet->size(), packet->packetType()); @@ -392,8 +396,7 @@ bool MqttClient::_advanceOutbox() { if (packet->removable()) { _outbox.removeCurrent(); } else { - // handle with care! millis() returns unsigned 32 bit, token is void* - packet->token = reinterpret_cast(millis()); + // we already set 'dup' here, in case we have to retry if ((packet->packetType()) == PacketType.PUBLISH) packet->setDup(); _outbox.next(); } @@ -496,6 +499,17 @@ void MqttClient::_checkPing() { } } +void MqttClient::_checkTimeout() { + espMqttClientInternals::Outbox::Iterator it = _outbox.front(); + if (it && _bytesSent == 0) { // check that we're not busy sending + if (millis() - *((uint32_t*)&(it.get()->token)) > _timeout) { // NOLINT(readability/casting) + // TODO(bertmelis): fix ugly casting hack + emc_log_w("Packet ack timeout, retrying"); + _outbox.resetCurrent(); + } + } +} + void MqttClient::_onConnack() { if (_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode == 0x00) { _pingSent = false; // reset after keepalive timeout disconnect diff --git a/lib/espMqttClient/src/MqttClient.h b/lib/espMqttClient/src/MqttClient.h index f95b966..382997c 100644 --- a/lib/espMqttClient/src/MqttClient.h +++ b/lib/espMqttClient/src/MqttClient.h @@ -65,17 +65,13 @@ class MqttClient { uint16_t publish(const char* topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length); void clearQueue(bool deleteSessionData = false); // Not MQTT compliant and may cause unpredictable results when `deleteSessionData` = true! const char* getClientId() const; - #if defined(ARDUINO_ARCH_ESP32) + void loop(); protected: - #endif - void loop(); #if defined(ARDUINO_ARCH_ESP32) explicit MqttClient(bool useTask, uint8_t priority = 1, uint8_t core = 1); bool _useTask; #else - - protected: MqttClient(); #endif espMqttClientInternals::Transport* _transport; @@ -102,6 +98,7 @@ class MqttClient { uint16_t _willPayloadLength; uint8_t _willQos; bool _willRetain; + uint32_t _timeout; // state is protected to allow state changes by the transport system, defined in child classes // eg. to allow AsyncTCP @@ -166,6 +163,7 @@ class MqttClient { bool _advanceOutbox(); void _checkIncoming(); void _checkPing(); + void _checkTimeout(); void _onConnack(); void _onPublish(); diff --git a/lib/espMqttClient/src/MqttClientSetup.h b/lib/espMqttClient/src/MqttClientSetup.h index a96bd48..40c68af 100644 --- a/lib/espMqttClient/src/MqttClientSetup.h +++ b/lib/espMqttClient/src/MqttClientSetup.h @@ -68,6 +68,11 @@ class MqttClientSetup : public MqttClient { return static_cast(*this); } + T& setTimeout(uint16_t timeout) { + _timeout = timeout * 1000; // s to ms conversion, will also do 16 to 32 bit conversion + return static_cast(*this); + } + T& onConnect(espMqttClientTypes::OnConnectCallback callback) { _onConnectCallback = callback; return static_cast(*this); diff --git a/lib/espMqttClient/src/Outbox.h b/lib/espMqttClient/src/Outbox.h index 5e1edcf..7f0d543 100644 --- a/lib/espMqttClient/src/Outbox.h +++ b/lib/espMqttClient/src/Outbox.h @@ -138,6 +138,10 @@ class Outbox { return nullptr; } + void resetCurrent() { + _current = _first; + } + Iterator front() const { Iterator it; it._node = _first; diff --git a/lib/espMqttClient/src/espMqttClient.cpp b/lib/espMqttClient/src/espMqttClient.cpp index 34e789b..bda3038 100644 --- a/lib/espMqttClient/src/espMqttClient.cpp +++ b/lib/espMqttClient/src/espMqttClient.cpp @@ -9,8 +9,8 @@ the LICENSE file. #include "espMqttClient.h" #if defined(ARDUINO_ARCH_ESP32) -espMqttClient::espMqttClient(uint8_t priority, uint8_t core) -: MqttClientSetup(true, priority, core) +espMqttClient::espMqttClient(bool internalTask, uint8_t priority, uint8_t core) +: MqttClientSetup(internalTask, priority, core) , _client() { #else espMqttClient::espMqttClient() @@ -21,8 +21,8 @@ espMqttClient::espMqttClient() #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32) -espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core) -: MqttClientSetup(priority, core) +espMqttClientSecure::espMqttClientSecure(bool internalTask, uint8_t priority, uint8_t core) +: MqttClientSetup(internalTask, priority, core) , _client() { #else espMqttClientSecure::espMqttClientSecure() diff --git a/lib/espMqttClient/src/espMqttClient.h b/lib/espMqttClient/src/espMqttClient.h index 9ee2279..a2aba97 100644 --- a/lib/espMqttClient/src/espMqttClient.h +++ b/lib/espMqttClient/src/espMqttClient.h @@ -23,7 +23,7 @@ the LICENSE file. class espMqttClient : public MqttClientSetup { public: #if defined(ARDUINO_ARCH_ESP32) - explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1); + explicit espMqttClient(bool internalTask = true, uint8_t priority = 1, uint8_t core = 1); #else espMqttClient(); #endif @@ -40,7 +40,7 @@ class espMqttClient : public MqttClientSetup { class espMqttClientSecure : public MqttClientSetup { public: #if defined(ARDUINO_ARCH_ESP32) - explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1); + explicit espMqttClientSecure(bool internalTask = true, uint8_t priority = 1, uint8_t core = 1); #else espMqttClientSecure(); #endif diff --git a/main.cpp b/main.cpp index 39780c9..19b37eb 100644 --- a/main.cpp +++ b/main.cpp @@ -106,8 +106,8 @@ void setupTasks() // configMAX_PRIORITIES is 25 xTaskCreatePinnedToCore(networkTask, "ntw", 8192, NULL, 3, &networkTaskHandle, 1); - xTaskCreatePinnedToCore(nukiTask, "nuki", 4096, NULL, 2, &nukiTaskHandle, 1); - xTaskCreatePinnedToCore(presenceDetectionTask, "prdet", 768, NULL, 5, &presenceDetectionTaskHandle, 1); + xTaskCreatePinnedToCore(nukiTask, "nuki", 3328, NULL, 2, &nukiTaskHandle, 1); + xTaskCreatePinnedToCore(presenceDetectionTask, "prdet", 896, NULL, 5, &presenceDetectionTaskHandle, 1); } uint32_t getRandomId() diff --git a/networkDevices/EthLan8720Device.cpp b/networkDevices/EthLan8720Device.cpp index 3c5a75d..5311625 100644 --- a/networkDevices/EthLan8720Device.cpp +++ b/networkDevices/EthLan8720Device.cpp @@ -25,7 +25,7 @@ EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* _prefere { Log->println(F("MQTT over TLS.")); Log->println(_ca); - _mqttClientSecure = new espMqttClientWifiSecure(); + _mqttClientSecure = new espMqttClientSecure(); _mqttClientSecure->setCACert(_ca); if(crtLength > 1 && keyLength > 1) // length is 1 when empty { @@ -38,7 +38,7 @@ EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* _prefere } else { Log->println(F("MQTT without TLS.")); - _mqttClient = new espMqttClientWifi(); + _mqttClient = new espMqttClient(); } if(_preferences->getBool(preference_mqtt_log_enabled)) @@ -112,11 +112,11 @@ void EthLan8720Device::update() { if(_useEncryption) { - _mqttClientSecure->update(); + _mqttClientSecure->loop(); } else { - _mqttClient->update(); + _mqttClient->loop(); } } diff --git a/networkDevices/EthLan8720Device.h b/networkDevices/EthLan8720Device.h index 1d23615..ad60793 100644 --- a/networkDevices/EthLan8720Device.h +++ b/networkDevices/EthLan8720Device.h @@ -4,7 +4,7 @@ #include #include #include "NetworkDevice.h" -#include "espMqttClientWifi.h" +#include "espMqttClient.h" class EthLan8720Device : public NetworkDevice { @@ -54,8 +54,8 @@ public: private: void onDisconnected(); - espMqttClientWifi* _mqttClient = nullptr; - espMqttClientWifiSecure* _mqttClientSecure = nullptr; + espMqttClient* _mqttClient = nullptr; + espMqttClientSecure* _mqttClientSecure = nullptr; bool _restartOnDisconnect = false; bool _startAp = false; diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index db414ab..93c3343 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -197,7 +197,7 @@ void W5500Device::initializeMacAddress(byte *mac) void W5500Device::update() { _maintainResult = Ethernet.maintain(); - _mqttClient.update(); + _mqttClient.loop(); } int8_t W5500Device::signalStrength() diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 7ddf5ae..a3a04a5 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -25,7 +25,7 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences) { Log->println(F("MQTT over TLS.")); Log->println(_ca); - _mqttClientSecure = new espMqttClientWifiSecure(); + _mqttClientSecure = new espMqttClientSecure(); _mqttClientSecure->setCACert(_ca); if(crtLength > 1 && keyLength > 1) // length is 1 when empty { @@ -38,7 +38,7 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences) } else { Log->println(F("MQTT without TLS.")); - _mqttClient = new espMqttClientWifi(); + _mqttClient = new espMqttClient(); } if(_preferences->getBool(preference_mqtt_log_enabled)) @@ -144,11 +144,11 @@ void WifiDevice::update() { if(_useEncryption) { - _mqttClientSecure->update(); + _mqttClientSecure->loop(); } else { - _mqttClient->update(); + _mqttClient->loop(); } } diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 53414e2..8d972cb 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -5,7 +5,7 @@ #include #include "NetworkDevice.h" #include "WiFiManager.h" -#include "espMqttClientWifi.h" +#include "espMqttClient.h" class WifiDevice : public NetworkDevice { @@ -58,8 +58,8 @@ private: void onDisconnected(); WiFiManager _wm; - espMqttClientWifi* _mqttClient = nullptr; - espMqttClientWifiSecure* _mqttClientSecure = nullptr; + espMqttClient* _mqttClient = nullptr; + espMqttClientSecure* _mqttClientSecure = nullptr; bool _restartOnDisconnect = false; bool _startAp = false; diff --git a/networkDevices/espMqttClientWifi.cpp b/networkDevices/espMqttClientWifi.cpp deleted file mode 100644 index 4ce1c87..0000000 --- a/networkDevices/espMqttClientWifi.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* -Copyright (c) 2022 Bert Melis. All rights reserved. - -This work is licensed under the terms of the MIT license. -For a copy, see or -the LICENSE file. -*/ - -#include "espMqttClientWifi.h" - -#if defined(ARDUINO_ARCH_ESP32) -espMqttClientWifi::espMqttClientWifi(uint8_t priority, uint8_t core) - : MqttClientSetup(false, priority, core) - , _client() { -#else - espMqttClient::espMqttClient() -: _client() { -#endif - _transport = &_client; -} - -void espMqttClientWifi::update() -{ - loop(); -} - -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) -#if defined(ARDUINO_ARCH_ESP32) -espMqttClientWifiSecure::espMqttClientWifiSecure(uint8_t priority, uint8_t core) - : MqttClientSetup(false, priority, core) - , _client() { -#else - espMqttClientSecure::espMqttClientSecure() -: _client() { -#endif - _transport = &_client; -} - -espMqttClientWifiSecure& espMqttClientWifiSecure::setInsecure() { - _client.client.setInsecure(); - return *this; -} - -#if defined(ARDUINO_ARCH_ESP32) -espMqttClientWifiSecure& espMqttClientWifiSecure::setCACert(const char* rootCA) { - _client.client.setCACert(rootCA); - return *this; -} - -espMqttClientWifiSecure& espMqttClientWifiSecure::setCertificate(const char* clientCa) { - _client.client.setCertificate(clientCa); - return *this; -} - -espMqttClientWifiSecure& espMqttClientWifiSecure::setPrivateKey(const char* privateKey) { - _client.client.setPrivateKey(privateKey); - return *this; -} - -espMqttClientWifiSecure& espMqttClientWifiSecure::setPreSharedKey(const char* pskIdent, const char* psKey) { - _client.client.setPreSharedKey(pskIdent, psKey); - return *this; -} - -void espMqttClientWifiSecure::update() -{ - loop(); -} - -#elif defined(ARDUINO_ARCH_ESP8266) -espMqttClientWifiSecure& espMqttClientWifiSecure::setFingerprint(const uint8_t fingerprint[20]) { - _client.client.setFingerprint(fingerprint); - return *this; -} - -espMqttClientWifiSecure& espMqttClientWifiSecure::setTrustAnchors(const X509List *ta) { - _client.client.setTrustAnchors(ta); - return *this; -} - -espMqttClientWifiSecure& espMqttClientWifiSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) { - _client.client.setClientRSACert(cert, sk); - return *this; -} - -espMqttClientWifiSecure& espMqttClientWifiSecure::setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) { - _client.client.setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type); - return *this; -} - -espMqttClientWifiSecure& espMqttClientWifiSecure::setCertStore(CertStoreBase *certStore) { - _client.client.setCertStore(certStore); - return *this; -} -#endif - -#endif diff --git a/networkDevices/espMqttClientWifi.h b/networkDevices/espMqttClientWifi.h deleted file mode 100644 index e78527c..0000000 --- a/networkDevices/espMqttClientWifi.h +++ /dev/null @@ -1,69 +0,0 @@ -/* -Copyright (c) 2022 Bert Melis. All rights reserved. - -API is based on the original work of Marvin Roger: -https://github.com/marvinroger/async-mqtt-client - -This work is licensed under the terms of the MIT license. -For a copy, see or -the LICENSE file. -*/ - -#pragma once - -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) -#include "Transport/ClientSync.h" -#include "Transport/ClientSecureSync.h" -#elif defined(__linux__) -#include "Transport/ClientPosix.h" -#endif - -#include "MqttClientSetup.h" - -class espMqttClientWifi : public MqttClientSetup { -public: -#if defined(ARDUINO_ARCH_ESP32) - explicit espMqttClientWifi(uint8_t priority = 1, uint8_t core = 1); -#else - espMqttClient(); -#endif - - void update(); - -protected: -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) - espMqttClientInternals::ClientSync _client; -#elif defined(__linux__) - espMqttClientInternals::ClientPosix _client; -#endif -}; - -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) -class espMqttClientWifiSecure : public MqttClientSetup { -public: -#if defined(ARDUINO_ARCH_ESP32) - explicit espMqttClientWifiSecure(uint8_t priority = 1, uint8_t core = 1); -#else - espMqttClientSecure(); -#endif - espMqttClientWifiSecure& setInsecure(); -#if defined(ARDUINO_ARCH_ESP32) - espMqttClientWifiSecure& setCACert(const char* rootCA); - espMqttClientWifiSecure& setCertificate(const char* clientCa); - espMqttClientWifiSecure& setPrivateKey(const char* privateKey); - espMqttClientWifiSecure& setPreSharedKey(const char* pskIdent, const char* psKey); -#else - espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]); - espMqttClientSecure& setTrustAnchors(const X509List *ta); - espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk); - espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type); - espMqttClientSecure& setCertStore(CertStoreBase *certStore); -#endif - - void update(); - -protected: - espMqttClientInternals::ClientSecureSync _client; -}; - -#endif