MKR 1010 wifi - Enable to connect to 2 different mqtt brokers

Hi
I try to connect two different mqtt brokers

MqttClient    mqttClient_1(sslClient);
MqttClient    mqttClient_2(sslClient);


void loop() {
//broker 1

if (!mqttClient_1.connected()) {
    // MQTT client 1 is disconnected, connect
    connectMQTT_1);
  }
  mqttClient_1.poll();
  publishMessage_1();
  
  delay (5000);

  //broker 2
  
  if (!mqttClient_2.connected()) {
    // MQTT client is disconnected, connect
    connectMQTT_2();
  }
 mqttClient_2.poll();
 publishMessage_2();
    
  delay(5000);
}

connectMQTT function and publishMessage function are let me say "standard" functions taken from Arduino example.

void connectMQTT_1() {
  
  while (!mqttClient_1.connect(broker_1, 8883)) {
    // failed, retry
      
    Serial.print(".");
    Serial.println(mqttClient_1.connectError());
    delay(5000);
  }
  Serial.println();

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

  // subscribe to a topic
  mqttClient_1.subscribe("devices/" + deviceId + "/messages/devicebound/#");
}

connectMQTT_2() works with broker2 and another topic

What happend is that (mqttClient_1.connected) in loop function is FALSE after publishMessage_2 and viceversa.

So every time mqttclient attempt to reconnect
It seems that when I publish to Broker2 I lose my connection to Broker 1
and vice versa

What's wrong?

PublishMessage is "standard"

void publishMessage_1() {
  StaticJsonDocument<300> doc;
  Serial.println("Publishing message");
 
   float temperature = ENV.readTemperature() + TempCalibration;
  float humidity    = ENV.readHumidity();
  float pressure    = ENV.readPressure();
  float illuminance = ENV.readIlluminance();
  
  // send message, the Print interface can be used to set the message contents
  mqttClient_1.beginMessage("devices/" + deviceId + "/messages/events/");
  doc["deviceId"] = deviceId;
  doc["timestamp"] = getTime();
  doc["temp"] = temperature;
  doc["humidity"] = humidity;
  doc["pressure"] = pressure;
  doc["illuminance"] = illuminance;
  Serial.print ("Azure IOT HUB :");
  serializeJson(doc,Serial);
  serializeJson(doc, mqttClient_1);
  mqttClient_1.endMessage();
  Serial.println ("OK _IOT");
}

Publish Message for broker 2, works with mqqtClient_2

maybe you need sslClient1 and sslCiient2

Just to try this is not the way:

BearSSLClient sslClient(wifiClient); // Used for SSL/TLS connection, integrates with ECC508
BearSSLClient sslClient2(wifiClient); // Used for SSL/TLS connection, integrates with ECC508
..../arm-none-eabi-gcc/7-2017q4/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe: ....\Temp\arduino-sketch-FED15C25119FDC95ADC98660FB33517D/da Test.ino.elf section `.bss' will not fit in region `RAM'
.../arm-none-eabi-gcc/7-2017q4/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe: region RAM overflowed with stack
.../arm-none-eabi-gcc/7-2017q4/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe: region `RAM' overflowed by 4756 bytes

and wifiClient1 and wifiClient2.
but why you don't use WiFiSSLClient?

Hi.
I solve setting mqttClient.setKeepAliveInterval(60) // 1 minute
Not very clear why but this works.
I thought was enough mqttClient.pool