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.
@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);
}
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.
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?
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).
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?