MQTT connection failed! Error code = -1 <<Solved>>


I am using ArduinoMqttClient library, On running a loop every 70 seconds, it connects the first time and sends the message, then on the second message and after retries every 5 mins, I am getting

MQTT connection failed! Error code = -1

I cannot find any reference on the interweb for (-1) error code,

Anyone any ideas??

Thanks in advance.

Any code?

Read How to use this forum, and post your sketch- all of it- using code tags.

#include <MKRGSM.h>
#include <ArduinoMqttClient.h>

GSM gsmAccess;
GSMClient gsmClient;
MqttClient mqttClient(gsmClient);

const char apn[]      = "";
const char login[] = "";      // keep blank if not sure
const char password[] = "";      // keep blank if not sure

const char broker[] = "";
int        port     = 1883;
const char topic[]  = "test2";
const char publishTopic[]  = "357520078007230";

void parseData(String dataToSend)
  mqttClient.setUsernamePassword("username", "password");
  Serial.print("Attempting to connect to the MQTT broker: ");

  Serial.println("Sending data...");
     while (!mqttClient.connect(broker, port))
    Serial.print("MQTT connection failed! Error code = ");
   Serial.println("You're connected to the MQTT broker!");

  // set the message receive callback


This is the code I am calling from the main script for MQTT connection. The main script only sets up GSM connection and gets a temperature reading.


#include <MKRGSM.h>

#include <ArduinoMqttClient.h>

I don’t see anything wrong with the code but I have never used the ArduinoMqttClient library.

Try the PubSubClient library, which I suspect is a lot more commonplace here.

Thanks for all your help. The problem turned out to be a problem with the local SIM card operator. They were having some outage in the area, I switched provider and the problem has been rectified. Once again thanks.

This can be now closed.