MQTT with MKR NB 1500

Hello,

For the past few days I have been trying to connect to an MQTT Broker using the MKR NB 1500 module. The arduino connects correctly to the NB network, using 1NCE sim.
If I run the Sketch, after a few seconds from the last log (AT+USOCO=0,"mqtt.eu.urbanasolutions.io",8883) it seems that the arduino crashes and the LED L starts pulsing.
I have also tried other MQTT brokers but the result does not change.
I have tried using the ArduinoMqttClient library but the Sketch is not executed by the Arduino, it immediately crashes.

Has anyone been able to connect to a MQTT broker via secure connection?

#include <MKRNB.h>
#include <MQTT.h>
#include <string.h>
//#include <ArduinoMqttClient.h>

enum ConnectionStatus {
  CELLULAR_CONNECTION,
  MQTT_CONNECTION,
  CONNECTED,
};

const char SIM_PIN[]              = "";
const char PROVIDER_APN[]         = "iot.1nce.net";
const char PROVIDER_LOGIN[]       = "";
const char PROVIDER_PASSWORD[]    = "";
const char MQTT_BROKER[]          = "mqtt.eu.urbanasolutions.io";
const char MQTT_USERNAME[]        = "USERNAME_REMOVED";
const char MQTT_PASSWORD[]        = "PASSWORD_REMOVED";
const char MQTT_CLIENTID[]        = "CLIENTID_REMOVED";
const char MQTT_TOPIC[]           = "myTopic";

NBSSLClient nbClient(true);
NB nbAccess(true);
//MqttClient mqttClient(nbClient);
MQTTClient mqttClient;

ConnectionStatus conStatus;
unsigned long lastMillisFsmNB = 0;

void fsmNB() {
  switch(conStatus) {
    case CELLULAR_CONNECTION:
      //Device not connected to cellular network -> Start connection with cellular network 
      if((millis() - lastMillisFsmNB) > 1000) {
        Serial.println("Connecting to Cellular Network");
        if(nbAccess.begin(SIM_PIN, PROVIDER_APN, PROVIDER_LOGIN, PROVIDER_PASSWORD) == NB_READY) {
          Serial.println("Connected to Cellular Network");
          //Connected to cellular network -> MQTT connection
          conStatus = MQTT_CONNECTION;
        } else
          Serial.println("--- Waiting connection to Cellular Network");
        
        lastMillisFsmNB = millis();
      }
      break;
    case MQTT_CONNECTION:
      //Start connection with MQTT broker
      if((millis() - lastMillisFsmNB) > 5000) {
        Serial.println("Connecting to MQTT Broker");
        //mqttClient.setId(MQTT_CLIENTID);
        //mqttClient.setUsernamePassword(MQTT_USERNAME, MQTT_PASSWORD);
        //mqttClient.setCleanSession(true);
        if(mqttClient.connect(MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD)) {
        //if(mqttClient.connect(MQTT_BROKER, 8883)) {
          Serial.println("Connected to MQTT Broker");
          //Connected to cellular network -> MQTT connection
          conStatus = CONNECTED;
        } else {
          //Serial.println("--- MQTT Broker connection error: " + String(mqttClient.connectError()));
          Serial.println("--- MQTT Broker connection error: " + String(mqttClient.lastError()) + " " + String(mqttClient.returnCode()));
        }
        
        lastMillisFsmNB = millis();
      }
      break;
  }
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  conStatus = CELLULAR_CONNECTION;
  lastMillisFsmNB = millis();

  mqttClient.begin(MQTT_BROKER, 8883, nbClient);
  mqttClient.setKeepAlive(3600);
  mqttClient.setCleanSession(true);
  //mqttClient.setWill(MQTT_TOPIC);
}

void loop() {
  //mqttClient.poll();
  mqttClient.loop();

  fsmNB();
}

Output Serial Monitor:

Connecting to Cellular Network
��0
	*�H��
0��10	UGB1e0UGreater Manchester10UaaSalford10U
COMODO CA Limited1+0)U"COMODO RSA Certification Authority0
100119000000Z
380118235959Z0��10	UGB1e0UGreater Manchester10UaaSalford10U
COMODO CA Limited1+0)U"COMODO RSA Certification Authority0�"0
	*�H��
�0�
���T��
V��
$���Dgt�+7�}#pq�S���*�KV��pr�a�K�=�a�����>
�>\���4z�k眳���zv�q����
��l�߰��~��Ħ/O���gCr�։�k,�폘�~��n�ب�$�Ckb�U����l������li��xH0Eխ�
<E`�2�Q'�g��.�k�F�� ��e�H��N��F7���́HCgNr*�\�Le(�\"{������Q��	FNm>����|3WA<�Q��\�,c�W�?��]ě�E��Z$���V=�o��IX�������7����:��CB�؄��ٻ�`��(V��
�q���=���H�<���
"L���V;�Ӣ�[��"R���i�Le�-pt�g)iR���PjUF���(ap�â�,!�G�(�Ev��'�ծ��P�k�L�q0������@���B��3:.\A�c�qk+즊�1\:jG��yY� ��j��r�WJ�K����LA��=^(��̿�QڧG�S�B0@0U��~=���<����8���22�0U�0U�0�0
	*�H��
�
��F���Q�l�MA�L����TϠ%�����
� �<-�V���~��.��.�r��Dla5���iZ���>��/��ae*��z�����������+�=���s	XFn����'ӕ�7�r�s�G�8]�Iy)�� a$iWx�9�:����|��.�b'�9���<	h���<���W����OȽ�#��ICy�\�����h�L��
�S,���	ue�{�#��(��e��+b�{	��g��hjc!u���n�ƅ�-���$G����yX���*ٝה}�J����>�V���I=��	��OI�=�@��Y��:�.i<l����{��2F���uL�KFc�4@p�ù�ݦp��A�逑�d�z�"��noeOl��^�n��u��@�S�'�J��w!����޼��߷I��n0Nv����V�}��m
�������:N���nlŠ|� �����
�ˑ��|��if�����-�Q!�5S�J]���e_t
+USECMNG: 0,0,"COMODO_RSA_Certification_Authority","1B31B0714036CC143691ADC43EFDEC18"

OK
AT

OK
AT+CMEE=0

OK
AT+CFUN=0

OK
AT+CPIN?

+CPIN: READY

OK
AT+CMGF=1

OK
AT+UDCONF=1,1

OK
AT+CTZU=1

OK
AT+CGDCONT=1,"IP","iot.1nce.net"
OK
AT+UAUTHREQ=1,0

OK
AT+CFUN=1

OK
AT+CEREG?

+CEREG: 0,2

OK
AT+CEREG?

+CEREG: 0,2

OK
AT+CEREG?

+CEREG: 0,2

OK
AT+CEREG?

+CEREG: 0,2

OK
AT+CEREG?

+CEREG: 0,2

OK
AT+CEREG?

+CEREG: 0,5

OK
Connected to Cellular Network
Connecting to MQTT Broker
AT+USECMNG=0,0,"AddTrust_External_CA_Root",1082

>
+USECMNG: 0,0,"AddTrust_External_CA_Root","1D3554048578B03F42424DBF20730A3F"

OK
AT+USECMNG=0,0,"Baltimore_CyberTrust_Root",891

>
+USECMNG: 0,0,"Baltimore_CyberTrust_Root","ACB694A59C17E0D791529BB19706A6E4"

OK
AT+USECMNG=0,0,"COMODO_RSA_Certification_Authority",1500

>
+USECMNG: 0,0,"COMODO_RSA_Certification_Authority","1B31B0714036CC143691ADC43EFDEC18"

OK
AT+USECMNG=0,0,"DST_Root_CA_X3",846

>
+USECMNG: 0,0,"DST_Root_CA_X3","410352DC0FF7501B16F0028EBA6F45C5"

OK
AT+USECMNG=0,0,"DigiCert_High_Assurance_EV_Root_CA",969

>
+USECMNG: 0,0,"DigiCert_High_Assurance_EV_Root_CA","D474DE575C39B2D39C8583C5C065498A"

OK
AT+USECMNG=0,0,"Entrust_Root_Certification_Authority",1173

>
+USECMNG: 0,0,"Entrust_Root_Certification_Authority","D6A5C3ED5DDD3E00C13D87921F1D3FE4"

OK
AT+USECMNG=0,0,"Equifax_Secure_Certificate_Authority",804

>
+USECMNG: 0,0,"Equifax_Secure_Certificate_Authority","67CB9DC013248A829BB2171ED11BECD4"

OK
AT+USECMNG=0,0,"GeoTrust_Global_CA",856

>
+USECMNG: 0,0,"GeoTrust_Global_CA","F775AB29FB514EB7775EFF053C998EF5"

OK
AT+USECMNG=0,0,"GeoTrust_Primary_Certification_Authority_G3",1026

>
+USECMNG: 0,0,"GeoTrust_Primary_Certification_Authority_G3","B5E83436C910445848706D2E83D4B805"

OK
AT+USECMNG=0,0,"GlobalSign",958

>
+USECMNG: 0,0,"GlobalSign","9414777E3E5EFD8F30BD41B0CFE7D030"

OK
AT+USECMNG=0,0,"Go_Daddy_Root_Certificate_Authority_G2",969

>
+USECMNG: 0,0,"Go_Daddy_Root_Certificate_Authority_G2","803ABC22C1E6FB8D9B3B274A321B9A01"

OK
AT+USECMNG=0,0,"VeriSign_Class_3_Public_Primary_Certification_Authority_G5",1239

>
+USECMNG: 0,0,"VeriSign_Class_3_Public_Primary_Certification_Authority_G5","CB17E431673EE209FE455793F30AFA1C"

OK
AT+USECMNG=2,0,"AmazonRootCA1"

ERROR
AT+USECMNG=0,0,"Starfield_Services_Root_Certificate_Authority_G2",1011

>
+USECMNG: 0,0,"Starfield_Services_Root_Certificate_Authority_G2","173574AF7B611CEBF4F93CE2EE40F9A2"

OK
AT+USOCR=6

+USOCR: 0

OK
AT+USOSEC=0,1,0

OK
AT+USECPRF=0,0,1

OK
AT+USOCO=0,"mqtt.eu.urbanasolutions.io",8883

Thanks.

Small update. I switched to the ArduinoMqttClient library and I call the .poll method only if the MQTT connection was successful, Arduino now no longer crashes. :melting_face:
From the new logs, the Arduino cannot open a TCP socket. I tried to open a socket with a python script and it works without any problems.

The MQTT broker uses self signed certificates, could this be the problem?

The logs now are:

AT+USOCR=6

+USOCR: 0

OK
AT+USOSEC=0,1,0

OK
AT+USECPRF=0,0,1

OK
AT+USOCO=0,"mqtt.eu.urbanasolutions.io",8883

ERROR

+UUSOCL: 0
AT+USOCL=0

ERROR
--- MQTT Broker connection error: -2
Connecting to MQTT Broker
AT+USOER

+USOER: 9

OK
AT+USOCR=6

+USOCR: 0

OK
AT+USOSEC=0,1,0

OK
AT+USECPRF=0,0,1

OK
AT+USOCO=0,"mqtt.eu.urbanasolutions.io",8883

ERROR

+UUSOCL: 0
AT+USOCL=0

ERROR
--- MQTT Broker connection error: -2

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