mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
fix switch to secure mqtt #2492
This commit is contained in:
@@ -34,7 +34,7 @@ void MqttSettingsService::startClient() {
|
|||||||
static bool isSecure = false;
|
static bool isSecure = false;
|
||||||
if (_mqttClient != nullptr) {
|
if (_mqttClient != nullptr) {
|
||||||
// do we need to change the client?
|
// do we need to change the client?
|
||||||
if ((isSecure && _state.enableTLS) || (!isSecure && _state.enableTLS)) {
|
if ((isSecure && _state.enableTLS) || (!isSecure && !_state.enableTLS)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
delete _mqttClient;
|
delete _mqttClient;
|
||||||
@@ -48,11 +48,9 @@ void MqttSettingsService::startClient() {
|
|||||||
} else {
|
} else {
|
||||||
_mqttClient = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
|
_mqttClient = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
|
||||||
}
|
}
|
||||||
if (_state.rootCA == "insecure") {
|
if (!_mqttClient) {
|
||||||
static_cast<espMqttClientSecure *>(_mqttClient)->setInsecure();
|
emsesp::EMSESP::logger().warning("MQTT Client alloc failed");
|
||||||
} else {
|
return;
|
||||||
String certificate = "-----BEGIN CERTIFICATE-----\n" + _state.rootCA + "\n-----END CERTIFICATE-----\n";
|
|
||||||
static_cast<espMqttClientSecure *>(_mqttClient)->setCACert(certificate.c_str());
|
|
||||||
}
|
}
|
||||||
static_cast<espMqttClientSecure *>(_mqttClient)->onConnect([this](bool sessionPresent) { onMqttConnect(sessionPresent); });
|
static_cast<espMqttClientSecure *>(_mqttClient)->onConnect([this](bool sessionPresent) { onMqttConnect(sessionPresent); });
|
||||||
static_cast<espMqttClientSecure *>(_mqttClient)->onDisconnect([this](espMqttClientTypes::DisconnectReason reason) { onMqttDisconnect(reason); });
|
static_cast<espMqttClientSecure *>(_mqttClient)->onDisconnect([this](espMqttClientTypes::DisconnectReason reason) { onMqttDisconnect(reason); });
|
||||||
@@ -185,9 +183,14 @@ bool MqttSettingsService::configureMqtt() {
|
|||||||
_reconfigureMqtt = false;
|
_reconfigureMqtt = false;
|
||||||
#ifndef TASMOTA_SDK
|
#ifndef TASMOTA_SDK
|
||||||
if (_state.enableTLS) {
|
if (_state.enableTLS) {
|
||||||
#if defined(EMSESP_DEBUG)
|
if (_state.rootCA == "insecure") {
|
||||||
|
emsesp::EMSESP::logger().debug("Start insecure MQTT");
|
||||||
|
static_cast<espMqttClientSecure *>(_mqttClient)->setInsecure();
|
||||||
|
} else {
|
||||||
emsesp::EMSESP::logger().debug("Start secure MQTT with rootCA");
|
emsesp::EMSESP::logger().debug("Start secure MQTT with rootCA");
|
||||||
#endif
|
String certificate = "-----BEGIN CERTIFICATE-----\n" + _state.rootCA + "\n-----END CERTIFICATE-----\n";
|
||||||
|
static_cast<espMqttClientSecure *>(_mqttClient)->setCACert(certificate.c_str());
|
||||||
|
}
|
||||||
static_cast<espMqttClientSecure *>(_mqttClient)->setServer(_state.host.c_str(), _state.port);
|
static_cast<espMqttClientSecure *>(_mqttClient)->setServer(_state.host.c_str(), _state.port);
|
||||||
if (_state.username.length() > 0) {
|
if (_state.username.length() > 0) {
|
||||||
static_cast<espMqttClientSecure *>(_mqttClient)
|
static_cast<espMqttClientSecure *>(_mqttClient)
|
||||||
|
|||||||
Reference in New Issue
Block a user