Connecting Takes Forever

Something strange is happening.

I have made several connections in the past to losant platform but had a break developing since the COVID, I decided to pick u where I left off, but I am having difficulty connecting to Losant, my code compiled correctly and uploaded to my ESP32 using Arduino, but would never connect to the Losant platform.

I get something like this:
Connecting to Losant…
for hours nothing is happening beyond this spot.

Can someone assist me on this @Heath my device is not connecting to the platform and this is weird. I have the DEVICEID set, ACCESS KEY, and SECRET KEY but would never connect to losant. What is happening? This is a little frustrating.

For the record, the device is connected to my wifi spot without issue.

Is this problem related to Losant platform???

Hey @Sunday_Ajiroghene,

I definitely understand your frustration.

Would you be able to share your code with me (preferably without your device id, access key, and secret)?

Since you are using Arduino, you are using our Arduino Library, yes?

Also, are you using the Arduino IDE or are you using Platform.io?

Is this problem related to Losant platform???

I cannot say for certain, yet. But we have not received any other reports of devices being unable to connect to Losant.

Thank you,
Heath

@Heath I can share with you everything including the device ID and secret key I can even share with you my login detail, its a proof of concept work that I am doing.

Yes I am using the Arduino - Losant-mqtt library and I am using the Arduino IDE.
Find below my code, which is an exact same of the walkthrough tutorial code found on Github:

/**
 * Example that connects an ESP32 based board to the Losant
 * IoT platform. This example reports state to Losant whenever a button is
 * pressed. It also listens for the "toggle" command to turn the LED on and off.
 *
 * This example assumes the following connections:
 * Button connected to pin 14.
 * LED connected to pin 12.
 *
 * Copyright (c) 2021 Losant. All rights reserved.
 * http://losant.com
 */

#include <WiFiClientSecure.h>
#include <Losant.h>

// WiFi credentials.
const char* WIFI_SSID = "Ajiozi Dev";
const char* WIFI_PASS = "sandiewhyte";

// Losant credentials.
const char* LOSANT_DEVICE_ID = "607ffa0aca605a0007c7e84c"; //607ffa0aca605a0007c7e84c
const char* LOSANT_ACCESS_KEY = "e31ac52c-f66d-4616-9210-17f67aa89d71";
const char* LOSANT_ACCESS_SECRET = "239eefd9b63ea71079156eb19c7c75f9d6ddc7897292a0a97fdbdcba1068876c";

const int BUTTON_PIN = 14;
const int LED_PIN = 12;

bool ledState = false;

// initiate the the wifi client
WiFiClientSecure wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

void toggle() {
  Serial.println("Toggling LED.");
  ledState = !ledState;
  digitalWrite(LED_PIN, ledState ? HIGH : LOW);
}

// Called whenever the device receives a command from the Losant platform.
void handleCommand(LosantCommand *command) {
  Serial.print("Command received: ");
  Serial.println(command->name);

  // Optional command payload. May not be present on all commands.
  // JsonObject payload = *command->payload;

  // Perform action specific to the command received.
  if(strcmp(command->name, "toggle") == 0) {
    toggle();
    /**
    * In Losant, including a payload along with your
    * command is optional. This is an example of how
    * to parse a JSON payload from Losant and print
    * the value of a key called "temperature".
    */
    // int temperature = payload["temperature"];
    // Serial.println(temperature);
  }
}

void connect() {

  // Connect to Wifi.
  Serial.println();
  Serial.println();
  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...");

  device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

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

  Serial.println("Connected!");
}

void setup() {
  Serial.begin(115200);
  while(!Serial) { }
  pinMode(BUTTON_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);

  // Register the command handler to be called when a command is received
  // from the Losant platform.
  device.onCommand(&handleCommand);

  connect();
}

void buttonPressed() {
  Serial.println("Button Pressed!");

  // Losant uses a JSON protocol. Construct the simple state object.
  // { "button" : true }
  StaticJsonDocument<200> jsonBuffer;
  JsonObject root = jsonBuffer.to<JsonObject>();
  root["button"] = true;

  // Send the state to Losant.
  device.sendState(root);
}

int buttonState = 0;

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

  int currentRead = digitalRead(BUTTON_PIN);

  if(currentRead != buttonState) {
    buttonState = currentRead;
    if(buttonState) {
      buttonPressed();
    }
  }

  delay(100);
}

@Sunday_Ajiroghene,

Thank you so much for sharing that!

Nothing in your code is glaringly obvious. I’m going to take this code and test it on my own to verify that things are working correctly.

Just to confirm, you are using the ESP32 Example Code from our Library’s examples?

I will report back with what I find, shortly.

Thank you,
Heath

Thanks @Heath , Yes I am using esp32 example code. I am using the ESP32 WROOM 32 LOLIN32 v1.0.0.

@Sunday_Ajiroghene,

I was able to get my ESP32 connected to Losant simply by copying and pasting the example code, and updating the values for my WiFi, DeviceID, Access Key, and Access Secret.

image

Here’s my code (minus my credentials):

#include <Arduino.h>
/**
 * Example that connects an ESP32 based board to the Losant
 * IoT platform. This example reports state to Losant whenever a button is
 * pressed. It also listens for the "toggle" command to turn the LED on and off.
 *
 * This example assumes the following connections:
 * Button connected to pin 14.
 * LED connected to pin 12.
 *
 * Copyright (c) 2021 Losant. All rights reserved.
 * http://losant.com
 */

#include <WiFiClientSecure.h>
#include <Losant.h>

// WiFi credentials.
const char* WIFI_SSID = "";
const char* WIFI_PASS = "";

// Losant credentials.
const char* LOSANT_DEVICE_ID = "";
const char* LOSANT_ACCESS_KEY = "";
const char* LOSANT_ACCESS_SECRET = "";

const int BUTTON_PIN = 14;
const int LED_PIN = 12;

bool ledState = false;

// initiate the the wifi client
WiFiClientSecure wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

void toggle() {
  Serial.println("Toggling LED.");
  ledState = !ledState;
  digitalWrite(LED_PIN, ledState ? HIGH : LOW);
}

// Called whenever the device receives a command from the Losant platform.
void handleCommand(LosantCommand *command) {
  Serial.print("Command received: ");
  Serial.println(command->name);

  // Optional command payload. May not be present on all commands.
  // JsonObject payload = *command->payload;

  // Perform action specific to the command received.
  if(strcmp(command->name, "toggle") == 0) {
    toggle();
    /**
    * In Losant, including a payload along with your
    * command is optional. This is an example of how
    * to parse a JSON payload from Losant and print
    * the value of a key called "temperature".
    */
    // int temperature = payload["temperature"];
    // Serial.println(temperature);
  }
}

void connect() {

  // Connect to Wifi.
  Serial.println();
  Serial.println();
  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...");

  device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

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

  Serial.println("Connected!");
}

void setup() {
  Serial.begin(115200);
  while(!Serial) { }
  pinMode(BUTTON_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);

  // Register the command handler to be called when a command is received
  // from the Losant platform.
  device.onCommand(&handleCommand);

  connect();
}

void buttonPressed() {
  Serial.println("Button Pressed!");

  // Losant uses a JSON protocol. Construct the simple state object.
  // { "button" : true }
  StaticJsonDocument<200> jsonBuffer;
  JsonObject root = jsonBuffer.to<JsonObject>();
  root["button"] = true;

  // Send the state to Losant.
  device.sendState(root);
}

int buttonState = 0;

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

  int currentRead = digitalRead(BUTTON_PIN);

  if(currentRead != buttonState) {
    buttonState = currentRead;
    if(buttonState) {
      buttonPressed();
    }
  }

  delay(100);
}

I am, though, using Platform.io. This difference should not cause devices to not connect, though.

I also flashed the code you sent along (with my own credentials) and my device connected to Losant also. I did encounter an immediate disconnect, though, right after my device connected. I pressed the physical reset button on my ESP32 and the device reconnected and has remained connected.

Are you able to try a different WiFi Network? Would you happen to have a different ESP32 you can test?

I know that you mentioned that your Serial output would say “Connecting to Losant…” for hours with no change, but are you seeing anything in your Application Log?

Thank you,
Heath

Many thanks @Heath , I noticed you added the arduino library, which I would also add and revert back the result.

Thanks again

@Sunday_Ajiroghene,

I noticed you added the arduino library

Adding the Arduino library to the top is a requirement for Platform.io, and not necessary if you’re using the Arduino IDE.

Another option you can try is, in your code, instead of using device.connectSecure(...), you can use device.connect(...).

The difference here is that this would use a different port for our broker. TLS connections (secure, connectSecure) happen on port 8883, and unsecured connections happen on port 1883 (connect).

Please let me know if any of this works or helps.

Thank you,
Heath

Hello @Heath thanks for the effort so far.

I have tried the option you gave but couldn’t get it working.

I even when ahead to changed my esp32 from the lolin spec to dev kit spec same issue.

I am beginning to think that this might be related to my account.

Would you gracious enough to log into my account and use my credentials to see exactly where the issue is coming from.

In the meantime, I would have to learn quickly how to use the platform.io and retry the whole process again.

losant connect issue .

losant error

@Sunday_Ajiroghene,

I followed up with you privately in a Direct Message.

Thank you,
Heath

update on the problem connecting, thanks to @Heath for his assistance, I eventually get it to connect using the following code as below:

/**
 * Example that connects an ESP32 based board to the Losant
 * IoT platform. This example reports state to Losant whenever a button is
 * pressed. It also listens for the "toggle" command to turn the LED on and off.
 *
 * This example assumes the following connections:
 * Button connected to pin 14.
 * LED connected to pin 12.
 *
 * Copyright (c) 2021 Losant. All rights reserved.
 * http://losant.com
 * 
 */
 
#include "PubSubClient.h"
#include <WiFiClient.h>
#include <Losant.h>
#include <ETH.h>
#include <WiFi.h>
#include <WiFiAP.h>
#include <WiFiClient.h>
#include <WiFiGeneric.h>
#include <WiFiMulti.h>
#include <WiFiScan.h>
#include <WiFiServer.h>
#include <WiFiSTA.h>
#include <WiFiType.h>
#include <WiFiUdp.h>
#include <MQTT.h>
#include <MQTTClient.h>


// WiFi credentials.
const char* WIFI_SSID = "Ajiozi Dev";
const char* WIFI_PASS = "sandiewhyte";

// Losant credentials.
const char* LOSANT_DEVICE_ID = "xxxxxxxxxxxxxxxxxxxx"; 
const char* LOSANT_ACCESS_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const char* LOSANT_ACCESS_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";

const int BUTTON_PIN = 14;
const int LED_PIN = 12;

bool ledState = false;

// initiate the the wifi client
WiFiClient wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

void toggle() {
  Serial.println("Toggling LED.");
  ledState = !ledState;
  digitalWrite(LED_PIN, ledState ? HIGH : LOW);
}

// Called whenever the device receives a command from the Losant platform.
void handleCommand(LosantCommand *command) {
  Serial.print("Command received: ");
  Serial.println(command->name);

  // Optional command payload. May not be present on all commands.
  // JsonObject payload = *command->payload;

  // Perform action specific to the command received.
  if(strcmp(command->name, "toggle") == 0) {
    toggle();
    /**
    * In Losant, including a payload along with your
    * command is optional. This is an example of how
    * to parse a JSON payload from Losant and print
    * the value of a key called "temperature".
    */
    // int temperature = payload["temperature"];
    // Serial.println(temperature);
  }
}

void connect() {

  // Connect to Wifi.
  Serial.println();
  Serial.println();
  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...");

  device.connect(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) { }
  pinMode(BUTTON_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);

  // Register the command handler to be called when a command is received
  // from the Losant platform.
  device.onCommand(&handleCommand);

  connect();
}

void buttonPressed() {
  Serial.println("Button Pressed!");

  // Losant uses a JSON protocol. Construct the simple state object.
  // { "button" : true }
  StaticJsonDocument<200> jsonBuffer;
  JsonObject root = jsonBuffer.to<JsonObject>();
  root["button"] = true;

  // Send the state to Losant.
  device.sendState(root);
}

int buttonState = 0;

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

  int currentRead = digitalRead(BUTTON_PIN);

  if(currentRead != buttonState) {
    buttonState = currentRead;
    if(buttonState) {
      buttonPressed();
    }
  }/

  delay(100);
}

The solution was replacing the device.connectSecure(...), with device.connect(...) and thereafter deleted the #include <WiFiClientSecure.h> while importing the unsecured wifi library as seen. I then moved on to replacing the WiFiClientSecure wifiClient; to WiFiClient wifiClient;

Meanwhile, I would love to know why the secured wouldn’t work but the unsecured worked, has it to do with my wifi security compliance?

@Sunday_Ajiroghene,

has it to do with my wifi security compliance?

Losant uses port 1883 for unsecured MQTT communication, and port 8883 for TLS /secured MQTT communication. So it could be a few things:

  1. Port 8883 is blocked by your network.
  2. Your device isn’t able to communicate via SSL/TLS.
  3. Your device can’t verify the SSL/TLS certificate being used.

I’m glad that we were able to get you connected!

Thank you,
Heath

@Heath ok, thanks, I would look into this port thing thus.

Thank you
Ajioz