MKR NB 1500 can’t connect to MQTT broker

Hello Arduino Community,

since a few days I do not get on with my project.
I try to connect with an Arduino MKR NB 1500 as MQTT client to a MQTT broker. Therefore I use the MKRNB.h and the PubSubClient.h libaries.
My hardware setup also includes a suitable antenna and an IoT SIM card. I am confident that the problem is not with my hardware setup because I was already able to output the HTML of a website from the serial interface using the example code "NBWebClient.ino" of the MKRNB.h library.
I already tried to connect to different brokers from this list as well as shiftr .io and tingg .io.

Now I am going to let the code speak.

// libraries
#include <MKRNB.h>
#include <PubSubClient.h>

//The MKRNB lib needs this even if its blank; My sim card does not require a pin
const char PINNUMBER[] = "";

//MQTT info
const char* mqtt_server = "broker.hivemq.com";
const int port = 1883;

const char* clientID = "MKRNB1500";
const char* topic = "test";
const char* payload = "Hallo MQTT Welt";

// const char* username = "username";
// const char* password = "password";

// initialize the library instance
NBClient LTEclient;
GPRS gprs;
NB nbAccess;

//connect the pubsub client
PubSubClient client(LTEclient);

//connection and reconnection function
void reconnect() {
  while (!client.connected()) {
    // Attemp to connect
    Serial.println("connection attemp to broker");
    if (client.connect(clientID)) {       //client.connect(clientID,username,password)
      Serial.println("Connected");
      client.publish(topic,"hello mqtt");
     //client.subscribe(subtopic);
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 2 seconds");
      // Wait 2 seconds before retrying
      delay(2000);
    }
  }
}

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("starting up");

  // connection state
  bool connected = false;

  // After starting the modem with NB.begin()
  // attach to the GPRS network with the APN, login and password
   while (!connected) {
    if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
        (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("modem started && attached to GPRS network");

  client.setServer(mqtt_server, port);
  Serial.println("server details set");
}

void loop(){
    //if the connection drops then reconnect it
  if (!client.connected()) {
    reconnect();
  }
  //This should be called regularly to allow the client to process incoming messages
  //and maintain its connection to the server.
  client.loop();

  client.publish(topic,payload);
  Serial.println("Message Published");

}

My output is as follows:
12:34:17.355 -> starting up
12:34:21.875 -> modem started && attached to GPRS network
12:34:21.875 -> server details set
12:34:21.875 -> connection attemp to broker
12:36:47.813 -> Failed, rc=-2 try again in 2 seconds
12:36:49.835 -> connection attemp to broker
12:40:11.125 -> Failed, rc=-2 try again in 2 seconds

I would be very happy if someone could help me out.
Thank you in advance.

Using HiveMQ requires a TLS Certificate, Username / Password, and Port 8883. See example here: https://community.hivemq.com/t/hivemq-using-esp32-and-nodered/1291

Thank you for your advice. But shouldn't I be able to reach this free public test broker (broker.hivemq.com) without an certificate and TCP port 1883? (network communication is new to me so I ask to be on the safe side)
Nevertheless, I plan to secure the connection with an SSL in the future. Now another problem arises.
I am trying to find a network client that supports SSL. As far as I understand my NBClient from the MKRNB.h does not support SSL. Do you (or someone else here) know a reference / library which allows me to do this with my MKR NB 1500?
And what role can the installed ECC508 crypto chip fulfill here? Please let me know if this should be a new topic.

Not to my knowledge. Have you found some documentation stating that this is the case?

I have absolutely no idea regarding your other questions. I use ESP32 to work with HiveMQ and it just works. Perhaps you should make things easy on yourself and switch to that platform.

In this link under "MQTT connection settings" the tcp port 1883 is specified. That's where I got this assumption.
But it doesn't matter now because I want to use ssl anyway.
I don't want to use any other hardware. Because I only chose this arduino because of the NB/LTE functionality.
To my question which network client supports SSL: Meanwhile I found out that the MKRNB libraryy provides a "NBSSLClient". Unfortunately the official arduino documentation lacks this.
Currently I try to import the required root CA ".pem" via AT command into the modem. For this I use MKRNB-master\examples\Tools\SerialSARAPassthrough. I keep you up to date...
About an info where the mounted crypto chip ATECC508 is needed I would be very grateful.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.