monstrenyatko / ArduinoMqtt

MQTT client for Arduino
MIT License
72 stars 13 forks source link

Got Disconnect when sent 3 messages in once using SSL #15

Closed tarzan115 closed 6 years ago

tarzan115 commented 7 years ago

hi @monstrenyatko I tested on MQTT use SSL when sent 3 messages in once. The device just receives the first message and then disconnected. I also tried in MQTT not have SSL, it works fine. My message has the length about 100 bytes for each. NET_MIN_TM_MS I set default is 10 My device is ESP8266

thank you and have a good day.

monstrenyatko commented 7 years ago

hi @tarzan115 The library doesn't handle the SSL. It uses provided Arduino Client compatible class to send/receive MQTT messages.

I believe your problem is related to ESP WiFiClientSecure implementation. SSL traffic takes much more resources of the ESP chip comparing to normal TCP traffic using ESP WiFiClient.

Need to check the amount of the free memory available on reception of the each message. I don't know the ESP network buffer size but It could be that the size is not enough to receive and keep all three messages between calls to the MqttClient::yield to consume data.

Please try to investigate more.

In parallel, I am going to prepare the SSL setup to try but it will take some time.

tarzan115 commented 7 years ago

okay, thank you. I am waiting for that prepare 😄

monstrenyatko commented 7 years ago

Hi @tarzan115 I noticed the code example in esp8266/Arduino#3627.

In callback method you allocate the new buffer for each new incoming message. Unfortunately, I don't see what is inside processJsonFromServerbut I believe you free that buffer in this function. My recommendation is to avoid allocations for each message and use a preallocated buffer if you can predict the maximum message size.

In loop you made the assumption that yield receives exactly one message on each call. Potentially the long enough yield call is able to receive more than one message. But you call yield with 1 ms value so probably the timer will expire right after the reception of the single message. Better to call yield with 0 ms value to be sure.

Please add additional debug log to check the amount of the free memory available in ESP on reception of the each message. Take a look on ESP.getFreeHeap and similar to investigate available ESP resources.

Does your ESP disconnect and successfully reconnect to the broker without restart or it just crashes and reconnect on next boot? Please attach some logs.

tarzan115 commented 7 years ago

hi @monstrenyatko I'm on testing and I also call yield with 0 ms but it still errors when ESP disconnected, MQTT does not reconnect anymore. I have to add mqtt->disconnect(); before network.stop(); which don't have in your example. I'm checking to see which one is taking up memory, need more time to find out.

tarzan115 commented 7 years ago

hi @monstrenyatko I have check ESP.getFreeHeap, each message has a value about 200 bytes and the memory free is about 13000 bytes. but when I turn off SSL the memory free is about 30000 bytes. much more with SSL.

tarzan115 commented 7 years ago

hi @monstrenyatko it not about the amount of the free memory. after fix some small bugs. The memory takes back all memory by MQTT and buffer. so I think this error from WiFiClientSecure library.

monstrenyatko commented 6 years ago

hi @tarzan115 Seems like the esp8266/Arduino#3627 is resolved. Could you please let me know If that helps.

tarzan115 commented 6 years ago

I work perfectly 😄 this issue can close for now.

thank you for your remind me.

monstrenyatko commented 6 years ago

@tarzan115 Great! Thank you for the confirmation.