Sending mqtt data in SSL

Hello to all,
I am trying to send mqtt data from a different wifi point than the one where my home asistant server is located.
I managed to do it a while ago without SSl (no security).
Which is very dangerous nowadays, so I am trying to do it by certificates.

I have the router port 443 open and I use for the HTTPS of the home assitant 8123.

In principle I have configured the mqtt plugin for 443 and localhost, but it doesn’t let me activate the auto authentication.
So if it doesn’t connect to the arduino I think it has to do with the certificates.
I leave you the arduino code, and if someone can give me the steps to follow I would appreciate it.

Arduino code:

#include <esp_crt_bundle.h>
#include <ssl_client.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// WiFi network name and password:
const char* ssid = "xxxxxx";
const char* password = "xxxxxx";
// MQTT Broker settings:
const char* mqtt_server = "xxxx.duckdns.org";
const int mqtt_port = 443;
const char* mqtt_user = "mqtt";
const char* mqtt_password = "xxxxxx";

// Certificado CA raíz en formato PEM:
const char* root_ca= \
"-----BEGIN CERTIFICATE-----\n" \

"-----END CERTIFICATE-----";
// Configuración del sensor DHT11
#define DHTPIN 33 // Pin donde está conectado el DHT11
#define DHTTYPE DHT11 // Tipo de DHT
DHT dht(DHTPIN, DHTTYPE);
WiFiClientSecure espClient;
PubSubClient client(espClient);
void setup() {
  Serial.begin(115200);
  dht.begin();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi conectado");
  // Configurar el cliente MQTT
  espClient.setCACert(root_ca); // Establecer el certificado CA raíz
  client.setServer(mqtt_server, mqtt_port);
  client.setClient(espClient);
  client.setCallback(mqtt_callback);
}
void loop() {
  if (!client.connected()) {
    mqtt_reconnect();
  }
  client.loop();

  // Leer datos del DHT11
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Verificar si alguna lectura falló y salir temprano (para intentar de nuevo).
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("¡Fallo al leer del DHT!");
    return;
  }
  // Crear el objeto JSON para los valores de temperatura y humedad
  StaticJsonDocument<200> jsonDoc;
  jsonDoc["temp"] = temperature;
  jsonDoc["hum"] = humidity;
  char jsonBuffer[512];
  serializeJson(jsonDoc, jsonBuffer); // Serializar el objeto JSON a una cadena
  // Publicar los valores al broker MQTT
  client.publish("esp32iotsensor/CustomSensor", jsonBuffer);
}
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
  // Manejar mensaje recibido
}
void mqtt_reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Intentando conexión MQTT...");
    // Attempt to connect
    if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
      Serial.println("conectado");
      // Subscribe or publish here
    } else {
      Serial.print("falló con estado ");
      Serial.print(client.state());
      delay(5000);
    }
  }
}


Captura de pantalla 2024-03-13 144919