Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setWill() cause mqtt could not connect with SSL enable #107

Closed
dzungpv opened this issue Jul 20, 2018 · 22 comments
Closed

setWill() cause mqtt could not connect with SSL enable #107

dzungpv opened this issue Jul 20, 2018 · 22 comments

Comments

@dzungpv
Copy link

dzungpv commented Jul 20, 2018

I am switch from PubSubClient becasue it block cpu. But i have a problem.
I enable SSL by change ASYNC_TCP_SSL_ENABLED to 1.
Then i set this line:

mqttClient.setWill(MQTT_LIGHT_AVAILABILITY_TOPIC, 0, true, MQTT_LIGHT_AVAILABILITY_LWTMESSAGE);

after

mqttClient.setServer(MQTT_HOST, MQTT_PORT);

But it will not connect to my server, remove the line it connected.

I have try to setWill() in onMqttConnect() but it not work.

@rneurink
Copy link

I am also having this problem. I can connect over SSL perfectly but when using the SetWill command it will not connect with an disconnect with reason 0

@bertmelis
Copy link
Contributor

I'm working without SSL but can't connect after setWill. After commenting out, it works just fine!

@bertmelis
Copy link
Contributor

bertmelis commented Sep 2, 2018

I figured out my problem. AsyncMqttClient doesn't copy the last will topic or value but only saves a pointer. And after setWill is called, topic and value might get out of scope.

I've marked topic and payload static to keep them available for when the mqtt connection is established (which is async and not in the same scope as setWill).

@rneurink
Copy link

rneurink commented Sep 4, 2018

@bertmelis
Could you post your code? I still seem to have the problem
This is my code https://pastebin.com/T4e9Ezc8
I'm getting : [ WARN ] Disconnected from MQTT. Reason: 0

@bertmelis
Copy link
Contributor

bertmelis commented Sep 4, 2018

You're using mosquitto? What does the log of your broker say?

FYI, my code is in https://github.com/bertmelis/TreeLight

I don't use SSL though.

@rneurink
Copy link

rneurink commented Sep 4, 2018

1536078725: Socket error on client , disconnecting.
1536078725: New connection from 192.168.0.107 on port 8883.
It is kind of weird how it tries to connect twice just after each other

@bertmelis
Copy link
Contributor

Without SSL it is working? It sure seems like some buffer got overwritten.

@rneurink
Copy link

rneurink commented Sep 4, 2018

It is working without SSL and it is working without the mqttClient.setWill
Edit: The log without the setwill
1536083371: New connection from 192.168.0.107 on port 8883.
1536083371: New client connected from 192.168.0.107 as esp8266055ab7 (c1, k20, u'').
1536083371: Sending CONNACK to esp8266055ab7 (0, 0)
1536083371: Received SUBSCRIBE from esp8266055ab7
1536083371: house/office/firealarm/command (QoS 0)
1536083371: esp8266055ab7 0 house/office/firealarm/command
1536083371: Sending SUBACK to esp8266055ab7
1536083385: Received PINGREQ from esp8266055ab7
1536083385: Sending PINGRESP to esp8266055ab7

@sooyewguan
Copy link

Same problem, setwill doesn't work with SSL

@timpur
Copy link
Collaborator

timpur commented Sep 13, 2018

Sadly ATM I lack time to investigate, but I will try to... When I can

@jeroenst
Copy link
Contributor

For some reason I now have the same problem when not using SSL. When I remove setwill from code I can connect, with setwill connection will fail, and the mosquitto logging says:

1545571378: New connection from 192.168.2.153 on port 1883.
1545571378: Socket error on client , disconnecting.

@sooyewguan
Copy link

Try change the lwIP to v2 Higher Bandwidth

@jeroenst
Copy link
Contributor

jeroenst commented Dec 23, 2018

My error was caused because the setwill function doesn't copy the *topic data but just stores the pointer. Because I called this from a separate mqttinit function, the topic was lost because the variable containing the topic was not static. But SSL is still not working though...

@ghost
Copy link

ghost commented Sep 21, 2019

Hi
is there a workaround to get setwill() with SSL working?

@Plggeldy
Copy link

I would love to know, what I can do for MQTTClient::setwill() with TLS/SSL..

Is there a chance to get it working?

@Plggeldy
Copy link

The solution is here:

https://github.com/256dpi/arduino-mqtt

Set a will message (last testament) that gets registered on the broker after connecting. setWill() has to be called before calling connect():<

@jeroenst
Copy link
Contributor

jeroenst commented Nov 1, 2019

The solution is here:

https://github.com/256dpi/arduino-mqtt

Set a will message (last testament) that gets registered on the broker after connecting. setWill() has to be called before calling connect():<

Is this really non-blocking asynchronous and for esp8266?

@jeroenst
Copy link
Contributor

jeroenst commented Nov 1, 2019

The problem is that the _onconnect function calls _client.add for each piece of data to send to the mqtt server. When setwill is used, there is to much data to be send by the ASyncTCP module because it also add ssl data to each packet, causing it to drop the password.

After changing the code to send less packets setwill works with SSL!

OttoWinter pushed a commit to OttoWinter/async-mqtt-client that referenced this issue Nov 7, 2019
marvinroger pushed a commit that referenced this issue Dec 8, 2019
* Solved setWill() cause mqtt could not connect with SSL enable #107
#107

* Fixed #160

* Fixed bug #147

* Fixed #166
- ESP32 now also uses a unique client id
- getClientId() fuction added

* Solved Minor Flaw #147 (comment)
kostko pushed a commit to kostko/async-mqtt-client that referenced this issue Mar 18, 2020
…#166 (marvinroger#165)

* Solved setWill() cause mqtt could not connect with SSL enable marvinroger#107
marvinroger#107

* Fixed marvinroger#160

* Fixed bug marvinroger#147

* Fixed marvinroger#166
- ESP32 now also uses a unique client id
- getClientId() fuction added

* Solved Minor Flaw marvinroger#147 (comment)
@KrYpToDeN
Copy link

KrYpToDeN commented Jun 4, 2020

The problem is that the _onconnect function calls _client.add for each piece of data to send to the mqtt server. When setwill is used, there is to much data to be send by the ASyncTCP module because it also add ssl data to each packet, causing it to drop the password.

After changing the code to send less packets setwill works with SSL!

Could you help me, how send less packets?
Our problem is the same - eclipse-mosquitto/mosquitto#1715

Will topic works sometimes, not always.

@qcasey
Copy link

qcasey commented Jun 4, 2020

I believe this is ultimately caused by #193. What I've done to get SSL working is implementing a queue and only allowing the packet in the head to be on the wire at any time.

This requires QoS2 and popping packets once they're acknowledged

@KrYpToDeN
Copy link

KrYpToDeN commented Jun 5, 2020

I believe this is ultimately caused by #193. What I've done to get SSL working is implementing a queue and only allowing the packet in the head to be on the wire at any time.

This requires QoS2 and popping packets once they're acknowledged

Hello.

Thank you for your answer, but it's for asynchronous code.

We use synchronous version, because packets not so much to send/get.

But the problem is the same. Server got Will topic in mosquitto log file. But it publishes 50/50 times. Also we can't find a way, when it's happed. It's always different. Will topic can go/it may be lost.

Do you know how solve it?

@bertmelis
Copy link
Contributor

The issue should be gone using the development branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants