MKR1000 hangs on trying to connect to Losant


#1

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();
}

#2

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


#3

Nothing so far, just listening.

No logs yet.
Listening...

#4

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


#5

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(...)

#6

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


#7

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.