# Can't connect to Losant

**URL:** <https://forums.losant.com/t/cant-connect-to-losant/179>\
**Category:** Help\
**Created:** [June 10, 2016, 12:31pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179 "2016-06-10T12:31:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jana\_R](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jana\_R](https://forums.losant.com/u/Jana_R)\
**Post date:** [June 10, 2016, 12:31pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/1 "2016-06-10T12:31:52Z")

</div>

I’ve been working on getting a simple Arduino board connected to Losant. I’m using the older Arduino Wifi shield and I updated the firmware so now it connects to the wifi with no problems. However, it has yet to connect to Losant. It just gets stuck in the connecting loop. I even tried changing the Losant.h file like in the “MQTT Not Connecting” help topic (until I realized that it was no longer relevant haha). Any suggestions? Code I’m using is below:

```auto
include WiFi.h
include Losant.h

// WiFi credentials.
 char* WIFI_SSID = "my_network";
 char* WIFI_PASS = "my_password";

// Losant credentials.
  char* LOSANT_DEVICE_ID = "my_id";
  char* LOSANT_ACCESS_KEY = "my_key";
  char* LOSANT_ACCESS_SECRET = "my_secret";

// Create a secure WiFi client. This can also be an instance of the unsecured
// WiFiClient class, but the secure TLS client is recommended.
WiFiClient client;

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

  // Connect the device instance to Losant using TLS encryption.
  device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  while(!device.connected()) {
    delay(500);
    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();
}

```

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [June 10, 2016, 2:36pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/2 "2016-06-10T14:36:39Z")

</div>

The first thing I would check is the communications log on the application page. With the application page open, attempt to connect your device. If you see some messages, we at least know the board is reaching Losant. Here’s a screenshot of what I’m referring to:

 ![](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/1X/0782c09cf9a754550b592bba74a24eb9a3ae1664.png)

Also, what is the specific Arduino board you’re using?

---

<div class="post-metadata">

**Author:** ![Jana\_R](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jana\_R](https://forums.losant.com/u/Jana_R)\
**Post date:** [June 10, 2016, 2:41pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/3 "2016-06-10T14:41:36Z")

</div>

Communications log says “No communication logs yet.” I am using the Arduino Mega 2560 r3.

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [June 10, 2016, 2:48pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/4 "2016-06-10T14:48:53Z")

</div>

Ok, let’s log the underlying mqtt client’s state to try to see why the board is not reaching Losant. Add a println to the loop where it’s waiting to connect:

```auto
while(!device.connected()) {
    delay(500);
    Serial.println(device.mqttClient.state()); // HERE
    Serial.print(".");
}

```

This should log out some number that we can use for more debugging.

---

<div class="post-metadata">

**Author:** ![Jana\_R](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jana\_R](https://forums.losant.com/u/Jana_R)\
**Post date:** [June 10, 2016, 2:51pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/5 "2016-06-10T14:51:16Z")

</div>

Yep, it prints out ‘-4’

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [June 10, 2016, 2:59pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/6 "2016-06-10T14:59:55Z")

</div>

-4 is `MQTT_CONNECTION_TIMEOUT`, which doesn’t particularly help, but I have another thing to try. Since you said you’re using an older WiFi, you may have to enable an option in the underlying PubSubClient library that we use. From their readme:

> Arduino WiFi Shield - if you want to send packets \> 90 bytes with this shield,  
> enable the MQTT\_MAX\_TRANSFER\_SIZE define in PubSubClient.h.

PubSubClient.h will be located in your Arduino’s library folder. On a Mac, it’s at: `~/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h`. Open this file and uncomment out the following line (roughly line 42):

```arduino
#define MQTT_MAX_TRANSFER_SIZE 80

```

Save the file and recompile and upload your sketch to the Arduino to pick up the change.

---

<div class="post-metadata">

**Author:** ![Jana\_R](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jana\_R](https://forums.losant.com/u/Jana_R)\
**Post date:** [June 10, 2016, 3:05pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/7 "2016-06-10T15:05:48Z")

</div>

Hmm…just gave that a go and it took a little longer to connect to the WiFi and then got stuck in the same “Connecting to Losant” loop. (still reporting a client state of -4 btw)

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [June 10, 2016, 3:10pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/8 "2016-06-10T15:10:17Z")

</div>

Can you please try the basic web request example, just to rule out the possibility that the wifi shield is unable to connect to any external source: [https://www.arduino.cc/en/Tutorial/WiFiWebClient](https://www.arduino.cc/en/Tutorial/WiFiWebClient)

---

<div class="post-metadata">

**Author:** ![Jana\_R](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jana\_R](https://forums.losant.com/u/Jana_R)\
**Post date:** [June 10, 2016, 3:21pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/9 "2016-06-10T15:21:39Z")

</div>

That example works great! Glad that part works but it makes it harder to find the connection problem…

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [June 10, 2016, 3:39pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/10 "2016-06-10T15:39:24Z")

</div>

Let’s just remove Losant and try the PubSubClient directly. Please attempt to run this sketch.

```auto
#include "WiFi.h"
#include "PubSubClient.h"

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Command received.");
}

WiFiClient wiFiclient;
PubSubClient client("broker.losant.com", 1883, callback, wiFiclient);

void setup() {
  WiFi.begin("my-wifi-ssid", "my-wifi-pass");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");

  if(client.connect("my-device-id", "my-access-key", "my-access-token")) {
    Serial.println("Connected to Losant");
  }
  else {
    Serial.println("Failed to connect.");
    Serial.println(client.state());
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  client.loop();
}

```

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [June 10, 2016, 3:44pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/11 "2016-06-10T15:44:27Z")

</div>

I forgot to put the `Serial.begin` lines in the setup. Make sure you add that.

---

<div class="post-metadata">

**Author:** ![Jana\_R](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jana\_R](https://forums.losant.com/u/Jana_R)\
**Post date:** [June 10, 2016, 3:46pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/12 "2016-06-10T15:46:57Z")

</div>

Added in the Serial.begin…ran the sketch and it printed this:

Connected to WiFi  
Failed to connect.  
-4

---

<div class="post-metadata">

**Author:** ![Jana\_R](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jana\_R](https://forums.losant.com/u/Jana_R)\
**Post date:** [June 10, 2016, 3:49pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/13 "2016-06-10T15:49:31Z")

</div>

So, I had changed back the MQTT\_MAX\_TRANSFER\_SIZE 80. I uncommented it again and this time it worked! It says:

Connected to WiFi  
Connected to Losant

Just tried the original sketch from the first post, and now it works! Hurray! Thanks so much for your help 🙂

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [June 10, 2016, 3:54pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/14 "2016-06-10T15:54:00Z")

</div>

Awesome! Looks like MQTT\_MAX\_TRANSFER\_SIZE was likely the trick?

---

<div class="post-metadata">

**Author:** ![Jana\_R](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@Jana\_R](https://forums.losant.com/u/Jana_R)\
**Post date:** [June 10, 2016, 3:57pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/15 "2016-06-10T15:57:01Z")

</div>

That must have been it. Not sure why it didn’t work the first time but it works now so I can’t complain!

---

<div class="post-metadata">

**Author:** ![Mohsin\_Mushtaq](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Mohsin\_Mushtaq](https://forums.losant.com/u/Mohsin_Mushtaq)\
**Post date:** [June 15, 2019, 6:06am UTC](https://forums.losant.com/t/cant-connect-to-losant/179/16 "2019-06-15T06:06:00Z")

</div>

my error is -2  
the device is authenticated but it is not connecting to losant  
error code is -2

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [June 17, 2019, 2:14pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/17 "2019-06-17T14:14:16Z")

</div>

Hi @Mohsin_Mushtaq,

Can you provide some screenshots of what you are seeing? What device are you using? Can you also provide a code sample?

Thanks!  
Julia

---

<div class="post-metadata">

**Author:** ![Mohsin\_Mushtaq](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Mohsin\_Mushtaq](https://forums.losant.com/u/Mohsin_Mushtaq)\
**Post date:** [June 18, 2019, 4:37pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/18 "2019-06-18T16:37:25Z")

</div>

thank you for your response. i have sorted it out. i use unsecure protocol which solved my problem

---

<div class="post-metadata">

**Author:** ![Nay\_Htun](https://avatars.discourse-cdn.com/v4/letter/n/dec6dc/32.png) [@Nay\_Htun](https://forums.losant.com/u/Nay_Htun)\
**Post date:** [August 11, 2019, 6:23pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/19 "2019-08-11T18:23:19Z")

</div>

My error is -1. Can anyone help me? I am using ESP32 Heltec WiFi LoRa module…

---

<div class="post-metadata">

**Author:** ![JuliaKempf](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/juliakempf/32/2446_2.png) [@JuliaKempf](https://forums.losant.com/u/JuliaKempf)\
**Post date:** [August 13, 2019, 5:38pm UTC](https://forums.losant.com/t/cant-connect-to-losant/179/20 "2019-08-13T17:38:32Z")

</div>

Hi @Nay_Htun,

Could you please provide some screenshots of what you are seeing? We also have multiple forum posts with the same issue, so maybe one of them could lead to a solution. Here are a couple:

> [@\[Solved\] Can't connect to Losant MESSAGE: mqttClient.state -1](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329):
>
> Hi, I made a code to my WEMOS D1 Controller and ths code should be send data (temp, humidity) to Losant, Connecting to WiFi is ok, but when the Controller connecting to Losant is a message: WiFi connected IP address: 192.168.43.222 Authenticating Device…This device is authorized! `WiFi connected IP address: 192.168.43.222 Authenticating Device…This device is authorized! Connecting to Losant…-1 .-1 .-1 .-1 .-1 .-1 ` My code: [https://pastebin.com/XGDwJfHL](https://pastebin.com/XGDwJfHL) In Logs in Losant websi…

> [@\[Solved\] Can't connect to Losant with ESP32](https://forums.losant.com/t/solved-cant-connect-to-losant-with-esp32/1364/3):
>
> I have changed code that use WiFiClient instead of WiFiClientSecure and use device.connect instead of device.connectSecure. unfortunately, Errors continue to occur. Serial log is below. ====================== Connecting to MakeySQ . WiFi connected IP address: 192.168.0.20 Connecting to Losant…-1 .-1 .-1 .-1 .-1 .-1 .-1 .-1 .-1 .-1

Thanks!  
Julia

[Next page](https://forums.losant.com/t/cant-connect-to-losant/179.md?page=2)
