diff --git a/Config.h b/Config.h index 4e3be3c..f90b06b 100644 --- a/Config.h +++ b/Config.h @@ -1,6 +1,6 @@ #pragma once -#define NUKI_HUB_VERSION "8.12-pre-1" +#define NUKI_HUB_VERSION "8.12-pre-8" #define MQTT_QOS_LEVEL 1 #define MQTT_CLEAN_SESSIONS false \ No newline at end of file diff --git a/Network.cpp b/Network.cpp index 480867e..5674cb2 100644 --- a/Network.cpp +++ b/Network.cpp @@ -357,7 +357,8 @@ bool Network::reconnect() while(!_connectReplyReceived && millis() < timeout) { - delay(200); + delay(50); + _device->update(); if(_keepAliveCallback != nullptr) { _keepAliveCallback(); diff --git a/lib/espMqttClient/src/MqttClient.h b/lib/espMqttClient/src/MqttClient.h index db7b254..1d8f906 100644 --- a/lib/espMqttClient/src/MqttClient.h +++ b/lib/espMqttClient/src/MqttClient.h @@ -66,10 +66,10 @@ class MqttClient { void clearQueue(bool all = false); // Not MQTT compliant and may cause unpredictable results when `all` = 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; diff --git a/lib/espMqttClient/src/espMqttClientAsync.cpp b/lib/espMqttClient/src/espMqttClientAsync.cpp index bc53878..156572c 100644 --- a/lib/espMqttClient/src/espMqttClientAsync.cpp +++ b/lib/espMqttClient/src/espMqttClientAsync.cpp @@ -66,3 +66,69 @@ void espMqttClientAsync::onPollCb(void* a, AsyncClient* c) { } #endif + + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP32) +espMqttClientSecureAsync::espMqttClientSecureAsync(uint8_t priority, uint8_t core) + : MqttClientSetup(false, priority, core) + , _client() { +#else + espMqttClientSecure::espMqttClientSecure() +: _client() { +#endif + _transport = &_client; +} + +espMqttClientSecureAsync& espMqttClientSecureAsync::setInsecure() { + _client.client.setInsecure(); + return *this; +} + +#if defined(ARDUINO_ARCH_ESP32) +espMqttClientSecureAsync& espMqttClientSecureAsync::setCACert(const char* rootCA) { + _client.client.setCACert(rootCA); + return *this; +} + +espMqttClientSecureAsync& espMqttClientSecureAsync::setCertificate(const char* clientCa) { + _client.client.setCertificate(clientCa); + return *this; +} + +espMqttClientSecureAsync& espMqttClientSecureAsync::setPrivateKey(const char* privateKey) { + _client.client.setPrivateKey(privateKey); + return *this; +} + +espMqttClientSecureAsync& espMqttClientSecureAsync::setPreSharedKey(const char* pskIdent, const char* psKey) { + _client.client.setPreSharedKey(pskIdent, psKey); + return *this; +} +#elif defined(ARDUINO_ARCH_ESP8266) +espMqttClientSecureAsync& espMqttClientSecureAsync::setFingerprint(const uint8_t fingerprint[20]) { + _client.client.setFingerprint(fingerprint); + return *this; +} + +espMqttClientSecureAsync& espMqttClientSecureAsync::setTrustAnchors(const X509List *ta) { + _client.client.setTrustAnchors(ta); + return *this; +} + +espMqttClientSecureAsync& espMqttClientSecureAsync::setClientRSACert(const X509List *cert, const PrivateKey *sk) { + _client.client.setClientRSACert(cert, sk); + return *this; +} + +espMqttClientSecureAsync& espMqttClientSecureAsync::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; +} + +espMqttClientSecureAsync& espMqttClientSecureAsync::setCertStore(CertStoreBase *certStore) { + _client.client.setCertStore(certStore); + return *this; +} +#endif +#endif \ No newline at end of file diff --git a/lib/espMqttClient/src/espMqttClientAsync.h b/lib/espMqttClient/src/espMqttClientAsync.h index f3f321d..3493989 100644 --- a/lib/espMqttClient/src/espMqttClientAsync.h +++ b/lib/espMqttClient/src/espMqttClientAsync.h @@ -16,6 +16,7 @@ the LICENSE file. #include "Transport/ClientAsync.h" #include "MqttClientSetup.h" +#include "Transport/ClientSecureSync.h" class espMqttClientAsync : public MqttClientSetup { public: @@ -38,3 +39,32 @@ class espMqttClientAsync : public MqttClientSetup { }; #endif + + +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +class espMqttClientSecureAsync : public MqttClientSetup { +public: +#if defined(ARDUINO_ARCH_ESP32) + explicit espMqttClientSecureAsync(uint8_t priority = 1, uint8_t core = 1); +#else + espMqttClientSecure(); +#endif + espMqttClientSecureAsync& setInsecure(); +#if defined(ARDUINO_ARCH_ESP32) + espMqttClientSecureAsync& setCACert(const char* rootCA); + espMqttClientSecureAsync& setCertificate(const char* clientCa); + espMqttClientSecureAsync& setPrivateKey(const char* privateKey); + espMqttClientSecureAsync& 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 + +protected: + espMqttClientInternals::ClientSecureSync _client; +}; + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index a1a15c4..2a955a5 100644 --- a/main.cpp +++ b/main.cpp @@ -51,7 +51,7 @@ void networkTask(void *pvParameters) restartEsp(RestartReason::RestartTimer); } - delay(200); + delay(100); // Serial.println(uxTaskGetStackHighWaterMark(NULL)); } diff --git a/networkDevices/EthLan8720Device.cpp b/networkDevices/EthLan8720Device.cpp index 991599d..83aa9a5 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 espMqttClientSecure(); + _mqttClientSecure = new espMqttClientSecureAsync(); _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 espMqttClient(); + _mqttClient = new espMqttClientAsync(); } if(_preferences->getBool(preference_mqtt_log_enabled)) @@ -110,7 +110,14 @@ ReconnectStatus EthLan8720Device::reconnect() void EthLan8720Device::update() { - + if(_useEncryption) + { + _mqttClientSecure->loop(); + } + else + { + _mqttClient->loop(); + } } void EthLan8720Device::onDisconnected() diff --git a/networkDevices/EthLan8720Device.h b/networkDevices/EthLan8720Device.h index ad60793..a74af82 100644 --- a/networkDevices/EthLan8720Device.h +++ b/networkDevices/EthLan8720Device.h @@ -4,7 +4,7 @@ #include #include #include "NetworkDevice.h" -#include "espMqttClient.h" +#include "espMqttClientAsync.h" class EthLan8720Device : public NetworkDevice { @@ -54,8 +54,8 @@ public: private: void onDisconnected(); - espMqttClient* _mqttClient = nullptr; - espMqttClientSecure* _mqttClientSecure = nullptr; + espMqttClientAsync* _mqttClient = nullptr; + espMqttClientSecureAsync* _mqttClientSecure = nullptr; bool _restartOnDisconnect = false; bool _startAp = false; diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index c8fb38b..93c3343 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -197,6 +197,7 @@ void W5500Device::initializeMacAddress(byte *mac) void W5500Device::update() { _maintainResult = Ethernet.maintain(); + _mqttClient.loop(); } int8_t W5500Device::signalStrength() diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 1ccaf57..92188f1 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 espMqttClientSecure(); + _mqttClientSecure = new espMqttClientSecureAsync(); _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 espMqttClient(); + _mqttClient = new espMqttClientAsync(); } if(_preferences->getBool(preference_mqtt_log_enabled)) @@ -142,7 +142,14 @@ ReconnectStatus WifiDevice::reconnect() void WifiDevice::update() { - + if(_useEncryption) + { + _mqttClientSecure->loop(); + } + else + { + _mqttClient->loop(); + } } void WifiDevice::onDisconnected() diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 8d972cb..52a5c64 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -5,7 +5,7 @@ #include #include "NetworkDevice.h" #include "WiFiManager.h" -#include "espMqttClient.h" +#include "espMqttClientAsync.h" class WifiDevice : public NetworkDevice { @@ -58,8 +58,8 @@ private: void onDisconnected(); WiFiManager _wm; - espMqttClient* _mqttClient = nullptr; - espMqttClientSecure* _mqttClientSecure = nullptr; + espMqttClientAsync* _mqttClient = nullptr; + espMqttClientSecureAsync* _mqttClientSecure = nullptr; bool _restartOnDisconnect = false; bool _startAp = false; diff --git a/networkDevices/espMqttClientW5500.cpp b/networkDevices/espMqttClientW5500.cpp index 445cb71..eeb7e0b 100644 --- a/networkDevices/espMqttClientW5500.cpp +++ b/networkDevices/espMqttClientW5500.cpp @@ -1,7 +1,7 @@ #include "espMqttClientW5500.h" espMqttClientW5500::espMqttClientW5500(uint8_t priority, uint8_t core) -: MqttClientSetup(true, priority, core), +: MqttClientSetup(false, priority, core), _client() { _transport = &_client;