move mqtt loop into network task

This commit is contained in:
technyon
2023-02-26 10:26:55 +01:00
parent c5bd325ed5
commit 8b647e8b8e
12 changed files with 129 additions and 17 deletions

View File

@@ -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

View File

@@ -357,7 +357,8 @@ bool Network::reconnect()
while(!_connectReplyReceived && millis() < timeout)
{
delay(200);
delay(50);
_device->update();
if(_keepAliveCallback != nullptr)
{
_keepAliveCallback();

View File

@@ -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;

View File

@@ -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

View File

@@ -16,6 +16,7 @@ the LICENSE file.
#include "Transport/ClientAsync.h"
#include "MqttClientSetup.h"
#include "Transport/ClientSecureSync.h"
class espMqttClientAsync : public MqttClientSetup<espMqttClientAsync> {
public:
@@ -38,3 +39,32 @@ class espMqttClientAsync : public MqttClientSetup<espMqttClientAsync> {
};
#endif
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
class espMqttClientSecureAsync : public MqttClientSetup<espMqttClientSecureAsync> {
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

View File

@@ -51,7 +51,7 @@ void networkTask(void *pvParameters)
restartEsp(RestartReason::RestartTimer);
}
delay(200);
delay(100);
// Serial.println(uxTaskGetStackHighWaterMark(NULL));
}

View File

@@ -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()

View File

@@ -4,7 +4,7 @@
#include <WiFiClientSecure.h>
#include <Preferences.h>
#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;

View File

@@ -197,6 +197,7 @@ void W5500Device::initializeMacAddress(byte *mac)
void W5500Device::update()
{
_maintainResult = Ethernet.maintain();
_mqttClient.loop();
}
int8_t W5500Device::signalStrength()

View File

@@ -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()

View File

@@ -5,7 +5,7 @@
#include <Preferences.h>
#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;

View File

@@ -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;