Unable to connect to MQTT broker using MKR Wifi 1010

Hi Arduino community,

Below is the code used to connect to MQTT broker but it behaves strangely. If I comment MKRCarrier library calls then only it will connect, if I uncomment then MQTT broker connection will not be established. Please help if any of you faced similar issue.

MQTT library used - ArduinoMqttClient version - 0.1.5
WiFi library used - WiFiNINA

Full code:

#include "arduino_secrets.h"
#include <ArduinoBearSSL.h>
#include <ArduinoECCX08.h>
#include <Arduino_JSON.h>
#include <WiFiNINA.h> // change to #include <WiFi101.h> for MKR1000
#include <ArduinoMqttClient.h>

#include <Arduino_MKRIoTCarrier.h>
MKRIoTCarrier carrier;

/////// Enter your sensitive data in arduino_secrets.h
const char ssid = SECRET_SSID;
const char pass = SECRET_PASS;
const char broker = SECRET_BROKER;
const char* certificate = SECRET_CERTIFICATE;
const char* key = SECRET_KEY;
int status = WL_IDLE_STATUS;

unsigned long lastMillis = 0;
WiFiClient wifiClient; // Used for the TCP socket connection
BearSSLClient sslClient(wifiClient); // Used for SSL/TLS connection, integrates with ECC508
MqttClient mqttClient(sslClient);

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if (!ECCX08.begin()) {
Serial.println("No ECCX08 present!");
while (1);
}
ArduinoBearSSL.onGetTime(getTime);

//for(int i=0; i<100; i++)
// buf[i] = 0x1;

sslClient.setKey(key, certificate);

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);

}
//connectWiFi();
Serial.println("Connected to wifi");

mqttClient.setId("jay_test_device");
mqttClient.onMessage(onMessageReceived);

while (!mqttClient.connected()) {
// MQTT client is disconnected, connect
connectMQTT();
}
Serial.print("Init the carrier\n");
//CARRIER_CASE = false;
//carrier.begin();
//carrier.display.setRotation(0);
delay(1500);

}

unsigned long getTime() {
// get the current time from the WiFi module
return WiFi.getTime();
}

void loop() {

if (!mqttClient.connected()) {
// MQTT client is disconnected, connect
Serial.print("Disconnected and connecting MQTT broker\n");
connectMQTT();
}

// poll for new MQTT messages and send keep alives
mqttClient.poll();

// publish a message roughly every 5 seconds.
if (millis() - lastMillis > 5000) {
lastMillis = millis();

publishMessage();

}

}

void connectMQTT() {
Serial.print("Attempting to MQTT broker: ");
Serial.print(broker);
Serial.println(" ");

while (!mqttClient.connect(broker, 8883)) {
// failed, retry
//Serial.print("Secret key: ");
//Serial.print(key);
Serial.print(".");
delay(5000);
}
Serial.println();

Serial.println("You're connected to the MQTT broker");
Serial.println();

// subscribe to a topic
// Pass Alternate ID
mqttClient.subscribe("ack/jay_test_device");
mqttClient.subscribe("commands/jay_test_device");
}

void publishMessage() {

delay(100);

JSONVar message;

message["sensorAlternateId"] = "jay_test_device_sensor";
message["capabilityAlternateId"] = "shreekant-capability";

JSONVar payload;
payload["ambientTemperature"] = 10;
payload["ambientHumidity"] = 10;
payload["ambientLight"] = 10;
payload["soilMoisture"] = 10;
message["measures"][0] = payload;
//message["dummy_buf"] = buf[0];

// send message, the Print interface can be used to set the message contents
// Pass the alternate id
mqttClient.beginMessage("measures/jay_test_device");
mqttClient.print(message);
mqttClient.endMessage();

}

void onMessageReceived(int messageSize) {
// we received a message, print out the topic and contents
Serial.print("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");

String topic = (String)mqttClient.messageTopic();
Serial.print(topic);
// if (Contains(topic,"commands")) {
//
// }

// use the Stream interface to print the contents
while (mqttClient.available()) {

Serial.print((char)mqttClient.read());     

}
Serial.println();
Serial.println();
}

void connectWiFi() {
Serial.print("Attempting to connect to SSID: ");
Serial.print(ssid);
Serial.print(" ");

while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println();

Serial.println("You're connected to the network");
Serial.println();
}

MKR IOT Carrier calls to be commented

//CARRIER_CASE = false;
//carrier.begin();
//carrier.display.setRotation(0);