Problem with using PubSubClient and WiFiClientSecure

I am trying to connect using MQTT over TLS. I have a working code ready for non-TLS version, which uses only 2 libraries: WiFi.h and PubSubClient.h. As I try to do the same, but with TLS, I changed library from WiFi.h to WiFiClientSecure.h, thus having PubSubClient.h and WiFiClientSecure.h now. I wanted to modify the earlier code by addng certs and I have succesfully managed to connect to the broker. The problem is, I did that without using publish or subscribe commands from MQTT, so basically I am connected to the broker but can neither subscribe nor publish. Also I had to comment out

//PubSubClient client();

to make it compile.

#include <WiFiClientSecure.h>
#include <PubSubClient.h>
 
const char* ssid     = "";
const char* password = "";;
const char* mqtt_broker = "";



const char* test_root_ca= \
"-----BEGIN CERTIFICATE-----\n" \

"-----END CERTIFICATE-----\n";


// You can use x.509 client certificates if you want
const char* test_client_key =  //to verify the client
"-----BEGIN PRIVATE KEY-----\n" \

"-----END PRIVATE KEY-----\n" ;

const char* test_client_cert =  //to verify the client
"-----BEGIN CERTIFICATE-----\n" \

"-----END CERTIFICATE-----\n" ;



WiFiClientSecure client;
//PubSubClient client();
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print("Connecting to ");
    Serial.println(ssid);
  }
 
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  client.setCACert(test_root_ca);
  client.setCertificate(test_client_cert); // for client verification
  client.setPrivateKey(test_client_key);  // for client verification
  while (!client.connected()) 
  {
    Serial.println("Connecting to the broker...");
 
    if (client.connect(mqtt_broker,8883)) 
    {
      Serial.println("Connected");
    }
    else 
    {
      Serial.print("Failed with state ");
      delay(2000);
    }
  }
 for(int i=0; i<=1000; i++)
 {
  char payload[10];
  itoa(i, payload, 10);
  //client.publish("DemoTopic",payload);
 }
}

void loop() {
  // do nothing
}

Any idea how to work it out? Any other libraries that will help me with a setup? The error I get is:

'class WiFiClientSecure' has no member named 'publish'

I edited your sketch a bit. Should work now :wink:

#include <WiFiClientSecure.h>
#include <PubSubClient.h>

const char* ssid     = "";
const char* password = "";;
const char* mqtt_server = "";



const char* test_root_ca = \
                           "-----BEGIN CERTIFICATE-----\n" \

                           "-----END CERTIFICATE-----\n";


// You can use x.509 client certificates if you want
const char* test_client_key =  //to verify the client
  "-----BEGIN PRIVATE KEY-----\n" \

  "-----END PRIVATE KEY-----\n" ;

const char* test_client_cert =  //to verify the client
  "-----BEGIN CERTIFICATE-----\n" \

  "-----END CERTIFICATE-----\n" ;



WiFiClientSecure  espClient;
PubSubClient client(espClient);

void callback(char* topic, byte* message, unsigned int length);
void reconnect();

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print("Connecting to ");
    Serial.println(ssid);
  }

  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  espClient.setCACert(test_root_ca);
  espClient.setCertificate(test_client_cert); // for client verification
  espClient.setPrivateKey(test_client_key);  // for client verification
  while (!client.connected())
    client.setServer(mqtt_server, 8883);
  client.setCallback(callback);
  if (!client.connected()) {
    reconnect();
  }
  for (int i = 0; i <= 1000; i++)
  {
    yield();
    client.loop(); //ESP32 will be able to write received datas to UART monitor
    char payload[10];
    itoa(i, payload, 10);
    client.publish("DemoTopic", payload);
    client.subscribe("DemoTopic");
  }
}

void loop() {
}

void callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;

  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP32Client")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

Forgot to write, but within 20 min of posting my question I solved it on my own, but not in a pretty manner like u.