Issue connecting to losant

We are running an esp8266 and we keep getting error code -2 when it is trying to connect. We are using the sample code in the losant MQTT client example.

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

// WiFi credentials.
const char WIFI_SSID = “WIFI_SSID”;*
const char WIFI_PASS = “WIFI_PASS”;*

// Losant credentials.
const char LOSANT_DEVICE_ID = “my-device-id”;*
const char LOSANT_ACCESS_KEY = “my-app-key”;*
const char LOSANT_ACCESS_SECRET = “my-app-secret”;*

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

bool ledState = false;

WiFiSSLClient wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

// Toggles and LED on or off.
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);**

** if(strcmp(command->name, “toggle”) == 0) {**
** toggle();**
** }**

void connect() {

** WiFi.begin(WIFI_SSID, WIFI_PASS);**

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

** // Connect to Losant.**
** device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);**

** while(!device.connected()) {**
** delay(500);**
** }**

void setup() {
** Serial.begin(115200);**
** delay(100);**
** 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 }**
** StaticJsonBuffer<200> jsonBuffer;**
** JsonObject& root = jsonBuffer.createObject();**
** 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);**

Hi @Yader_Mendieta,

I would recommend checking out the resolution steps found here:

Please let me know if you have any further questions.


I am having similar issues. I have narrowed it down (maybe incorrectly) to JSON upgrade from 5 to 6. I have tried following along with the upgrade instructions on the ArduinoJson upgrade site but I am still having issues. Will the examples be changed to reflect the JSON upgrade?


What issues are you having? Could you post a screen shot of any errors you are receiving? I can help point you in the right direction.

Thanks @anaptfox
I am just following the directions from this page… and I get the following error

Arduino: 1.8.10 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), dtr (aka nodemcu), 26 MHz, 40MHz, DOUT (compatible), 1MB (FS:64KB OTA:~470KB), 2, nonos-sdk 2.2.1+100 (190703), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

wifitest:75:11: error: StaticJsonBuffer is a class from ArduinoJson 5. Please see to learn how to upgrade your program to ArduinoJson version 6

 StaticJsonBuffer<200> jsonBuffer;


wifitest:137:11: error: StaticJsonBuffer is a class from ArduinoJson 5. Please see to learn how to upgrade your program to ArduinoJson version 6

   StaticJsonBuffer<500> jsonBuffer;


Multiple libraries were found for "ESP8266HTTPClient.h"
 Used: C:\Users\jelse\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\ESP8266HTTPClient
Multiple libraries were found for "Losant.h"
 Used: C:\Users\jelse\Documents\Arduino\libraries\losant-mqtt-arduino
 Not used: C:\Users\jelse\Documents\Arduino\libraries\losant-mqtt-arduino-master
Multiple libraries were found for "PubSubClient.h"
 Used: C:\Users\jelse\Documents\Arduino\libraries\PubSubClient
Multiple libraries were found for "ArduinoJson.h"
 Used: C:\Users\jelse\Documents\Arduino\libraries\ArduinoJson
Multiple libraries were found for "ESP8266WiFi.h"
 Used: C:\Users\jelse\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\libraries\ESP8266WiFi
exit status 1
StaticJsonBuffer is a class from ArduinoJson 5. Please see to learn how to upgrade your program to ArduinoJson version 6

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Hey @Eddie_Elsenburg, this is because one of the underlying libraries (ArduinoJson) did a major update that is not compatible with our library.

You’ll want to ensure you’ve installed a 5.x version of the ArduinoJson library, and not the latest 6.x version.

Well I will be disappointed if this is the solution…well disappointed in myself for not trying this in the first place as I have had this in the past where I had to downgrade an update because of incompatibility.
I will try this when I get home this afternoon…thanks for the suggestion. I will let you know how it works.