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

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

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:

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) 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.

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)

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

Any help would be greatly appreciated

I have a MKR1000 and I am following the example here.

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

2 Likes

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

-Kevin

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

1 Like

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

Thanks for posting this.

2 Likes