Can't connect to Losant

I’ve been working on getting a simple Arduino board connected to Losant. I’m using the older Arduino Wifi shield and I updated the firmware so now it connects to the wifi with no problems. However, it has yet to connect to Losant. It just gets stuck in the connecting loop. I even tried changing the Losant.h file like in the “MQTT Not Connecting” help topic (until I realized that it was no longer relevant haha). Any suggestions? Code I’m using is below:

include  WiFi.h
include Losant.h

// WiFi credentials.
 char* WIFI_SSID = "my_network";
 char* WIFI_PASS = "my_password";

// Losant credentials.
  char* LOSANT_DEVICE_ID = "my_id";
  char* LOSANT_ACCESS_KEY = "my_key";
  char* LOSANT_ACCESS_SECRET = "my_secret";

// Create a secure WiFi client. This can also be an instance of the unsecured
// WiFiClient class, but the secure TLS client is recommended.
WiFiClient client;

// Create an instance of a Losant device.
LosantDevice device(LOSANT_DEVICE_ID);

// Connects to WiFi and then to the Losant platform.
void connect() {

  // Connect to WiFi.
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Connect to Losant.
  Serial.println();
  Serial.print("Connecting to Losant...");

  // Connect the device instance to Losant using TLS encryption.
  device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  while(!device.connected()) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("Connected!");
}

void setup() {
  Serial.begin(115200);
  while(!Serial) { }

  connect();
}

void loop() {

  bool toReconnect = false;

  if(WiFi.status() != WL_CONNECTED) {
    Serial.println("Disconnected from WiFi");
    toReconnect = true;
  }

  if(!device.connected()) {
    Serial.println("Disconnected from Losant");
    toReconnect = true;
  }

  if(toReconnect) {
    connect();
  }

  device.loop();
}

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