Can't connect to Losant

The first thing I would check is the communications log on the application page. With the application page open, attempt to connect your device. If you see some messages, we at least know the board is reaching Losant. Here’s a screenshot of what I’m referring to:

Also, what is the specific Arduino board you’re using?

Communications log says “No communication logs yet.” I am using the Arduino Mega 2560 r3.

Ok, let’s log the underlying mqtt client’s state to try to see why the board is not reaching Losant. Add a println to the loop where it’s waiting to connect:

while(!device.connected()) {
    delay(500);
    Serial.println(device.mqttClient.state()); // HERE
    Serial.print(".");
}

This should log out some number that we can use for more debugging.

Yep, it prints out ‘-4’

-4 is MQTT_CONNECTION_TIMEOUT, which doesn’t particularly help, but I have another thing to try. Since you said you’re using an older WiFi, you may have to enable an option in the underlying PubSubClient library that we use. From their readme:

Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield,
enable the MQTT_MAX_TRANSFER_SIZE define in PubSubClient.h.

PubSubClient.h will be located in your Arduino’s library folder. On a Mac, it’s at: ~/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h. Open this file and uncomment out the following line (roughly line 42):

#define MQTT_MAX_TRANSFER_SIZE 80

Save the file and recompile and upload your sketch to the Arduino to pick up the change.

Hmm…just gave that a go and it took a little longer to connect to the WiFi and then got stuck in the same “Connecting to Losant” loop. (still reporting a client state of -4 btw)

Can you please try the basic web request example, just to rule out the possibility that the wifi shield is unable to connect to any external source: https://www.arduino.cc/en/Tutorial/WiFiWebClient

That example works great! Glad that part works but it makes it harder to find the connection problem…

Let’s just remove Losant and try the PubSubClient directly. Please attempt to run this sketch.

#include "WiFi.h"
#include "PubSubClient.h"

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Command received.");
}

WiFiClient wiFiclient;
PubSubClient client("broker.losant.com", 1883, callback, wiFiclient);


void setup() {
  WiFi.begin("my-wifi-ssid", "my-wifi-pass");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");

  if(client.connect("my-device-id", "my-access-key", "my-access-token")) {
    Serial.println("Connected to Losant");
  }
  else {
    Serial.println("Failed to connect.");
    Serial.println(client.state());
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  client.loop();
}

I forgot to put the Serial.begin lines in the setup. Make sure you add that.

Added in the Serial.begin…ran the sketch and it printed this:

Connected to WiFi
Failed to connect.
-4

So, I had changed back the MQTT_MAX_TRANSFER_SIZE 80. I uncommented it again and this time it worked! It says:

Connected to WiFi
Connected to Losant

Just tried the original sketch from the first post, and now it works! Hurray! Thanks so much for your help :slight_smile:

Awesome! Looks like MQTT_MAX_TRANSFER_SIZE was likely the trick?

That must have been it. Not sure why it didn’t work the first time but it works now so I can’t complain!

1 Like

my error is -2
the device is authenticated but it is not connecting to losant
error code is -2

Hi @Mohsin_Mushtaq,

Can you provide some screenshots of what you are seeing? What device are you using? Can you also provide a code sample?

Thanks!
Julia

thank you for your response. i have sorted it out. i use unsecure protocol which solved my problem

1 Like

My error is -1. Can anyone help me? I am using ESP32 Heltec WiFi LoRa module…

Hi @Nay_Htun,

Could you please provide some screenshots of what you are seeing? We also have multiple forum posts with the same issue, so maybe one of them could lead to a solution. Here are a couple:

Thanks!
Julia

Thank for your offer, Julia. I have already solved that problem by changing MQTT MAX TRANSFER SIZE.

1 Like