upadte esp mqtt client
This commit is contained in:
@@ -25,7 +25,7 @@ EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* _prefere
|
||||
{
|
||||
Log->println(F("MQTT over TLS."));
|
||||
Log->println(_ca);
|
||||
_mqttClientSecure = new espMqttClientSecureAsync();
|
||||
_mqttClientSecure = new espMqttClientWifiSecure();
|
||||
_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 espMqttClientAsync();
|
||||
_mqttClient = new espMqttClientWifi();
|
||||
}
|
||||
|
||||
if(_preferences->getBool(preference_mqtt_log_enabled))
|
||||
@@ -112,11 +112,11 @@ void EthLan8720Device::update()
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->loop();
|
||||
_mqttClientSecure->update();
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->loop();
|
||||
_mqttClient->update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <Preferences.h>
|
||||
#include "NetworkDevice.h"
|
||||
#include "espMqttClientAsync.h"
|
||||
#include "espMqttClientWifi.h"
|
||||
|
||||
class EthLan8720Device : public NetworkDevice
|
||||
{
|
||||
@@ -54,8 +54,8 @@ public:
|
||||
private:
|
||||
void onDisconnected();
|
||||
|
||||
espMqttClientAsync* _mqttClient = nullptr;
|
||||
espMqttClientSecureAsync* _mqttClientSecure = nullptr;
|
||||
espMqttClientWifi* _mqttClient = nullptr;
|
||||
espMqttClientWifiSecure* _mqttClientSecure = nullptr;
|
||||
|
||||
bool _restartOnDisconnect = false;
|
||||
bool _startAp = false;
|
||||
|
||||
@@ -197,7 +197,7 @@ void W5500Device::initializeMacAddress(byte *mac)
|
||||
void W5500Device::update()
|
||||
{
|
||||
_maintainResult = Ethernet.maintain();
|
||||
_mqttClient.loop();
|
||||
_mqttClient.update();
|
||||
}
|
||||
|
||||
int8_t W5500Device::signalStrength()
|
||||
|
||||
@@ -25,7 +25,7 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
||||
{
|
||||
Log->println(F("MQTT over TLS."));
|
||||
Log->println(_ca);
|
||||
_mqttClientSecure = new espMqttClientSecureAsync();
|
||||
_mqttClientSecure = new espMqttClientWifiSecure();
|
||||
_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 espMqttClientAsync();
|
||||
_mqttClient = new espMqttClientWifi();
|
||||
}
|
||||
|
||||
if(_preferences->getBool(preference_mqtt_log_enabled))
|
||||
@@ -144,11 +144,11 @@ void WifiDevice::update()
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->loop();
|
||||
_mqttClientSecure->update();
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->loop();
|
||||
_mqttClient->update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <Preferences.h>
|
||||
#include "NetworkDevice.h"
|
||||
#include "WiFiManager.h"
|
||||
#include "espMqttClientAsync.h"
|
||||
#include "espMqttClientWifi.h"
|
||||
|
||||
class WifiDevice : public NetworkDevice
|
||||
{
|
||||
@@ -58,8 +58,8 @@ private:
|
||||
void onDisconnected();
|
||||
|
||||
WiFiManager _wm;
|
||||
espMqttClientAsync* _mqttClient = nullptr;
|
||||
espMqttClientSecureAsync* _mqttClientSecure = nullptr;
|
||||
espMqttClientWifi* _mqttClient = nullptr;
|
||||
espMqttClientWifiSecure* _mqttClientSecure = nullptr;
|
||||
|
||||
bool _restartOnDisconnect = false;
|
||||
bool _startAp = false;
|
||||
|
||||
@@ -6,3 +6,8 @@ espMqttClientW5500::espMqttClientW5500(uint8_t priority, uint8_t core)
|
||||
{
|
||||
_transport = &_client;
|
||||
}
|
||||
|
||||
void espMqttClientW5500::update()
|
||||
{
|
||||
loop();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ public:
|
||||
espMqttClient();
|
||||
#endif
|
||||
|
||||
void update();
|
||||
|
||||
protected:
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
espMqttClientInternals::ClientSyncW5500 _client;
|
||||
|
||||
97
networkDevices/espMqttClientWifi.cpp
Normal file
97
networkDevices/espMqttClientWifi.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
Copyright (c) 2022 Bert Melis. All rights reserved.
|
||||
|
||||
This work is licensed under the terms of the MIT license.
|
||||
For a copy, see <https://opensource.org/licenses/MIT> 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
|
||||
69
networkDevices/espMqttClientWifi.h
Normal file
69
networkDevices/espMqttClientWifi.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
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 <https://opensource.org/licenses/MIT> 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<espMqttClientWifi> {
|
||||
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<espMqttClientWifiSecure> {
|
||||
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
|
||||
Reference in New Issue
Block a user