# \[Solved\] Can't connect to Losant MESSAGE: mqttClient.state -1

**URL:** <https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329>\
**Category:** Help\
**Tags:** mqtt, pubsub\
**Created:** [December 17, 2018, 7:32pm UTC](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329 "2018-12-17T19:32:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mslawo95](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mslawo95](https://forums.losant.com/u/mslawo95)\
**Post date:** [December 17, 2018, 7:32pm UTC](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329/1 "2018-12-17T19:32:54Z")

</div>

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 website I have only message:

`Device RoomSensor1 authenticated with Losant Authentication succeeded ndz 16 gru 2018 21:45:41 GMT+01:00`

so the Controller had a connection/authentication succeeded, but next process is won’t be gone… all the time this message which I paste over.

Please HELP ME!

I made this Tutorial/Course with Losant:

> **[Getting Started With the ESP8266 and DHT22 Sensor](https://www.losant.com/blog/getting-started-with-the-esp8266-and-dht22-sensor)**
>
> A tutorial on getting started with the ESP8266 NodeMCU and a DHT22 temperature and humidity sensor.

---

<div class="post-metadata">

**Author:** ![Michael\_Kuehl](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/michael_kuehl/32/99_2.png) [@Michael\_Kuehl](https://forums.losant.com/u/Michael_Kuehl)\
**Post date:** [December 18, 2018, 7:23pm UTC](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329/2 "2018-12-18T19:23:49Z")

</div>

So the initial output (and the message in the Losant application log) is just verifying that your credentials are valid using the REST API (so we can give a nicer error message for invalid device ID / access key / secret). The fact that that prints out that `This device is authorized!` means your that info is correct.

The `-1` translates to the status `MQTT_DISCONNECTED` (see [https://github.com/knolleary/pubsubclient/blob/master/src/PubSubClient.h#L48](https://github.com/knolleary/pubsubclient/blob/master/src/PubSubClient.h#L48)) which means the MQTT client hasn’t even begun connecting yet. How long have you let it print out the dots and `-1`? Does it never move beyond the `-1` status? One thing you could try would be to use `connect` instead of `connectSecure` - it might be an issue with accepting Losant’s SSL certificate.

---

<div class="post-metadata">

**Author:** ![mslawo95](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mslawo95](https://forums.losant.com/u/mslawo95)\
**Post date:** [December 18, 2018, 7:49pm UTC](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329/3 "2018-12-18T19:49:28Z")

</div>

I have all the time only -1 answer, that can be after 10 minutes too is -1… I can try with connect secure but I am not sure, that I had it earlier and was the same (mqqt - 1)

---

<div class="post-metadata">

**Author:** ![Yann\_Randin](https://avatars.discourse-cdn.com/v4/letter/y/e19b73/32.png) [@Yann\_Randin](https://forums.losant.com/u/Yann_Randin)\
**Post date:** [January 4, 2019, 11:55am UTC](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329/4 "2019-01-04T11:55:17Z")

</div>

Hello,  
Quite the same problem here with a similiar hardware (NodeMcu V3 ESP8266 MOD).  
Stuck when trying to connect to Losant, I get … (but no -1).

I tried to modify the line device.connectSecure (wifiClient, LOSANT\_ACCESS\_KEY, LOSANT\_ACCESS\_SECRET); by using device.connect instead but the problem remains the same.

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

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

Any help would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![Kevin\_Niemiller](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/kevin_niemiller/32/745_2.png) [@Kevin\_Niemiller](https://forums.losant.com/u/Kevin_Niemiller)\
**Post date:** [January 4, 2019, 1:58pm UTC](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329/5 "2019-01-04T13:58:07Z")

</div>

I have a MKR1000 and I am following the example [here](https://docs.losant.com/getting-started/boards/getting-started-with-arduino-wifi-101/).

I was able to connect insecurely by making the following 3 changes:

1. WiFiSSLClient wifiClient; to: WiFiClient wifiClient;
2. device.connectSecure(wifiClient, LOSANT\_ACCESS\_KEY, LOSANT\_ACCESS\_SECRET); to: device.connect(wifiClient, LOSANT\_ACCESS\_KEY, LOSANT\_ACCESS\_SECRET);
3. Change the PubSubClient version from v2.7.0 to v2.6.0.

I am not able to connect securely at the moment but I am currently investigating that issue now.

Also, in the example I am following I added Serial.print(device.mqttClient.state()); I put this code in this while loop: while(!device.connected() so that I could see the client state value when trying to connect to Losant (the -1 seen above).

Thank you,  
Kevin

---

<div class="post-metadata">

**Author:** ![Kevin\_Niemiller](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/kevin_niemiller/32/745_2.png) [@Kevin\_Niemiller](https://forums.losant.com/u/Kevin_Niemiller)\
**Post date:** [January 4, 2019, 3:12pm UTC](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329/6 "2019-01-04T15:12:04Z")

</div>

From my previous comment above, it looks like you can use PubSubClient v2.7.0 but you have to go into the PubSubClient code and change

#define MQTT\_MAX\_PACKET\_SIZE 128 to  
#define MQTT\_MAX\_PACKET\_SIZE 256

This will resolve the -1 error.

[Link to the resolved MKR1000 -1 issue](https://forums.losant.com/t/mkr1000-hangs-on-trying-to-connect-to-losant/1242/8)

-Kevin

---

<div class="post-metadata">

**Author:** ![Mark\_van\_der\_Pol](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/mark_van_der_pol/32/1366_2.png) [@Mark\_van\_der\_Pol](https://forums.losant.com/u/Mark_van_der_Pol)\
**Post date:** [May 6, 2019, 7:27am UTC](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329/7 "2019-05-06T07:27:55Z")

</div>

Down revving the PubSubClient was the key for me!  
v2.6.0 works.  
As well as setting the SSL cert, and the buffer size.

---

<div class="post-metadata">

**Author:** ![d\_funk](https://avatars.discourse-cdn.com/v4/letter/d/b5a626/32.png) [@d\_funk](https://forums.losant.com/u/d_funk)\
**Post date:** [May 20, 2019, 2:29am UTC](https://forums.losant.com/t/solved-cant-connect-to-losant-message-mqttclient-state-1/1329/8 "2019-05-20T02:29:18Z")

</div>

Finally got this to work with the non-secure connection as noted here and on a couple other threads.

Thanks for posting this.
