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:
https :// (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 ");

  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {

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

  // Connect to Losant.
  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()) {


void setup() {
  while (!Serial) { }


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



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.


Nothing so far, just listening.

No logs yet.


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

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




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

  • I tried adding 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.

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