Add SSL support for MQTT#285
Conversation
copyrights
commented
May 27, 2018
- Replace PubSubClient with AsyncMqttClient library
- Add new setting MQTT secure
- Add new setting MQTT server fingerprint
* Add new setting MQTT secure * Add new setting MQTT server fingerprint
|
This is excellent. How much testing have you done? |
|
My use-case is milight remote -> esp8266_milight_hub (NRF24L01) -> MQTT (user/pw,SSL+Fingerprint) -> mosquitto -> OpenHAB. So I mainly focus on that with "MQTT update topic pattern". I did some basic {"state":"ON"} {"state":"OFF"} tests with "MQTT topic pattern". When I active "MQTT state topic pattern" the MQTT message only shows a single "{". As I didn't use that feature (nor esp8266_milight_hub) before, I don't know what to expect here. Furthermore I did some negative testing with a wrong fingerprint. The esp8266 didn't connect to the broker (as expected). |
|
One more comment on my implementation. |
|
The state topic message should always be valid JSON. Perhaps published messages are getting clipped somewhere? re: asysnc, makes sense. This is the main reason I asked the question, actually. I'd love to refactor more of the library to be async-compatible because this is the main hurdle to using an ESP32. This has felt like a pretty significant undertaking to me, but perhaps I'm overestimating. |
|
Oh, I think I just found the issue with the MQTT state behaviour. I will fix and test it. |
The previous library has retain boolean on a different positon. I accendently put the retain boolean to the message length position. As long as retain false it gets casted into 0 which is equal to no length set, take whole string. When retain is true string length is 1.
|
Found it, fixed it, tested it. MQTT state now publish state as valid JSON. |
|
Nice, good find. Looks like that fixed the clipped message problem. I think we might need to poke at the asynchronicity a bit more. We're losing packets. For example, if I spam 8 command messages really quickly like so: This is what I see on MQTT: Notice that there's only one update. It seems like the ESP is dropping packets. If I spam the command a couple of times, I see one or two more packets sent, but it's never the full 8. For reference: the same test on 1.7.1 passes:I'm guessing this is because your callback only buffers one message. We should probably buffer more than that. I would suggest using One last point -- free heap is about 5 KB lower on this branch (that's about 20% of free heap on the 1.7.1 branch). Guessing that's just TLS, but I haven't confirmed yet. If there's any low-hanging fruit you're aware of to trim memory usage, we should probably explore that. |
Without ASYNC_TCP_SSL_ENABLED more free heap is available, but SSL is disabled.
|
About the free heap it seems to be SSL. If you compile without ASYNC_TCP_SSL_ENABLED in build_flags, free heap does increase. I have a SSL-only broker, so I can't test how much it is after a connection is established. Your test results are as I would expect. if(!mqttmsg) // For now irgnore new message while process last message.I will look into |
|
I did some quick testing with Next I wanted to know how IMHO a good compromise would be to only queue up a certain amount messages/commands and afterwards drop the rest until a threshold level in the queue is reached. Better to drop than to block. To achieve this I think the messages should be parsed into a memory-efficient command object inside the callback. I haven’t check yet how small this object could get, but wouldn’t except more than a few bytes. Any thought on that? |
The PubSubClient bar is probably set by whatever the SDK's internal TCP buffer can handle given that packets are being handled synchronously.
Yes, this is exactly what I was meaning to suggest with using
I haven't thought too much about how to structure the buffer, but yeah, definitely not good to buffer the 400 bytes per message or whatever it is. A couple of suggestions:
I probably prefer (1) because it's way more straightforward. |
|
@copyrights no rush, but wanted to check if you'd had a chance to look at this. I'm also happy to help out however I can. :) |
|
@sidoh sorry, I was quiet busy. It's still at the top on my hobby list. Hopefully I'll find some time for it soon. |
|
Oops, think this got closed automatically when I nuked the 1.8 branch. Re-opening. |
|
can the pull request be merged? |
|
@Timo2208 - no, we'll need to fix the issues discussed earlier in this thread before merging. @copyrights - no worries, I'll take a stab at it at some point. :) (also obviously happy to look at updates to the PR from anyone else who wants to have a go at it) |
|
Packet queuing is implemented in v1.10, which should enable using the async MQTT library. |
|
Is this still in scope so you can use SSL on MQTT? |