[Solved] MKR1000 hangs on trying to connect to Losant

Hi all,
I’m trying to use the arduino example from the documentation. It connects to the WiFi but hangs when connecting to Losant and goes no further.

The device.mqttClient.State() method returns a -1 which I believe translates to MQTT_DISCONNECTED - the client is disconnected cleanly which is strange to me as it never connects in the first place.

I am aware that I need a certificate to connect and I have tried a few different URLs as i’m not sure exactly what URL to use:
triggers.losant.com/
broker.losant.com/
https://app.losant.com/
https ://www.losant.com/ (Ignore the space between the s and :, i’m just trying to stop it from adding a preview)

None of these work and I am at a loss.

Any help wouold be gladly recieved.
Code below.

#include <PubSubClient.h>
#include <WiFi101.h>
#include <Losant.h>

// WiFi credentials.
#include "secrets.h"
//sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

// Losant credentials.
const char* LOSANT_DEVICE_ID = [obscured_device_id];
const char* LOSANT_ACCESS_KEY = [obscured_access_key];
const char* LOSANT_ACCESS_SECRET = [obscured_access_secret];

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

// 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(ssid);

  WiFi.begin(ssid, pass);

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

  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("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.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  while (!device.connected()) {
    delay(500);
    Serial.println(device.mqttClient.state());
    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 to check is your application’s communication log. With your browser window open, power up the Arduino and have it attempt to connect. Unfortunately the MQTT protocol is not very helpful when it comes to error codes, but if our platform sees the device attempting to connect at all, something may appear in that log.

https://docs.losant.com/applications/overview/#communication-log

Nothing so far, just listening.

No logs yet.
Listening...

So it’s still not connecting and i’m still confused as to what the problem with this is.
Any help, please?

The code looks correct. Our broker uses a certificate authority that should be pre-installed on Arduino devices. That may have changed, however. We’ll have to find an Arduino and try it out.

If you do need to install the CA, it can be found here:

Behind the scenes, your device is connecting to broker.losant.com.

You can also try connecting without SSL to see if that eliminates the issue. There’s only two changes that need to be made:

  1. Change WiFiSSLClient to WiFiClient
  2. Change device.connectSecure(...) to device.connect(...)

Try this:

I had to change two macros in PubSubClient.h to get it working on my esp8266

#define MQTT_VERSION MQTT_VERSION_3_1
#define MQTT_MAX_PACKET_SIZE 256

brandon_cannaday:

  • I tried connecting without SSL and was unable to connect.

  • I tried adding broker.losant.com to the certificate uploader in the arduino IDE and I was still unable to connect to losant.
    Is that the correct url? As the one you have provided causes the certificate uploader to throw an error. Is there a simpler one as it seems to want them without a lot of slashes.

cassiano_martin:
thanks or the suggestion, unfortunately it did not work as I am still unable to connect.

I have a MKR1000 and I am following the example mentioned in the original post.

I was not able to connect securely. I did resolve the -1 error by changing MQTT_MAX_PACKET_SIZE from 128 to 256 in PubSubClient.h as mentioned in a previous comment. This then gave me a -2 error. I was able to resolve this error by using the Arduino FirmwareUpdater tool and uploading the certificate found at broker.losant.com:8883. This then gave me error code 5 (Unauthorized) which I have not been able to solve yet.

I tried connecting insecurely and was able to by making the following changes:

  1. WiFiSSLClient wifiClient; to: WiFiClient wifiClient;
  2. device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET); to: device.connect(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

-Kevin

1 Like

In my previous post the last error that was received was error code 5 for Unauthorized. We have solved this issue.

The MKR1000\WiFi101 was trying to connect to the Losant Platform with an incorrect client certificate instead of using the device id, key, and secret. We implemented a fix which is active now that if the client certificate does not succeed that it will try again with the device id, key, and secret.

The steps above in the previous post still apply which are:

  1. Update the MQTT_MAX_PACKET_SIZE from 128 to 256 in PubSubClient.h
  2. Use the Arduino Firmware Updater to upload the broker.losant.com:8883 certificate to the MKR1000\WiFi101.

Thank you,
Kevin